void openPort(QString port) { #ifndef Q_OS_WIN32 int tries=MAX_RETRIES; int i; //repeatedly open the port until there are no errors QByteArray ba = port.toLocal8Bit(); for(i=0;i<tries;i++) { if (ser_open(ba.data(), 38400, &portFD) != -1) { break; } else { std::cout << "Error opening serial port\n"; SLEEP(250); } } if (i==tries) { exit(1); } //reconfigure the device for Mac _portName = port; //std::cout << "Reconfiguring device\n"; ser_setspeed(&portFD, 38400); #else _portName = port; _port = new QextSerialPort(port); _port->setBaudRate(BAUD9600); _port->setTimeout(1000); if (!_port->open(QextSerialPort::ReadWrite)) error("Could not open serial port."); #endif }
int ser_open(char * port, union pinfo pinfo, union filedescriptor *fdp) { int rc; int fd; /* * open the serial port */ fd = open(port, O_RDWR | O_NOCTTY | O_NONBLOCK); if (fd < 0) { printf("ser_open(): can't open device \"%s\": %s\n", port, strerror(errno)); return -1; } fdp->ifd = fd; /* * set serial line attributes */ rc = ser_setspeed(fdp, pinfo.baud); if (rc) { printf("%s: ser_open(): can't set attributes for device \"%s\": %s\n", port, strerror(-rc)); close(fd); return -1; } return 0; }
static int ser_open(char * port, long baud, union filedescriptor *fdp) { int rc; int fd; /* * If the port is of the form "net:<host>:<port>", then * handle it as a TCP connection to a terminal server. */ if (strncmp(port, "net:", strlen("net:")) == 0) { return net_open(port + strlen("net:"), fdp); } /* * open the serial port */ fd = open(port, O_RDWR | O_NOCTTY | O_NONBLOCK); if (fd < 0) { fprintf(stderr, "%s: ser_open(): can't open device \"%s\": %s\n", progname, port, strerror(errno)); return -1; } fdp->ifd = fd; /* * set serial line attributes */ rc = ser_setspeed(fdp, baud); if (rc) { fprintf(stderr, "%s: ser_open(): can't set attributes for device \"%s\": %s\n", progname, port, strerror(-rc)); close(fd); return -1; } return 0; }
static int ser_open(char * port, union pinfo pinfo, union filedescriptor *fdp) { LPVOID lpMsgBuf; HANDLE hComPort=INVALID_HANDLE_VALUE; char *newname = 0; /* * If the port is of the form "net:<host>:<port>", then * handle it as a TCP connection to a terminal server. */ if (strncmp(port, "net:", strlen("net:")) == 0) { #ifdef HAVE_LIBWS2_32 return net_open(port + strlen("net:"), fdp); #else avrdude_message(MSG_INFO, "%s: ser_open(): " "not configured for TCP connections\n", progname); return -1; #endif } if (strncasecmp(port, "com", strlen("com")) == 0) { // prepend "\\\\.\\" to name, required for port # >= 10 newname = malloc(strlen("\\\\.\\") + strlen(port) + 1); if (newname == 0) { avrdude_message(MSG_INFO, "%s: ser_open(): out of memory\n", progname); exit(1); } strcpy(newname, "\\\\.\\"); strcat(newname, port); port = newname; } hComPort = CreateFile(port, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hComPort == INVALID_HANDLE_VALUE) { FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language (LPTSTR) &lpMsgBuf, 0, NULL); avrdude_message(MSG_INFO, "%s: ser_open(): can't open device \"%s\": %s\n", progname, port, (char*)lpMsgBuf); LocalFree( lpMsgBuf ); return -1; } if (!SetupComm(hComPort, W32SERBUFSIZE, W32SERBUFSIZE)) { CloseHandle(hComPort); avrdude_message(MSG_INFO, "%s: ser_open(): can't set buffers for \"%s\"\n", progname, port); return -1; } fdp->pfd = (void *)hComPort; if (ser_setspeed(fdp, pinfo.baud) != 0) { CloseHandle(hComPort); avrdude_message(MSG_INFO, "%s: ser_open(): can't set com-state for \"%s\"\n", progname, port); return -1; } if (!serial_w32SetTimeOut(hComPort,0)) { CloseHandle(hComPort); avrdude_message(MSG_INFO, "%s: ser_open(): can't set initial timeout for \"%s\"\n", progname, port); return -1; } if (newname != 0) { free(newname); } return 0; }