/************************************************************************** * -Name: win_InitSerial ( port, settings ) * * -Description: This routine initializes the communications port. * * -Arguments: GIZ_PORT porttoopen - communications port to be used * GIZ_PORT_COM1 - for communications port 1 * GIZ_PORT_COM2 - for communications port 2 * short portsettings - communications port parameters * modes are OR'd together to achieve comm port settings. * Default: GIZBAUD_9600 | GIZNONE | GIZONESTOPBIT | GIZEIGHTBITS. * * baud rates : GIZBAUD_300, GIZBAUD_600, GIZBAUD_1200, * GIZBAUD_2400, GIZBAUD_4800, GIZBAUD_9600 * * -Notes: Gizzmo must have comm port settings of no parity (GIZNONE), * 1 stop bit (GIZONESTOPBITS), and 8 data bits (GIZEIGHTBITS). * The baud rate is the only settings that can currently * be varied. To do this, please review the programmers' * guide on changing the baud rate. * * -Returns : 0 if comm port setup okay *************************************************************************/ long win_InitSerial ( int port ) { long err; DWORD start,current; char aBuf[16]; DWORD bytesRead; if((err = setup_dcb( (GIZ_PORT)(port-1), 0, (short)LINES_LOW))!=0) return err; SISetLinesLow(); while ((current-start) < PowerOnTimeDelay) current = GetTickCount(); // PowerOnTime delay before launch to let calibrator wake up and stabilize /* create timeout data structure */ GetCommTimeouts(PortID,&TimeOutData); TimeOutData.ReadIntervalTimeout = 2500;// millisecs between characters TimeOutData.ReadTotalTimeoutMultiplier = 250; // millisec multiplier by number of characters TimeOutData.ReadTotalTimeoutConstant = 2500; // 2.5 sec + nchar * 250 msec total time out(about 3secs); TimeOutData.WriteTotalTimeoutMultiplier = 250; TimeOutData.WriteTotalTimeoutConstant = 250; SetCommTimeouts(PortID,&TimeOutData); start = GetTickCount(); current = GetTickCount(); SISetLinesHigh(); while ((current-start) < PowerOnTimeDelay) current = GetTickCount(); // PowerOnTime delay before launch to let calibrator wake up and stabilize ReadFile( PortID, // handle of file to read aBuf, // address of buffer that receives data 1, // number of bytes to read: the count byte and the command byte &bytesRead, // address of number of bytes read NULL // address of structure for data ); return (err); }
__bool CSerialPort::open() { char buf[64]; DCB dcb; COMMTIMEOUTS tout; if(INVALID_HANDLE_VALUE != m_hFile){ return __true; } sprintf(buf, "\\\\.\\COM%d", m_iPortNo); m_hFile = CreateFile( buf, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0 ); utils_trace( "COM %d opened as 0x%08x\n", m_iPortNo, m_hFile ); if(INVALID_HANDLE_VALUE==m_hFile){ return __false; } get_config_string(); if( !setup_dcb(m_Setting, &dcb) ){ close(); utils_trace( "COM%d, error in setting string : %s, code %d\n", m_iPortNo, m_Setting, GetLastError() ); return __false; } if( !SetCommState(m_hFile, &dcb) ){ utils_error( "COM%d, SetCommState failed with %d.\n", m_iPortNo, GetLastError() ); close(); } utils_trace( "Ok, COM%d setting applied : '%s'\n", m_iPortNo, m_Setting ); if( !GetCommTimeouts(m_hFile, &tout) ){ close(); utils_trace( "COM%d, Error in GetCommTimeouts, Code %d.\n", m_iPortNo, GetLastError() ); return __false; } tout.ReadIntervalTimeout = MAXDWORD; tout.ReadTotalTimeoutMultiplier = 0; tout.ReadTotalTimeoutConstant = 0; tout.WriteTotalTimeoutMultiplier = 10; tout.WriteTotalTimeoutConstant = 0; if(!SetCommTimeouts(m_hFile, &tout)){ utils_trace( "COM%d, Error in SetCommTimeouts, Code %d.\n", m_iPortNo, GetLastError() ); close(); return __false; } m_OverlappedEvent = new CEvent(0, TRUE, FALSE); if(!m_OverlappedEvent || !m_OverlappedEvent->Handle()){ utils_trace( "COM%d, error %d creating overlapped event.\n", m_iPortNo, GetLastError() ); close(); return __false; } if( !PurgeComm( m_hFile, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR ) ){ utils_error( "COM%d, Warning : PurgeComm failed with %d\n", m_iPortNo, GetLastError() ); } COMMCONFIG cfg; DWORD size; cfg.dwSize = size = sizeof(cfg); if( !GetCommConfig(m_hFile, &cfg, &size) ){ utils_error( "Warning : GetCommConfig failed with %d\n", GetLastError() ); }else{ utils_trace( "COM%d setting is : '%d,%c,%d,%d(%08x)'\n", m_iPortNo, cfg.dcb.BaudRate, Parity(&cfg.dcb), cfg.dcb.ByteSize, Stopbits(cfg.dcb.StopBits), *((DWORD*)&cfg.dcb + 2) ); } return __true; }