/** * throws EXIT_FAILURE if could not open the port */ int Serial_Port:: open_serial() { // -------------------------------------------------------------------------- // OPEN PORT // -------------------------------------------------------------------------- //printf("OPEN PORT\n"); fd = _open_port(uart_name); // Check success if (fd == -1) { //printf("failure, could not open port.\n"); return -1; //throw EXIT_FAILURE; } // -------------------------------------------------------------------------- // SETUP PORT // -------------------------------------------------------------------------- bool success = _setup_port(baudrate, 8, 1, false, false); // -------------------------------------------------------------------------- // CHECK STATUS // -------------------------------------------------------------------------- if (!success) { printf("failure, could not configure port.\n"); //throw EXIT_FAILURE; return -1; } if (fd <= 0) { printf("Connection attempt to port %s with %d baud, 8N1 failed, exiting.\n", uart_name, baudrate); //throw EXIT_FAILURE; return -1; } // -------------------------------------------------------------------------- // CONNECTED! // -------------------------------------------------------------------------- printf("Connected to %s with %d baud, 8 data bits, no parity, 1 stop bit (8N1)\n", uart_name, baudrate); lastStatus.packet_rx_drop_count = 0; status = true; printf("\n"); return 0; }
void Serial_Port::open_serial() { // -------------------------------------------------------------------------- // OPEN PORT // -------------------------------------------------------------------------- printf("OPEN SERIAL PORT\n"); hComm = _open_port(LPportname); if (hComm == INVALID_HANDLE_VALUE) { CloseHandle(hComm); printf("Can not open serial port %s.\n",LPportname); } else { SetupComm(hComm,10000,10000); PurgeComm(hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT); // before read or write serial£¬we should use PurgeComm() to clean the buffer //PURGE_TXABORT break off all write operation and return immediately,even if the write operation has not finished //PURGE_RXABORT break off all read operation and return immediately,even if the read operation has not finished //PURGE_TXCLEAR clean the output buffer //PURGE_RXCLEAR clean the input buffer memset(&m_ov,0,sizeof(OVERLAPPED)); printf("Open comport success\n"); } BOOL dcbSuccess = _setupdcb(baudrate); if (!dcbSuccess) printf("Could not configure serial port.\n"); else printf("configure serial port success.\n"); BOOL timeoutSuccess = _setuptimeout(0,0,0,0,0); if (!timeoutSuccess) printf("Could not configure setuptimeout.\n"); else printf("Configure setuptimeout success.\n"); if (dcbSuccess && timeoutSuccess) { printf("Connected to %s with %d baud, 8 data bits, no parity, 1 stop bit (8N1)\n", LPportname, baudrate); system("pause"); lastStatus.packet_rx_drop_count = 0; status = 1; printf("\n"); } return; }
void open_serial() { printf("INITIALIZE \n"); initialize_defaults(); // -------------------------------------------------------------------------- // OPEN PORT // -------------------------------------------------------------------------- printf("OPEN PORT\n"); fd = _open_port(uart_name); // Check success if (fd == -1) { printf("failure, could not open port.\n"); } // -------------------------------------------------------------------------- // SETUP PORT // -------------------------------------------------------------------------- int success = _setup_port(baudrate, 8, 1, 0,0); // -------------------------------------------------------------------------- // CHECK STATUS // -------------------------------------------------------------------------- if (!success) { printf("failure, could not configure port.\n"); } if (fd <= 0) { printf("Connection attempt to port %s with %d baud, 8N1 failed, exiting.\n", uart_name, baudrate); } // -------------------------------------------------------------------------- // CONNECTED! // -------------------------------------------------------------------------- printf("Connected to %s with %d baud, 8 data bits, no parity, 1 stop bit (8N1)\n", uart_name, baudrate); lastStatus.packet_rx_drop_count = 0; status = 1; printf("\n"); return; }