예제 #1
0
void parse_command_line(int argc, char **argv)
{
   comm_port *port;
   comm       comn = _com1;
   int        i;

   if (argc > 0) {
      for (i=1; i<argc; i++) {
         if (!strcmp(argv[i], "-h")) {
	    give_usage(stderr);
            exit(0);
	 }
	 else {
	    port = comm_port_init(comn++);
	    if (port == NULL) {
	       dz_print_comm_err();
	       exit(1);
	    }
	    if (comm_port_load_settings(port, argv[i]) != 1) {
	       dz_print_comm_err();
	       exit(1);
	    }
	    if (!port1) port1 = port;
	    else port2 = port;
	 }
      }
   }


   /* Default condition: One port, use COM A */
   if (port1 == NULL) {
      port1 = comm_port_init(comn++);
      if (port1 == NULL) {
	 dz_print_comm_err();
	 exit(1);
      }
   }

   if (port1) {
      if (comm_port_install_handler(port1) != 1) {
	  dz_print_comm_err();
	 exit(1);
      }
   }
   if (port2) {
      if (comm_port_install_handler(port2) != 1) {
	  dz_print_comm_err();
	 exit(1);
      }
   }
}
예제 #2
0
파일: serial.c 프로젝트: kees/scantool
int open_comport()
{
#ifdef ALLEGRO_WINDOWS
   DCB dcb;
   COMMTIMEOUTS timeouts;
   DWORD bytes_written;
   char temp_str[16];
#endif
   
   if (comport.status == READY)    // if the comport is open,
      close_comport();    // close it
   
#ifdef ALLEGRO_WINDOWS
   // Naming of serial ports 10 and higher: See
   // http://www.connecttech.com/KnowledgeDatabase/kdb227.htm
   // http://support.microsoft.com/?id=115831
   sprintf(temp_str, "\\\\.\\COM%i", comport.number + 1);
   com_port = CreateFile(temp_str, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
   if (com_port == INVALID_HANDLE_VALUE)
   {
      comport.status = NOT_OPEN; //port was not open
      return -1; // return error
   }
   
   // Setup comport
   GetCommState(com_port, &dcb);
   dcb.BaudRate = comport.baud_rate;
   dcb.ByteSize = 8;
   dcb.StopBits = ONESTOPBIT;
   dcb.fParity = FALSE;
   dcb.Parity = NOPARITY;
   dcb.fOutxCtsFlow = FALSE;
   dcb.fOutxDsrFlow = FALSE;
   dcb.fOutX = FALSE;
   dcb.fInX = FALSE;
   dcb.fDtrControl = DTR_CONTROL_ENABLE;
   dcb.fRtsControl = RTS_CONTROL_ENABLE;
   dcb.fDsrSensitivity = FALSE;
   dcb.fErrorChar = FALSE;
   dcb.fAbortOnError = FALSE;
   SetCommState(com_port, &dcb);
   
   // Setup comm timeouts
   timeouts.ReadIntervalTimeout = MAXWORD;
   timeouts.ReadTotalTimeoutMultiplier = 0;
   timeouts.ReadTotalTimeoutConstant = 0;
   timeouts.WriteTotalTimeoutMultiplier = TX_TIMEOUT_MULTIPLIER;
   timeouts.WriteTotalTimeoutConstant = TX_TIMEOUT_CONSTANT;
   SetCommTimeouts(com_port, &timeouts);
   // Hack to get around Windows 2000 multiplying timeout values by 15
   GetCommTimeouts(com_port, &timeouts);
   if (TX_TIMEOUT_MULTIPLIER > 0)
      timeouts.WriteTotalTimeoutMultiplier = TX_TIMEOUT_MULTIPLIER * TX_TIMEOUT_MULTIPLIER / timeouts.WriteTotalTimeoutMultiplier;
   if (TX_TIMEOUT_CONSTANT > 0)
      timeouts.WriteTotalTimeoutConstant = TX_TIMEOUT_CONSTANT * TX_TIMEOUT_CONSTANT / timeouts.WriteTotalTimeoutConstant;
   SetCommTimeouts(com_port, &timeouts);
   
   // If the port is Bluetooth, make sure device is active
   PurgeComm(com_port, PURGE_TXCLEAR|PURGE_RXCLEAR);
   WriteFile(com_port, "?\r", 2, &bytes_written, 0);
   if (bytes_written != 2)  // If Tx timeout occured
   {
      PurgeComm(com_port, PURGE_TXCLEAR|PURGE_RXCLEAR);
      CloseHandle(com_port);
      comport.status = NOT_OPEN; //port was not open
      return -1;
   }
#elif TERMIOS
    char tmp[54];
    if( comport.number < 100 )
        snprintf(tmp, sizeof(tmp), "/dev/ttyS%d", comport.number);
    else
        snprintf(tmp, sizeof(tmp), "/dev/ttyUSB%d", comport.number-100);
    fdtty = open( tmp, O_RDWR | O_NOCTTY );
    if (fdtty <0) { return(-1); }

    tcgetattr(fdtty,&oldtio); /* save current port settings */

    bzero(&newtio, sizeof(newtio));

    cfsetspeed(&newtio, comport.baud_rate);

    cfmakeraw(&newtio);
    newtio.c_cflag |= (CLOCAL | CREAD);

            // No parity (8N1):
    newtio.c_cflag &= ~PARENB;
    newtio.c_cflag &= ~CSTOPB;
    newtio.c_cflag &= ~CSIZE;
    newtio.c_cflag |= CS8;

        // disable hardware flow control
    newtio.c_cflag &= ~CRTSCTS ;

    newtio.c_cc[VTIME]    = 0;   /* inter-character timer unused */
    newtio.c_cc[VMIN]     = 0;   /* blocking read until 5 chars received */

    tcflush(fdtty, TCIFLUSH);
    tcsetattr(fdtty,TCSANOW,&newtio);
#else
   com_port = comm_port_init(comport.number);
   if (!com_port) {
      write_log(szDZCommErr);
      comport.status = NOT_OPEN;
      return -1;
   }
   comm_port_set_baud_rate(com_port, comport.baud_rate);
   comm_port_set_parity(com_port, NO_PARITY);
   comm_port_set_data_bits(com_port, BITS_8);
   comm_port_set_stop_bits(com_port, STOP_1);
   comm_port_set_flow_control(com_port, NO_CONTROL);
   if (comm_port_install_handler(com_port) != 1)
   {
      write_log(szDZCommErr);
      comport.status = NOT_OPEN; //port was not open
      return -1; // return error
   }
#endif
   
   serial_time_out = FALSE;
   comport.status = READY;
   
   return 0; // everything is okay
}
예제 #3
0
파일: serial.c 프로젝트: jantman/python-obd
int open_comport()
{
#ifdef ALLEGRO_WINDOWS
   DCB dcb;
   COMMTIMEOUTS timeouts;
   char temp_str[16];
#endif

   if (comport.status == READY)    // if the comport is open,
      close_comport();    // close it

#ifdef ALLEGRO_WINDOWS
   sprintf(temp_str, "COM%i", comport.number + 1);
   com_port = CreateFile(temp_str, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
   if (com_port == INVALID_HANDLE_VALUE)
   {
      comport.status = NOT_OPEN; //port was not open
      return -1; // return error
   }

   GetCommState(com_port, &dcb);
   dcb.BaudRate = comport.baud_rate;
   dcb.ByteSize = 8;
   dcb.StopBits = ONESTOPBIT;
   dcb.fParity = FALSE;
   dcb.Parity = NOPARITY;
   dcb.fOutxCtsFlow = FALSE;
   dcb.fOutxDsrFlow = FALSE;
   dcb.fOutX = FALSE;
   dcb.fInX = FALSE;
   dcb.fDtrControl = DTR_CONTROL_ENABLE;
   dcb.fRtsControl = RTS_CONTROL_ENABLE;
   dcb.fDsrSensitivity = FALSE;
   dcb.fErrorChar = FALSE;
   dcb.fAbortOnError = FALSE;
   SetCommState(com_port, &dcb);
   
   timeouts.ReadIntervalTimeout = MAXWORD;
   timeouts.ReadTotalTimeoutMultiplier = 0;
   timeouts.ReadTotalTimeoutConstant = 0;
   timeouts.WriteTotalTimeoutMultiplier = 0;
   timeouts.WriteTotalTimeoutConstant = 0;
   SetCommTimeouts(com_port, &timeouts);
#else
   com_port = comm_port_init(comport.number);
   comm_port_set_baud_rate(com_port, comport.baud_rate);
   comm_port_set_parity(com_port, NO_PARITY);
   comm_port_set_data_bits(com_port, BITS_8);
   comm_port_set_stop_bits(com_port, STOP_1);
   comm_port_set_flow_control(com_port, NO_CONTROL);
   if (comm_port_install_handler(com_port) != 1)
   {
      comport.status = NOT_OPEN; //port was not open
      return -1; // return error
   }
#endif

   serial_time_out = FALSE;
   comport.status = READY;
   
   return 0; // everything is okay
}
예제 #4
0
int main(void)
{
   int           c;
#ifdef ALLEGRO_H
   unsigned char ch;

   allegro_init();
   install_keyboard();
#endif
   dzcomm_init();


   /* Set up comm1 */

   if ((port1 = comm_port_init(_com1)) == NULL) {
      dz_print_comm_err();
      exit(1);
   }

   if (comm_port_load_settings(port1, "exterm1.ini") == 0) {
       dz_print_comm_err();
       exit(1);
   }

   if (!comm_port_install_handler(port1)) {
      dz_print_comm_err();
      exit(1);
   }

   cur_port = port1;

#ifdef ALLEGRO_H
   /* Set up comm2 */

   if ((port2 = comm_port_init(_com2)) == NULL) {
       dz_print_comm_err();
       exit(1);
   }

   if (comm_port_load_settings(port2, "exterm2.ini") == 0) {
       dz_print_comm_err();
       exit(1);
   }

   if (!comm_port_install_handler(port2)) {
      dz_print_comm_err();
      exit(1);
   }
#else
   port2 = port1;
#endif

#ifdef ALLEGRO_H
   initialise_screen();
#else
   printf("Press Ctrl-C for a messy quit.\n");
   printf("\nCurrent port is: %s.\n\n", cur_port->szName);
#endif

   while(1) {

#ifdef ALLEGRO_H
      if (keypressed()) {
         c  = readkey();
         ch = ascii_(c);
         if (ctrl_(c,'C'))  {
            return (0);
         }
	 else if (ctrl_(c,'B')) {
	    comm_port_send_break(cur_port, 500);
	 }
         else if (ctrl_(c,'M')) {
            if (cur_port == port2) cur_port = port1;
            else                   cur_port = port2;
	    inform_port_change();
         }
         else comm_port_out(cur_port, ch);
      }
#endif

      if ((c = comm_port_test(cur_port)) != -1) {
	 show_received_character(data_(c));
      }
   }

}
예제 #5
0
int open_comport()
{
#ifdef ALLEGRO_WINDOWS
   DCB dcb;
   COMMTIMEOUTS timeouts;
   DWORD bytes_written;
   char temp_str[16];
#endif
   
   if (comport.status == READY)    // if the comport is open,
      close_comport();    // close it
   
#ifdef ALLEGRO_WINDOWS
   // Naming of serial ports 10 and higher: See
   // http://www.connecttech.com/KnowledgeDatabase/kdb227.htm
   // http://support.microsoft.com/?id=115831
   sprintf(temp_str, "\\\\.\\COM%i", comport.number + 1);
   com_port = CreateFile(temp_str, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
   if (com_port == INVALID_HANDLE_VALUE)
   {
      comport.status = NOT_OPEN; //port was not open
      return -1; // return error
   }
   
   // Setup comport
   GetCommState(com_port, &dcb);
   dcb.BaudRate = comport.baud_rate;
   dcb.ByteSize = 8;
   dcb.StopBits = ONESTOPBIT;
   dcb.fParity = FALSE;
   dcb.Parity = NOPARITY;
   dcb.fOutxCtsFlow = FALSE;
   dcb.fOutxDsrFlow = FALSE;
   dcb.fOutX = FALSE;
   dcb.fInX = FALSE;
   dcb.fDtrControl = DTR_CONTROL_ENABLE;
   dcb.fRtsControl = RTS_CONTROL_ENABLE;
   dcb.fDsrSensitivity = FALSE;
   dcb.fErrorChar = FALSE;
   dcb.fAbortOnError = FALSE;
   SetCommState(com_port, &dcb);
   
   // Setup comm timeouts
   timeouts.ReadIntervalTimeout = MAXWORD;
   timeouts.ReadTotalTimeoutMultiplier = 0;
   timeouts.ReadTotalTimeoutConstant = 0;
   timeouts.WriteTotalTimeoutMultiplier = TX_TIMEOUT_MULTIPLIER;
   timeouts.WriteTotalTimeoutConstant = TX_TIMEOUT_CONSTANT;
   SetCommTimeouts(com_port, &timeouts);
   // Hack to get around Windows 2000 multiplying timeout values by 15
   GetCommTimeouts(com_port, &timeouts);
   if (TX_TIMEOUT_MULTIPLIER > 0)
      timeouts.WriteTotalTimeoutMultiplier = TX_TIMEOUT_MULTIPLIER * TX_TIMEOUT_MULTIPLIER / timeouts.WriteTotalTimeoutMultiplier;
   if (TX_TIMEOUT_CONSTANT > 0)
      timeouts.WriteTotalTimeoutConstant = TX_TIMEOUT_CONSTANT * TX_TIMEOUT_CONSTANT / timeouts.WriteTotalTimeoutConstant;
   SetCommTimeouts(com_port, &timeouts);
   
   // If the port is Bluetooth, make sure device is active
   PurgeComm(com_port, PURGE_TXCLEAR|PURGE_RXCLEAR);
   WriteFile(com_port, "?\r", 2, &bytes_written, 0);
   if (bytes_written != 2)  // If Tx timeout occured
   {
      PurgeComm(com_port, PURGE_TXCLEAR|PURGE_RXCLEAR);
      CloseHandle(com_port);
      comport.status = NOT_OPEN; //port was not open
      return -1;
   }
#else
   com_port = comm_port_init(comport.number);
   comm_port_set_baud_rate(com_port, comport.baud_rate);
   comm_port_set_parity(com_port, NO_PARITY);
   comm_port_set_data_bits(com_port, BITS_8);
   comm_port_set_stop_bits(com_port, STOP_1);
   comm_port_set_flow_control(com_port, NO_CONTROL);
   if (comm_port_install_handler(com_port) != 1)
   {
      comport.status = NOT_OPEN; //port was not open
      return -1; // return error
   }
#endif
   
   serial_time_out = FALSE;
   comport.status = READY;
   
   return 0; // everything is okay
}