bool Dynamixel::open_serial(const char *serial_device) { serial_fd_ = CreateFileA(serial_device, GENERIC_READ | GENERIC_WRITE, 0, /* no share */ NULL, /* no security */ OPEN_EXISTING, 0, /* no threads */ NULL); /* no templates */ char mode_str[] = "baud=1000000 data=8 parity=n stop=1"; if(serial_fd_==INVALID_HANDLE_VALUE) return false; DCB port_settings; memset(&port_settings, 0, sizeof(port_settings)); /* clear the new struct */ port_settings.DCBlength = sizeof(port_settings); COMMTIMEOUTS Cptimeouts; Cptimeouts.ReadIntervalTimeout = MAXDWORD; Cptimeouts.ReadTotalTimeoutMultiplier = 0; Cptimeouts.ReadTotalTimeoutConstant = 0; Cptimeouts.WriteTotalTimeoutMultiplier = 0; Cptimeouts.WriteTotalTimeoutConstant = 0; if (!BuildCommDCBA(mode_str, &port_settings) || !SetCommState(serial_fd_, &port_settings) || !SetCommTimeouts(serial_fd_, &Cptimeouts)) { CloseHandle(serial_fd_); return false; } return true; }
/************************************************************************** * BuildCommDCB (USER.213) * * According to the ECMA-234 (368.3) the function will return FALSE on * success, otherwise it will return -1. */ INT16 WINAPI BuildCommDCB16(LPCSTR device, LPDCB16 lpdcb) { /* "COM1:96,n,8,1" */ /* 012345 */ int port; DCB dcb; TRACE("(%s), ptr %p\n", device, lpdcb); if (strncasecmp(device,"COM",3)) return -1; port = device[3] - '0'; if (port-- == 0) { ERR("BUG ! COM0 can't exist!\n"); return -1; } memset(lpdcb, 0, sizeof(DCB16)); /* initialize */ lpdcb->Id = port; dcb.DCBlength = sizeof(DCB); if (strchr(device,'=')) /* block new style */ return -1; if(!BuildCommDCBA(device,&dcb)) return -1; return COMM16_DCBtoDCB16(&dcb, lpdcb); }
bool serial_port::open(int comport_number, int baud) { if((comport_number>15)||(comport_number<0)) { printf("illegal comport number\n"); return(false); } sprintf(_baud_str, "baud=%d data=8 parity=N stop=1", baud); _baudrate = baud; _port_handle = CreateFileA(comports[comport_number], GENERIC_READ|GENERIC_WRITE, 0, /* no share */ NULL, /* no security */ OPEN_EXISTING, 0, /* no threads */ NULL); /* no templates */ if(_port_handle == INVALID_HANDLE_VALUE) { printf("unable to open comport\n"); return(false); } DCB port_settings; memset(&port_settings, 0, sizeof(port_settings)); /* clear the new struct */ port_settings.DCBlength = sizeof(port_settings); if(!BuildCommDCBA(_baud_str, &port_settings)) { printf("unable to set comport dcb settings\n"); CloseHandle(_port_handle); _port_handle = INVALID_HANDLE_VALUE; return(false); } if(!SetCommState(_port_handle, &port_settings)) { printf("unable to set comport cfg settings\n"); CloseHandle(_port_handle); _port_handle = INVALID_HANDLE_VALUE; //not sure this is required return(false); } COMMTIMEOUTS Cptimeouts; Cptimeouts.ReadIntervalTimeout = MAXDWORD; Cptimeouts.ReadTotalTimeoutMultiplier = 0; Cptimeouts.ReadTotalTimeoutConstant = 0; Cptimeouts.WriteTotalTimeoutMultiplier = 0; Cptimeouts.WriteTotalTimeoutConstant = 0; if(!SetCommTimeouts(_port_handle, &Cptimeouts)) { printf("unable to set comport time-out settings\n"); CloseHandle(_port_handle); return(false); } _port_name = comports[_port_number]; return(true); }
void initSerialPort() { char mode[40],portname[20]; COMMTIMEOUTS timeout_sets; DCB port_sets; strcpy(portname,"\\\\.\\"); strcat(portname,COM); port_handle = CreateFileA(portname, GENERIC_READ|GENERIC_WRITE, 0, /* no share */ NULL, /* no security */ OPEN_EXISTING, 0, /* no threads */ NULL); /* no templates */ if(port_handle==INVALID_HANDLE_VALUE) { printf("unable to open port %s -> %s\n",COM, portname); exit(0); } strcpy (mode,"baud=57600 data=8 parity=n stop=1"); memset(&port_sets, 0, sizeof(port_sets)); /* clear the new struct */ port_sets.DCBlength = sizeof(port_sets); if(!BuildCommDCBA(mode, &port_sets)) { printf("dcb settings failed\n"); CloseHandle(port_handle); exit(0); } if(!SetCommState(port_handle, &port_sets)) { printf("cfg settings failed\n"); CloseHandle(port_handle); exit(0); } timeout_sets.ReadIntervalTimeout = 1; timeout_sets.ReadTotalTimeoutMultiplier = 1000; timeout_sets.ReadTotalTimeoutConstant = 1; timeout_sets.WriteTotalTimeoutMultiplier = 1000; timeout_sets.WriteTotalTimeoutConstant = 1; if(!SetCommTimeouts(port_handle, &timeout_sets)) { printf("timeout settings failed\n"); CloseHandle(port_handle); exit(0); } }
int open_com(DeviceType device, HANDLE *p_result) { const char *dcb_string = "baud=9600 parity=N data=8 stop=1"; int port_number = 1; HANDLE com_port = INVALID_HANDLE_VALUE; char port_name[] = { '\\', '\\', '.', '\\', 'C', 'O', 'M', '0', 0 }; DCB port_dcb = {sizeof(DCB)}; COMMTIMEOUTS port_timeouts = {MAXDWORD, MAXDWORD, 1000, 0, 0 }; switch (device) { case CALIBRATOR: port_number = 1; break; case VOLTM_VER: port_number++; case VOLTM_REF: port_number++; dcb_string = "baud=9600 parity=N data=8 stop=2"; break; default: fprintf (stderr, "Unexpected device code %d\n", device); exit(1); } port_name[countof(port_name) - 2] += port_number; printf("name=%s\n", port_name); com_port = CreateFileA( port_name, GENERIC_READ | GENERIC_WRITE, 7, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL ); if (com_port == INVALID_HANDLE_VALUE) return ERROR_CODE; printf("h=%p\n", com_port); if (!BuildCommDCBA(dcb_string, &port_dcb)) return ERROR_CODE; if (!SetCommState(com_port, &port_dcb)) return ERROR_CODE; if (!SetCommTimeouts(com_port, &port_timeouts)) return ERROR_CODE; if (!SetupComm(com_port, 1024, 1024)) return ERROR_CODE; *p_result = com_port; return 0; }
int CSeries::SerOpen(int PortNo, char * param) { DCB dcb; BOOL fRetVal ; BYTE i,bSet; DWORD dwError; COMMTIMEOUTS to; char baudr[128] = {0}; TCHAR DevName[10]; wsprintf(DevName,"\\\\.\\COM%01d",(int)PortNo); SerClose(); if ((m_CommPort=CreateFile(DevName , GENERIC_READ | GENERIC_WRITE , 0 , NULL , OPEN_EXISTING , NULL , NULL )) == INVALID_HANDLE_VALUE) { return -1; } // set up DCB memset(&dcb, 0, sizeof(dcb)); dcb.DCBlength = sizeof(dcb) ; strcpy(baudr, (char *)param); if(!BuildCommDCBA(baudr, &dcb)) { SerClose(); return -1; } if(!SetCommState(m_CommPort, &dcb)) { SerClose(); return(-1); } to.ReadIntervalTimeout = MAXDWORD; to.ReadTotalTimeoutMultiplier = 0; to.ReadTotalTimeoutConstant = 0; to.WriteTotalTimeoutMultiplier = 0; to.WriteTotalTimeoutConstant = 0; if(!SetCommTimeouts(m_CommPort, &to) ) { SerClose(); return(-1); } return 0; }
serial_port uart_open(const char *pcPortName) { char acPortName[255]; struct serial_port_windows *sp = malloc(sizeof(struct serial_port_windows)); if (sp == 0) return INVALID_SERIAL_PORT; // Copy the input "com?" to "\\.\COM?" format sprintf(acPortName, "\\\\.\\%s", pcPortName); _strupr(acPortName); // Try to open the serial port sp->hPort = CreateFileA(acPortName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); if (sp->hPort == INVALID_HANDLE_VALUE) { uart_close(sp); return INVALID_SERIAL_PORT; } // Prepare the device control memset(&sp->dcb, 0, sizeof(DCB)); sp->dcb.DCBlength = sizeof(DCB); if (!BuildCommDCBA("baud=9600 data=8 parity=N stop=1", &sp->dcb)) { uart_close(sp); return INVALID_SERIAL_PORT; } // Update the active serial port if (!SetCommState(sp->hPort, &sp->dcb)) { uart_close(sp); return INVALID_SERIAL_PORT; } sp->ct.ReadIntervalTimeout = 30; sp->ct.ReadTotalTimeoutMultiplier = 0; sp->ct.ReadTotalTimeoutConstant = 30; sp->ct.WriteTotalTimeoutMultiplier = 30; sp->ct.WriteTotalTimeoutConstant = 0; if (!SetCommTimeouts(sp->hPort, &sp->ct)) { uart_close(sp); return INVALID_SERIAL_PORT; } PurgeComm(sp->hPort, PURGE_RXABORT | PURGE_RXCLEAR); return sp; }
int Serial::open(const std::string tty) { if (opened) { return 1; } com_handle = CreateFileA(tty.c_str(), GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); if (com_handle == INVALID_HANDLE_VALUE) { return 2; } DCB port_settings; memset(&port_settings, 0, sizeof(port_settings)); port_settings.DCBlength = sizeof(port_settings); if(!BuildCommDCBA("baud=9600 parity=N data=8 stop=1 dtr=on rts=on", &port_settings)) { CloseHandle(com_handle); return 5; } if(!SetCommState(com_handle, &port_settings)) { CloseHandle(com_handle); return 6; } COMMTIMEOUTS Cptimeouts; Cptimeouts.ReadIntervalTimeout = MAXDWORD; Cptimeouts.ReadTotalTimeoutMultiplier = 0; Cptimeouts.ReadTotalTimeoutConstant = 0; Cptimeouts.WriteTotalTimeoutMultiplier = 0; Cptimeouts.WriteTotalTimeoutConstant = 0; if(!SetCommTimeouts(com_handle, &Cptimeouts)) { CloseHandle(com_handle); return 7; } opened = true; return 0; }
//---------------------------------------------------------------- bool ofSerial::setup(std::string portName, int baud){ bInited = false; //--------------------------------------------- #if defined( TARGET_OSX ) || defined( TARGET_LINUX ) //--------------------------------------------- //lets account for the name being passed in instead of the device path if( portName.size() > 5 && portName.substr(0, 5) != "/dev/" ){ portName = "/dev/" + portName; } ofLog(OF_LOG_NOTICE,"ofSerialInit: opening port %s @ %d bps", portName.c_str(), baud); fd = open(portName.c_str(), O_RDWR | O_NOCTTY | O_NONBLOCK); if(fd == -1){ ofLog(OF_LOG_ERROR,"ofSerial: unable to open port %s", portName.c_str()); return false; } struct termios options; tcgetattr(fd,&oldoptions); options = oldoptions; switch(baud){ case 300: cfsetispeed(&options,B300); cfsetospeed(&options,B300); break; case 1200: cfsetispeed(&options,B1200); cfsetospeed(&options,B1200); break; case 2400: cfsetispeed(&options,B2400); cfsetospeed(&options,B2400); break; case 4800: cfsetispeed(&options,B4800); cfsetospeed(&options,B4800); break; case 9600: cfsetispeed(&options,B9600); cfsetospeed(&options,B9600); break; case 14400: cfsetispeed(&options,B14400); cfsetospeed(&options,B14400); break; case 19200: cfsetispeed(&options,B19200); cfsetospeed(&options,B19200); break; case 28800: cfsetispeed(&options,B28800); cfsetospeed(&options,B28800); break; case 38400: cfsetispeed(&options,B38400); cfsetospeed(&options,B38400); break; case 57600: cfsetispeed(&options,B57600); cfsetospeed(&options,B57600); break; case 115200: cfsetispeed(&options,B115200); cfsetospeed(&options,B115200); break; default: cfsetispeed(&options,B9600); cfsetospeed(&options,B9600); ofLog(OF_LOG_ERROR,"ofSerialInit: cannot set %i baud setting baud to 9600", baud); break; } options.c_cflag |= (CLOCAL | CREAD); options.c_cflag &= ~PARENB; options.c_cflag &= ~CSTOPB; options.c_cflag &= ~CSIZE; options.c_cflag |= CS8; tcsetattr(fd,TCSANOW,&options); bInited = true; ofLog(OF_LOG_NOTICE,"sucess in opening serial connection"); return true; //--------------------------------------------- #endif //--------------------------------------------- //--------------------------------------------- #ifdef TARGET_WIN32 //--------------------------------------------- // open the serial port: // "COM4", etc... hComm=CreateFileA(portName.c_str(),GENERIC_READ|GENERIC_WRITE,0,0, OPEN_EXISTING,0,0); if(hComm==INVALID_HANDLE_VALUE){ ofLog(OF_LOG_ERROR,"ofSerial: unable to open port"); return false; } // now try the settings: COMMCONFIG cfg; DWORD cfgSize; char buf[80]; cfgSize=sizeof(cfg); GetCommConfig(hComm,&cfg,&cfgSize); int bps = baud; sprintf_s(buf,80,"baud=%d parity=N data=8 stop=1",bps); #if (_MSC_VER) // microsoft visual studio // msvc doesn't like BuildCommDCB, //so we need to use this version: BuildCommDCBA if(!BuildCommDCBA(buf,&cfg.dcb)){ ofLog(OF_LOG_ERROR,"ofSerial: unable to build comm dcb; (%s)",buf); } #else if(!BuildCommDCB(buf,&cfg.dcb)){ ofLog(OF_LOG_ERROR,"ofSerial: Can't build comm dcb; %s",buf); } #endif // Set baudrate and bits etc. // Note that BuildCommDCB() clears XON/XOFF and hardware control by default if(!SetCommState(hComm,&cfg.dcb)){ ofLog(OF_LOG_ERROR,"ofSerial: Can't set comm state"); } //ofLog(OF_LOG_NOTICE,buf,"bps=%d, xio=%d/%d",cfg.dcb.BaudRate,cfg.dcb.fOutX,cfg.dcb.fInX); // Set communication timeouts (NT) COMMTIMEOUTS tOut; GetCommTimeouts(hComm,&oldTimeout); tOut = oldTimeout; // Make timeout so that: // - return immediately with buffered characters tOut.ReadIntervalTimeout=MAXDWORD; tOut.ReadTotalTimeoutMultiplier=0; tOut.ReadTotalTimeoutConstant=0; SetCommTimeouts(hComm,&tOut); bInited = true; return true; //--------------------------------------------- #endif //--------------------------------------------- }
int RS232_OpenComport(int comport_number, int baudrate, const char *mode) { if((comport_number>=RS232_PORTNR)||(comport_number<0)) { printf("illegal comport number\n"); return(1); } switch(baudrate) { case 110 : strcpy(mode_str, "baud=110"); break; case 300 : strcpy(mode_str, "baud=300"); break; case 600 : strcpy(mode_str, "baud=600"); break; case 1200 : strcpy(mode_str, "baud=1200"); break; case 2400 : strcpy(mode_str, "baud=2400"); break; case 4800 : strcpy(mode_str, "baud=4800"); break; case 9600 : strcpy(mode_str, "baud=9600"); break; case 19200 : strcpy(mode_str, "baud=19200"); break; case 38400 : strcpy(mode_str, "baud=38400"); break; case 57600 : strcpy(mode_str, "baud=57600"); break; case 115200 : strcpy(mode_str, "baud=115200"); break; case 128000 : strcpy(mode_str, "baud=128000"); break; case 256000 : strcpy(mode_str, "baud=256000"); break; case 500000 : strcpy(mode_str, "baud=500000"); break; case 1000000 : strcpy(mode_str, "baud=1000000"); break; default : printf("invalid baudrate\n"); return(1); break; } if(strlen(mode) != 3) { printf("invalid mode \"%s\"\n", mode); return(1); } switch(mode[0]) { case '8': strcat(mode_str, " data=8"); break; case '7': strcat(mode_str, " data=7"); break; case '6': strcat(mode_str, " data=6"); break; case '5': strcat(mode_str, " data=5"); break; default : printf("invalid number of data-bits '%c'\n", mode[0]); return(1); break; } switch(mode[1]) { case 'N': case 'n': strcat(mode_str, " parity=n"); break; case 'E': case 'e': strcat(mode_str, " parity=e"); break; case 'O': case 'o': strcat(mode_str, " parity=o"); break; default : printf("invalid parity '%c'\n", mode[1]); return(1); break; } switch(mode[2]) { case '1': strcat(mode_str, " stop=1"); break; case '2': strcat(mode_str, " stop=2"); break; default : printf("invalid number of stop bits '%c'\n", mode[2]); return(1); break; } strcat(mode_str, " dtr=on rts=on"); /* http://msdn.microsoft.com/en-us/library/windows/desktop/aa363145%28v=vs.85%29.aspx http://technet.microsoft.com/en-us/library/cc732236.aspx */ Cport[comport_number] = CreateFileA(comports[comport_number], GENERIC_READ|GENERIC_WRITE, 0, /* no share */ NULL, /* no security */ OPEN_EXISTING, 0, /* no threads */ NULL); /* no templates */ if(Cport[comport_number]==INVALID_HANDLE_VALUE) { printf("unable to open comport\n"); return(1); } DCB port_settings; memset(&port_settings, 0, sizeof(port_settings)); /* clear the new struct */ port_settings.DCBlength = sizeof(port_settings); if(!BuildCommDCBA(mode_str, &port_settings)) { printf("unable to set comport dcb settings\n"); CloseHandle(Cport[comport_number]); return(1); } if(!SetCommState(Cport[comport_number], &port_settings)) { printf("unable to set comport cfg settings\n"); CloseHandle(Cport[comport_number]); return(1); } COMMTIMEOUTS Cptimeouts; Cptimeouts.ReadIntervalTimeout = MAXDWORD; Cptimeouts.ReadTotalTimeoutMultiplier = 0; Cptimeouts.ReadTotalTimeoutConstant = 0; Cptimeouts.WriteTotalTimeoutMultiplier = 0; Cptimeouts.WriteTotalTimeoutConstant = 0; if(!SetCommTimeouts(Cport[comport_number], &Cptimeouts)) { printf("unable to set comport time-out settings\n"); CloseHandle(Cport[comport_number]); return(1); } return(0); }
int OpenComport(int comport_number, int baudrate) { if((comport_number>15)||(comport_number<0)) { printf("illegal comport number\n"); return(1); } switch(baudrate) { case 110 : strcpy(baudr, "baud=110 data=8 parity=N stop=1"); break; case 300 : strcpy(baudr, "baud=300 data=8 parity=N stop=1"); break; case 600 : strcpy(baudr, "baud=600 data=8 parity=N stop=1"); break; case 1200 : strcpy(baudr, "baud=1200 data=8 parity=N stop=1"); break; case 2400 : strcpy(baudr, "baud=2400 data=8 parity=N stop=1"); break; case 4800 : strcpy(baudr, "baud=4800 data=8 parity=N stop=1"); break; case 9600 : strcpy(baudr, "baud=9600 data=8 parity=N stop=1"); break; case 19200 : strcpy(baudr, "baud=19200 data=8 parity=N stop=1"); break; case 38400 : strcpy(baudr, "baud=38400 data=8 parity=N stop=1"); break; case 57600 : strcpy(baudr, "baud=57600 data=8 parity=N stop=1"); break; case 115200 : strcpy(baudr, "baud=115200 data=8 parity=N stop=1"); break; case 128000 : strcpy(baudr, "baud=128000 data=8 parity=N stop=1"); break; case 256000 : strcpy(baudr, "baud=256000 data=8 parity=N stop=1"); break; default : printf("invalid baudrate\n"); return(1); break; } Cport[comport_number] = CreateFileA(comports[comport_number], GENERIC_READ|GENERIC_WRITE, 0, /* no share */ NULL, /* no security */ OPEN_EXISTING, 0, /* no threads */ NULL); /* no templates */ if(Cport[comport_number]==INVALID_HANDLE_VALUE) { printf("unable to open comport\n"); return(1); } DCB port_settings; memset(&port_settings, 0, sizeof(port_settings)); /* clear the new struct */ port_settings.DCBlength = sizeof(port_settings); if(!BuildCommDCBA(baudr, &port_settings)) { printf("unable to set comport dcb settings\n"); CloseHandle(Cport[comport_number]); return(1); } if(!SetCommState(Cport[comport_number], &port_settings)) { printf("unable to set comport cfg settings\n"); CloseHandle(Cport[comport_number]); return(1); } COMMTIMEOUTS Cptimeouts; Cptimeouts.ReadIntervalTimeout = MAXDWORD; Cptimeouts.ReadTotalTimeoutMultiplier = 0; Cptimeouts.ReadTotalTimeoutConstant = 0; Cptimeouts.WriteTotalTimeoutMultiplier = 0; Cptimeouts.WriteTotalTimeoutConstant = 0; if(!SetCommTimeouts(Cport[comport_number], &Cptimeouts)) { printf("unable to set comport time-out settings\n"); CloseHandle(Cport[comport_number]); return(1); } return(0); }
//---------------------------------------------------------------- bool Serial::setup(string portName, int baud) { close(); //--------------------------------------------- #if defined( KINSKI_MAC ) || defined( KINSKI_LINUX ) //--------------------------------------------- //lets account for the name being passed in instead of the device path if( portName.size() > 5 && portName.substr(0, 5) != "/dev/" ) { portName = "/dev/" + portName; } LOG_DEBUG << "opening " << portName << " @ " << baud << " bps"; m_handle = open(portName.c_str(), O_RDWR | O_NOCTTY | O_NONBLOCK); if(m_handle == -1) { LOG_ERROR << "unable to open " << portName; return false; } struct termios options; tcgetattr(m_handle, &m_old_options); options = m_old_options; switch(baud) { case 300: cfsetispeed(&options, B300); cfsetospeed(&options, B300); break; case 1200: cfsetispeed(&options, B1200); cfsetospeed(&options, B1200); break; case 2400: cfsetispeed(&options, B2400); cfsetospeed(&options, B2400); break; case 4800: cfsetispeed(&options, B4800); cfsetospeed(&options, B4800); break; case 9600: cfsetispeed(&options, B9600); cfsetospeed(&options, B9600); break; #ifndef KINSKI_LINUX case 14400: cfsetispeed(&options, B14400); cfsetospeed(&options, B14400); break; case 28800: cfsetispeed(&options, B28800); cfsetospeed(&options, B28800); break; #endif case 19200: cfsetispeed(&options, B19200); cfsetospeed(&options, B19200); break; case 38400: cfsetispeed(&options, B38400); cfsetospeed(&options, B38400); break; case 57600: cfsetispeed(&options, B57600); cfsetospeed(&options, B57600); break; case 115200: cfsetispeed(&options, B115200); cfsetospeed(&options, B115200); break; case 230400: cfsetispeed(&options, B230400); cfsetospeed(&options, B230400); break; default: cfsetispeed(&options, B9600); cfsetospeed(&options, B9600); LOG_ERROR << "setup(): cannot set " << baud << " bps, setting to 9600"; break; } options.c_cflag |= (CLOCAL | CREAD); options.c_cflag &= ~PARENB; options.c_cflag &= ~CSTOPB; options.c_cflag &= ~CSIZE; options.c_cflag |= CS8; tcsetattr(m_handle,TCSANOW,&options); bInited = true; LOG_DEBUG << "opened " << portName << "sucessfully @ " << baud << " bps"; return true; //--------------------------------------------- #endif //--------------------------------------------- //--------------------------------------------- #ifdef KINSKI_MSW //--------------------------------------------- char pn[sizeof(portName)]; int num; if (sscanf(portName.c_str(), "COM%d", &num) == 1) { // Microsoft KB115831 a.k.a if COM > COM9 you have to use a different // syntax sprintf(pn, "\\\\.\\COM%d", num); } else { strncpy(pn, (const char *)portName.c_str(), sizeof(portName)-1); } // open the serial port: // "COM4", etc... hComm=CreateFileA(pn,GENERIC_READ|GENERIC_WRITE,0,0, OPEN_EXISTING,0,0); if(hComm==INVALID_HANDLE_VALUE) { LOG_ERROR << "setup(): unable to open " << portName; return false; } // now try the settings: COMMCONFIG cfg; DWORD cfgSize; char buf[80]; cfgSize=sizeof(cfg); GetCommConfig(hComm,&cfg,&cfgSize); int bps = baud; sprintf(buf,"baud=%d parity=N data=8 stop=1",bps); #if (_MSC_VER) // microsoft visual studio // msvc doesn't like BuildCommDCB, //so we need to use this version: BuildCommDCBA if(!BuildCommDCBA(buf,&cfg.dcb)) { LOG_ERROR << "setup(): unable to build comm dcb, (" << buf << ")"; } #else if(!BuildCommDCB(buf,&cfg.dcb)) { LOG_ERROR << "setup(): unable to build comm dcb, (" << buf << ")"; } #endif // Set baudrate and bits etc. // Note that BuildCommDCB() clears XON/XOFF and hardware control by default if(!SetCommState(hComm,&cfg.dcb)) { LOG_ERROR << "setup(): couldn't set comm state: " << cfg.dcb.BaudRate << " bps, xio " << cfg.dcb.fInX << "/" << cfg.dcb.fOutX;; } //ofLogNotice("Serial") << "bps=" << cfg.dcb.BaudRate << ", xio=" << cfg.dcb.fInX << "/" << cfg.dcb.fOutX; // Set communication timeouts (NT) COMMTIMEOUTS tOut; GetCommTimeouts(hComm,&oldTimeout); tOut = oldTimeout; // Make timeout so that: // - return immediately with buffered characters tOut.ReadIntervalTimeout=MAXDWORD; tOut.ReadTotalTimeoutMultiplier=0; tOut.ReadTotalTimeoutConstant=0; SetCommTimeouts(hComm,&tOut); bInited = true; return true; //--------------------------------------------- #endif //--------------------------------------------- }
//---------------------------------------------------------------- bool ofSerial::setup(std::string portName, int baud){ std::cout << "setup serial" << "\r\n"; bInited = false; char pn[sizeof(portName)]; int num; if (sscanf(portName.c_str(), "COM%d", &num) == 1) { // Microsoft KB115831 a.k.a if COM > COM9 you have to use a different // syntax sprintf(pn, "\\\\.\\COM%d", num); } else { strncpy(pn, (const char *)portName.c_str(), sizeof(portName)-1); } // open the serial port: // "COM4", etc... hComm=CreateFileA(pn,GENERIC_READ|GENERIC_WRITE,0,0, OPEN_EXISTING,0,0); if(hComm==INVALID_HANDLE_VALUE){ std::cout << "setup(): unable to open " << portName << "\r\n"; return false; } // now try the settings: COMMCONFIG cfg; DWORD cfgSize; char buf[80]; cfgSize=sizeof(cfg); GetCommConfig(hComm,&cfg,&cfgSize); int bps = baud; sprintf(buf,"baud=%d parity=N data=8 stop=1",bps); #if (_MSC_VER) // microsoft visual studio // msvc doesn't like BuildCommDCB, //so we need to use this version: BuildCommDCBA if(!BuildCommDCBA(buf,&cfg.dcb)){ std::cout << "setup(): unable to build comm dcb, (" << buf << ")" << "\r\n"; } #else if(!BuildCommDCB(buf,&cfg.dcb)){ std::cout << "setup(): unable to build comm dcb, (" << buf << ")" << "\r\n"; } #endif // Set baudrate and bits etc. // Note that BuildCommDCB() clears XON/XOFF and hardware control by default if(!SetCommState(hComm,&cfg.dcb)){ std::cout << "setup(): couldn't set comm state: " << cfg.dcb.BaudRate << " bps, xio " << cfg.dcb.fInX << "/" << cfg.dcb.fOutX << "\r\n"; } //std::cout << "bps=" << cfg.dcb.BaudRate << ", xio=" << cfg.dcb.fInX << "/" << cfg.dcb.fOutX << "\r\n"; // Set communication timeouts (NT) COMMTIMEOUTS tOut; GetCommTimeouts(hComm,&oldTimeout); tOut = oldTimeout; // Make timeout so that: // - return immediately with buffered characters tOut.ReadIntervalTimeout=MAXDWORD; tOut.ReadTotalTimeoutMultiplier=0; tOut.ReadTotalTimeoutConstant=0; SetCommTimeouts(hComm,&tOut); bInited = true; return true; }
value setup(value a, value b) { const char* portName = val_string(a); int baud = val_int(b); //--------------------------------------------- #if defined( TARGET_OSX ) || defined( TARGET_LINUX ) //--------------------------------------------- int fd = open(portName, O_RDWR | O_NOCTTY | O_NONBLOCK); if(fd == -1){ return alloc_null(); } struct termios options; struct termios oldoptions; tcgetattr(fd,&oldoptions); options = oldoptions; switch(baud){ case 300: cfsetispeed(&options,B300); cfsetospeed(&options,B300); break; case 1200: cfsetispeed(&options,B1200); cfsetospeed(&options,B1200); break; case 2400: cfsetispeed(&options,B2400); cfsetospeed(&options,B2400); break; case 4800: cfsetispeed(&options,B4800); cfsetospeed(&options,B4800); break; case 9600: cfsetispeed(&options,B9600); cfsetospeed(&options,B9600); break; #ifdef TARGET_OSX case 14400: cfsetispeed(&options,B14400); cfsetospeed(&options,B14400); break; #endif case 19200: cfsetispeed(&options,B19200); cfsetospeed(&options,B19200); break; #ifdef TARGET_OSX case 28800: cfsetispeed(&options,B28800); cfsetospeed(&options,B28800); break; #endif case 38400: cfsetispeed(&options,B38400); cfsetospeed(&options,B38400); break; case 57600: cfsetispeed(&options,B57600); cfsetospeed(&options,B57600); break; case 115200: cfsetispeed(&options,B115200); cfsetospeed(&options,B115200); break; default: cfsetispeed(&options,B9600); cfsetospeed(&options,B9600); break; } options.c_cflag |= (CLOCAL | CREAD); options.c_cflag &= ~(PARENB | PARODD); options.c_cflag &= ~CSTOPB; options.c_cflag &= ~CRTSCTS; options.c_cflag &= ~CSIZE; options.c_cflag |= CS8; if (tcsetattr(fd,TCSANOW,&options) != 0) { return alloc_null(); } return alloc_int(fd); //--------------------------------------------- #endif //--------------------------------------------- //--------------------------------------------- #ifdef TARGET_WIN32 //--------------------------------------------- // open the serial port: // "COM4", etc... HANDLE hComm = CreateFile( portName, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL ); if(hComm==INVALID_HANDLE_VALUE){ return alloc_null(); } // now try the settings: COMMCONFIG cfg; DWORD cfgSize; char buf[80]; cfgSize=sizeof(cfg); GetCommConfig(hComm,&cfg,&cfgSize); int bps = baud; sprintf(buf,"baud=%d parity=N data=8 stop=1",bps); #if (_MSC_VER) // microsoft visual studio // msvc doesn't like BuildCommDCB, //so we need to use this version: BuildCommDCBA if(!BuildCommDCBA(buf,&cfg.dcb)){ //ofLog(OF_LOG_ERROR,"ofSerial: unable to build comm dcb; (%s)",buf); } #else if(!BuildCommDCB(buf,&cfg.dcb)){ //ofLog(OF_LOG_ERROR,"ofSerial: Can't build comm dcb; %s",buf); } #endif // Set baudrate and bits etc. // Note that BuildCommDCB() clears XON/XOFF and hardware control by default if(!SetCommState(hComm,&cfg.dcb)){ //ofLog(OF_LOG_ERROR,"ofSerial: Can't set comm state"); } //ofLog(OF_LOG_NOTICE,buf,"bps=%d, xio=%d/%d",cfg.dcb.BaudRate,cfg.dcb.fOutX,cfg.dcb.fInX); // Set communication timeouts (NT) COMMTIMEOUTS tOut; COMMTIMEOUTS oldTimeout; GetCommTimeouts(hComm,&oldTimeout); tOut = oldTimeout; // Make timeout so that: // - return immediately with buffered characters tOut.ReadIntervalTimeout=MAXDWORD; tOut.ReadTotalTimeoutMultiplier=0; tOut.ReadTotalTimeoutConstant=0; SetCommTimeouts(hComm,&tOut); return alloc_int((int)hComm); //--------------------------------------------- #endif //--------------------------------------------- }
int OpenComport(const char * comport_name, int baudrate) { char baudr[64], portname[64]; HANDLE handle; switch(baudrate) { case 110 : strcpy(baudr, "baud=110 data=8 parity=N stop=1"); break; case 300 : strcpy(baudr, "baud=300 data=8 parity=N stop=1"); break; case 600 : strcpy(baudr, "baud=600 data=8 parity=N stop=1"); break; case 1200 : strcpy(baudr, "baud=1200 data=8 parity=N stop=1"); break; case 2400 : strcpy(baudr, "baud=2400 data=8 parity=N stop=1"); break; case 4800 : strcpy(baudr, "baud=4800 data=8 parity=N stop=1"); break; case 9600 : strcpy(baudr, "baud=9600 data=8 parity=N stop=1"); break; case 19200 : strcpy(baudr, "baud=19200 data=8 parity=N stop=1"); break; case 38400 : strcpy(baudr, "baud=38400 data=8 parity=N stop=1"); break; case 57600 : strcpy(baudr, "baud=57600 data=8 parity=N stop=1"); break; case 115200 : strcpy(baudr, "baud=115200 data=8 parity=N stop=1"); break; case 128000 : strcpy(baudr, "baud=128000 data=8 parity=N stop=1"); break; case 256000 : strcpy(baudr, "baud=256000 data=8 parity=N stop=1"); break; case 460800 : strcpy(baudr, "baud=460800 data=8 parity=N stop=1"); break; default : printf("invalid baudrate\n"); return(-1); break; } strcpy(portname,"\\\\.\\"); strcat(portname,comport_name); handle = CreateFileA(portname, GENERIC_READ|GENERIC_WRITE, 0, /* no share */ NULL, /* no security */ OPEN_EXISTING, 0, /* no threads */ NULL); /* no templates */ if(handle==INVALID_HANDLE_VALUE) { printf("unable to open comport\n"); return(-1); } DCB port_settings; memset(&port_settings, 0, sizeof(port_settings)); /* clear the new struct */ port_settings.DCBlength = sizeof(port_settings); if(!BuildCommDCBA(baudr, &port_settings)) { printf("unable to set comport dcb settings\n"); CloseHandle(handle); return(-1); } if(!SetCommState(handle, &port_settings)) { printf("unable to set comport cfg settings\n"); CloseHandle(handle); return(-1); } COMMTIMEOUTS Cptimeouts; Cptimeouts.ReadIntervalTimeout = 0; Cptimeouts.ReadTotalTimeoutMultiplier = 0; Cptimeouts.ReadTotalTimeoutConstant = readTimeoutMs; Cptimeouts.WriteTotalTimeoutMultiplier = 50; Cptimeouts.WriteTotalTimeoutConstant = 50; if(!SetCommTimeouts(handle, &Cptimeouts)) { printf("unable to set comport time-out settings\n"); CloseHandle(handle); return(-1); } return( (int)handle); }
//---------------------------------------------------------------- bool ofSerial::setup(int deviceNumber, int baud){ int deviceCount = 0; string str = ""; string device = ""; bool deviceFound = false; //--------------------------------------------- #if defined( TARGET_OSX ) || defined( TARGET_LINUX ) //--------------------------------------------- //---------------------------------------------------- //We will find serial devices by listing the directory DIR *dir; struct dirent *entry; dir = opendir("/dev"); if (dir == NULL){ ofLog(OF_LOG_ERROR,"ofSerial: error listing devices in /dev"); } while ((entry = readdir(dir)) != NULL){ str = (char *)entry->d_name; #ifdef TARGET_OSX //if( str.substr(0,3) == "cu." || str.substr(0,4) == "tty." ){ if( str.find("cu.usbserial") == 0 || str.find("tty.usbserial") == 0 ){ #else if( str.substr(0,4) == "ttyS" || str.substr(0,6) == "ttyUSB" || str.substr(0,3) == "rfc" ){ #endif if(deviceCount == deviceNumber){ device = "/dev/"+str; deviceFound = true; ofLog(OF_LOG_NOTICE,"ofSerial device %i - /dev/%s <--selected", deviceCount, str.c_str()); }else ofLog(OF_LOG_NOTICE,"ofSerial device %i - /dev/%s", deviceCount, str.c_str()); deviceCount++; } } if(deviceFound){ return setup(device, baud); }else{ ofLog(OF_LOG_ERROR,"ofSerial: could not find device %i - only %i devices found", deviceNumber, deviceCount); return false; } //--------------------------------------------- #endif //--------------------------------------------- //--------------------------------------------- #ifdef TARGET_WIN32 //--------------------------------------------- enumerateWin32Ports(); if (deviceNumber < nPorts){ device = portNamesShort[deviceNumber]; deviceFound = true; } if(deviceFound){ return setup(device, baud); }else{ ofLog(OF_LOG_ERROR,"ofSerial: could not find device %i - only %i devices found", deviceNumber, nPorts); return false; } //--------------------------------------------- #endif //--------------------------------------------- } //---------------------------------------------------------------- bool ofSerial::setup(string portName, int baud){ bInited = false; //--------------------------------------------- #if defined( TARGET_OSX ) || defined( TARGET_LINUX ) //--------------------------------------------- ofLog(OF_LOG_NOTICE,"ofSerialInit: opening port %s @ %d bps", portName.c_str(), baud); fd = open(portName.c_str(), O_RDWR | O_NOCTTY | O_NONBLOCK); if(fd == -1){ ofLog(OF_LOG_ERROR,"ofSerial: unable to open port"); return false; } struct termios options; tcgetattr(fd,&oldoptions); options = oldoptions; switch(baud){ case 300: cfsetispeed(&options,B300); cfsetospeed(&options,B300); break; case 1200: cfsetispeed(&options,B1200); cfsetospeed(&options,B1200); break; case 2400: cfsetispeed(&options,B2400); cfsetospeed(&options,B2400); break; case 4800: cfsetispeed(&options,B4800); cfsetospeed(&options,B4800); break; case 9600: cfsetispeed(&options,B9600); cfsetospeed(&options,B9600); break; case 14400: cfsetispeed(&options,B14400); cfsetospeed(&options,B14400); break; case 19200: cfsetispeed(&options,B19200); cfsetospeed(&options,B19200); break; case 28800: cfsetispeed(&options,B28800); cfsetospeed(&options,B28800); break; case 38400: cfsetispeed(&options,B38400); cfsetospeed(&options,B38400); break; case 57600: cfsetispeed(&options,B57600); cfsetospeed(&options,B57600); break; case 115200: cfsetispeed(&options,B115200); cfsetospeed(&options,B115200); break; default: cfsetispeed(&options,B9600); cfsetospeed(&options,B9600); ofLog(OF_LOG_ERROR,"ofSerialInit: cannot set %i baud setting baud to 9600\n", baud); break; } options.c_cflag |= (CLOCAL | CREAD); options.c_cflag &= ~PARENB; options.c_cflag &= ~CSTOPB; options.c_cflag &= ~CSIZE; options.c_cflag |= CS8; tcsetattr(fd,TCSANOW,&options); bInited = true; ofLog(OF_LOG_NOTICE,"sucess in opening serial connection"); return true; //--------------------------------------------- #endif //--------------------------------------------- //--------------------------------------------- #ifdef TARGET_WIN32 //--------------------------------------------- // open the serial port: // "COM4", etc... hComm=CreateFileA(portName.c_str(),GENERIC_READ|GENERIC_WRITE,0,0, OPEN_EXISTING,0,0); if(hComm==INVALID_HANDLE_VALUE){ ofLog(OF_LOG_ERROR,"ofSerial: unable to open port"); return false; } // now try the settings: COMMCONFIG cfg; DWORD cfgSize; char buf[80]; cfgSize=sizeof(cfg); GetCommConfig(hComm,&cfg,&cfgSize); int bps = baud; sprintf(buf,"baud=%d parity=N data=8 stop=1",bps); #if (_MSC_VER) // microsoft visual studio // msvc doesn't like BuildCommDCB, //so we need to use this version: BuildCommDCBA if(!BuildCommDCBA(buf,&cfg.dcb)){ ofLog(OF_LOG_ERROR,"ofSerial: unable to build comm dcb; (%s)",buf); } #else if(!BuildCommDCB(buf,&cfg.dcb)){ ofLog(OF_LOG_ERROR,"ofSerial: Can't build comm dcb; %s",buf); } #endif // Set baudrate and bits etc. // Note that BuildCommDCB() clears XON/XOFF and hardware control by default if(!SetCommState(hComm,&cfg.dcb)){ ofLog(OF_LOG_ERROR,"ofSerial: Can't set comm state"); } //ofLog(OF_LOG_NOTICE,buf,"bps=%d, xio=%d/%d",cfg.dcb.BaudRate,cfg.dcb.fOutX,cfg.dcb.fInX); // Set communication timeouts (NT) COMMTIMEOUTS tOut; GetCommTimeouts(hComm,&oldTimeout); tOut = oldTimeout; // Make timeout so that: // - return immediately with buffered characters tOut.ReadIntervalTimeout=MAXDWORD; tOut.ReadTotalTimeoutMultiplier=0; tOut.ReadTotalTimeoutConstant=0; SetCommTimeouts(hComm,&tOut); bInited = true; return true; //--------------------------------------------- #endif //--------------------------------------------- }
// // Initialize the port. This can be port 1 to 4. // BOOL CSerialPort::InitPort(CWnd* pPortOwner, // the owner (CWnd) of the port (receives message) UINT portnr, // portnumber (1..4) UINT baud, // baudrate char parity, // parity UINT databits, // databits UINT stopbits, // stopbits DWORD dwCommEvents, // EV_RXCHAR, EV_CTS etc UINT writebuffersize) // size to the writebuffer { assert(portnr > 0 && portnr < 255); assert(NULL != pPortOwner); // if the thread is alive: Kill UninitPort(); // create events if (m_ov.hEvent != NULL) ResetEvent(m_ov.hEvent); m_ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); if (m_hWriteEvent != NULL) ResetEvent(m_hWriteEvent); m_hWriteEvent = CreateEvent(NULL, TRUE, FALSE, NULL); if (m_hShutdownEvent != NULL) ResetEvent(m_hShutdownEvent); m_hShutdownEvent = CreateEvent(NULL, TRUE, FALSE, NULL); // initialize the event objects m_hEventArray[0] = m_hShutdownEvent; // highest priority m_hEventArray[1] = m_ov.hEvent; m_hEventArray[2] = m_hWriteEvent; // initialize critical section InitializeCriticalSection(&m_csCommunicationSync); // set buffersize for writing and save the owner m_pOwner = pPortOwner; if (m_szWriteBuffer != NULL) delete [] m_szWriteBuffer; m_szWriteBuffer = new char[writebuffersize]; m_nPortNr = portnr; m_nWriteBufferSize = writebuffersize; m_dwCommEvents = dwCommEvents; BOOL bResult = FALSE; char *szPort = new char[50]; char *szBaud = new char[50]; // now it critical! EnterCriticalSection(&m_csCommunicationSync); // if the port is already opened: close it if (INVALID_HANDLE_VALUE != m_hComm) { CloseHandle(m_hComm); m_hComm = INVALID_HANDLE_VALUE; } // prepare port strings sprintf_s(szPort, 50, "\\\\.\\COM%d", portnr); sprintf_s(szBaud, 50, "baud=%d parity=%c data=%d stop=%d", baud, parity, databits, stopbits); // get a handle to the port m_hComm = CreateFileA(szPort, // communication port string (COMX) GENERIC_READ | GENERIC_WRITE, // read/write types 0, // comm devices must be opened with exclusive access NULL, // no security attributes OPEN_EXISTING, // comm devices must use OPEN_EXISTING FILE_FLAG_OVERLAPPED, // Async I/O 0); // template must be 0 for comm devices if (m_hComm == INVALID_HANDLE_VALUE) { // port not found delete [] szPort; delete [] szBaud; return FALSE; } // set the timeout values m_CommTimeouts.ReadIntervalTimeout = 1000; m_CommTimeouts.ReadTotalTimeoutMultiplier = 1000; m_CommTimeouts.ReadTotalTimeoutConstant = 1000; m_CommTimeouts.WriteTotalTimeoutMultiplier = 1000; m_CommTimeouts.WriteTotalTimeoutConstant = 1000; // configure if (SetCommTimeouts(m_hComm, &m_CommTimeouts)) { if (SetCommMask(m_hComm, dwCommEvents)) { if (GetCommState(m_hComm, &m_dcb)) { m_dcb.fRtsControl = RTS_CONTROL_ENABLE; // set RTS bit high! if (BuildCommDCBA(szBaud, &m_dcb)) { if (SetCommState(m_hComm, &m_dcb)) ; // normal operation... continue else ProcessErrorMessage("SetCommState()"); } else ProcessErrorMessage("BuildCommDCB()"); } else ProcessErrorMessage("GetCommState()"); } else ProcessErrorMessage("SetCommMask()"); } else ProcessErrorMessage("SetCommTimeouts()"); delete [] szPort; delete [] szBaud; // flush the port PurgeComm(m_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT); // release critical section LeaveCriticalSection(&m_csCommunicationSync); TRACE("Initialisation for communicationport %d completed.\nUse Startmonitor to communicate.\n", portnr); return TRUE; }