Subj : rs232 access serialport To : borland.public.cpp.borlandcpp From : Dj. Bouma Date : Wed Sep 22 2004 12:20 am Hello all, can anyone help me out i'm new to this and cant seem to get the serialport working. if i start with CSerialComm.OpenSerialPort() , and it comes to CreateFile(...) m_hCom gets a value of 960 every time. the second cout gives me the value of 6 wich is imho an invalid handle value. i took out some code in order not to create a to big message. My question is what am i doing wrong ? why isn't it getting a propper handle ? [headerfile] #include #include #include // only needed for cout class CSerialComm { public: bool m_ComIsOpen; // is comport already open bool m_bPortReady; // is port after initialisation ready HANDLE m_hCom; // handle to commport DCB PortDCB; // dcb objects to change DCB OldDcb; // origional DCB settings to restore COMMTIMEOUTS m_CommTimeouts; char sBuffer[128]; char cPortName[5]; //comportname eg. COMxx CSerialComm(); virtual ~CSerialComm(); TCommPort(); bool OpenSerialPort(); bool CloseSerialPort(); bool Write(char *buffer); bool Read(char *buffer, unsigned int ByteCount); private: bool RestoreDcb(); bool ReadConfig(); }; #endif [cpp file] #include "Tcomm.h" #include "defenitions.h" CSerialComm::CSerialComm() :m_ComIsOpen(false), // m_CommPort("COM1"), m_hCom(0) { } CSerialComm::~CSerialComm() { CloseHandle(m_hCom); } bool CSerialComm::OpenSerialPort() { strcpy(cPortName,"COM1"); m_hCom = CreateFile ( cPortName,GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); printf( "hCom= : %d\n " , m_hCom ); <======================= h_mCom gets a value of 960 every time cout << "after handle: 1 :" << GetLastError() << endl; <============= lasterror = 0 // if fail to open the port, return FALSE. if ( m_hCom == INVALID_HANDLE_VALUE ) { // Could not open the port. cout << "could not get propper handle: " << GetLastError() << endl; return FALSE; } else cout << "after if invalid handle: " << GetLastError() << endl; <===== lasterror = 6 PortDCB.DCBlength = sizeof(DCB); cout << "sizeofdcb: " << GetLastError() << endl; <===== lasterror = 6 /* Get the default port setting information.*/ if ( !GetCommState ( m_hCom, &PortDCB) ); { /* got problems closeport */ CloseSerialPort(); } /* start populating object dcb */ [ cut out ] /* Configure the port according to the specs of the DCB structure.*/ if( !SetCommState( m_hCom,&PortDCB ) ); { /* commstate could not be set */ CloseSerialPort(); } /* we succeeded */ m_ComIsOpen = TRUE ; return TRUE; } .