/* Function: rtIOStreamOpen ================================================= * Abstract: * Open the connection with the target. */ int rtIOStreamOpen(int argc, void * argv[]) { unsigned int comNum = 0; int result = RTIOSTREAM_NO_ERROR; int streamID; HANDLE serialHandle; int closeFile = false; SerialCommsData *sd; initSerialCommsDataStructure(); for (streamID = 0; streamID < N_SERIAL_PORTS; streamID++) { if (SerialData[streamID].port[0] == 0) { break; } } if (streamID == N_SERIAL_PORTS) { printf( "All %d elements of the SerialCommsData structure are already in use.", N_SERIAL_PORTS ); return RTIOSTREAM_ERROR; } sd = &SerialData[streamID]; sd->baud = BAUD_UNINITIALIZED; processArgs(argc, argv, sd); if (sd->baud == BAUD_UNINITIALIZED) { printf( "A valid bit rate must be specified via the -baud option."); sd->port[0] = 0; return RTIOSTREAM_ERROR; } initDCB( sd->baud ); serialHandle = (void *) CreateFile( sd->port, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (serialHandle == INVALID_HANDLE_VALUE) { DWORD err = GetLastError( ); printf( "CreateFile failed, error %d or 0x%08x. One possible " "cause is that COM ports COM10 or greater must be " "identified by a fully qualified name, e.g. " "\\\\.\\COM10.\n", err, err); sd->port[0] = 0; streamID = RTIOSTREAM_ERROR; goto EXIT_POINT; } sd->serialHandle = serialHandle; if (!SetCommTimeouts(serialHandle, &cto_blocking)) { printf( "SetCommTimeouts failed\n"); streamID = RTIOSTREAM_ERROR; closeFile = true; goto EXIT_POINT; } if (!SetCommState(serialHandle, &dcb)) { printf( "SetCommState failed\n"); streamID = RTIOSTREAM_ERROR; closeFile = true; goto EXIT_POINT; } result = serialDataFlush( sd); /* clear out old data on the port */ if (result == RTIOSTREAM_ERROR) { printf( "serialDataFlush failed\n"); streamID = RTIOSTREAM_ERROR; closeFile = true; goto EXIT_POINT; } EXIT_POINT: if (closeFile) { CloseHandle(serialHandle); sd->port[0] = 0; } return streamID; }
static int serialPortOpenInitialize(SerialCommsData* sd, const int streamID,int argc, void* argv[]) { int errorCode = RTIOSTREAM_NO_ERROR; #ifdef _WIN32 HANDLE serialHandle; #else int serialHandle; #endif int closeFile = false; int result; sd->baud = BAUD_DEFAULT_VAL; strcpy(sd->port, PORTNAME_DEFAULT_VAL); sd->verbosity = DEFAULT_VERBOSITY; result = processArgs(argc, argv, sd); if (result == RTIOSTREAM_ERROR) { printf( "processArgs failed\n"); errorCode = RTIOSTREAM_ERROR; goto EXIT_POINT; } if (sd->verbosity) { printf("rtIOStreamOpen (connection id %d): port %s, baud %lu\n", streamID, sd->port, (unsigned long) sd->baud); } #ifdef _WIN32 initDCB( sd->baud ); serialHandle = (void *) CreateFile( sd->port, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (serialHandle == INVALID_HANDLE_VALUE) { DWORD err = GetLastError( ); printf( "Open serial port failed, error %d or 0x%08x.", err, err); #else/*UNIX*/ initTermios(sd->serialHandle, sd->baud); serialHandle = open(sd->port, O_RDWR /*Enable Read and Write Operations*/ | O_NOCTTY /*Not controlling flag specified*/ | O_NONBLOCK); /*Make the port non -blocking*/ if (serialHandle == -1) { /*-1 returned on error by OPEN*/ printf("Open serial port:%s failed, error %d: %s", sd->port,errno,strerror(errno)); #endif printf( "Possible causes " "are:\n a) The target is not powered on. \n " "b) The specified port number is not correct. \n " "c) The specified bit rate is not " "supported by the host machine.\n d) The previous " "connection was not closed correctly. " "If this is the case, you may need to re-start MATLAB.\n"); errorCode = RTIOSTREAM_ERROR; goto EXIT_POINT; } sd->serialHandle = serialHandle; #ifdef _WIN32 if (!SetCommTimeouts(serialHandle, &cto_timeout)) { printf( "SetCommTimeouts failed\n"); errorCode = RTIOSTREAM_ERROR; closeFile = true; goto EXIT_POINT; } if (!SetCommState(serialHandle, &dcb)) { printf( "SetCommState failed\n"); errorCode = RTIOSTREAM_ERROR; closeFile = true; goto EXIT_POINT; } #endif /*UNIX has this done with setting of termios data structure in initTermios*/ result = serialDataFlush( sd); /* clear out old data on the port */ if (result == RTIOSTREAM_ERROR) { printf( "serialDataFlush failed\n"); errorCode = RTIOSTREAM_ERROR; closeFile = true; goto EXIT_POINT; } EXIT_POINT: if (closeFile) { #ifdef _WIN32 CloseHandle(serialHandle); #else close(serialHandle); #endif } return errorCode; } /* Function: getSerialData ================================================= * Abstract: * Retrieves a SerialCommsData instance given its streamID. * * NOTE: An invalid streamID will lead to a NULL pointer being returned */ static SerialCommsData * getSerialData(int streamID) { /* return NULL for invalid or uninitialized streamIDs */ SerialCommsData * sd = NULL; if ((streamID >= 0) && (streamID < N_SERIAL_PORTS)) { if (SerialData[streamID].isInUse) { sd = &SerialData[streamID]; } } return sd; }