Subj : Win32 RS-232 help To : borland.public.cpp.borlandcpp From : Jeff Baker Date : Fri Apr 30 2004 01:30 am Well I've gotten a lot further then my last post but I'm still having a problem. I have a device connected to COM1. The device uses 3-wire rs-232 (9600,8,0,1). I know that I'm sending the send_stream[] correctly because I can see the device respond using an oscilliscope and if send_stream[] was not properly formatted or received by the device correctly then it would not respond at all. That leads me to belive that I'm setting up the port and initializing it correctly. The problem is that I'm expecting to see the device return 28 bytes and I'm only receiving 6 (without exception) and I can tell theat the device is sending quite a bit more then 6 bytes just by looking at the length of the data on the oscilliscope. So I started thinking that the software was timing out before the entire string could be read but it looks like I've set the DCB correctly. Does anyone notice a programming error is the source below? I thank you in advance. #include #include #include void ComRoutine(); class CComPort { public: char *number; HANDLE handle; UINT received[128]; DCB dcb; COMMTIMEOUTS tout; void OpenPort(); CComPort(); ~CComPort(); }Com; CComPort::CComPort() { } CComPort::~CComPort() { } int main(int argc, char *argv[]) { BOOL program_complete = false; INT x,kbin; for (x = 0; x < argc; x++) { if (strcmpi("COM2",argv[x]) == 0) Com.number = "COM2"; else if (strcmpi("COM3",argv[x]) == 0) Com.number = "COM3"; else if (strcmpi("COM4",argv[x]) == 0) Com.number = "COM4"; else Com.number = "COM1"; } textmode(C4350); textbackground(BLUE); clrscr(); Com.OpenPort(); while (program_complete == false) { if (kbhit()) { kbin = getch(); if (kbin == 0) kbin = getch(); if (kbin == 27) program_complete = true; else ComRoutine(); } } if (! CloseHandle(Com.handle)) exit(EXIT_FAILURE); exit(EXIT_SUCCESS); } void ComRoutine() { UINT x,send_stream[]={4,0,0,0,4,0}; DWORD to_write = 6; DWORD written = 0; DWORD to_read = 28; DWORD read = 0; gotoxy(1,8); if ((! WriteFile(Com.handle,send_stream,to_write,&written,NULL)) || (to_write != written)) { textcolor(LIGHTRED); if (to_write != written) cprintf("Unable to write entire stream to %s (TIME_OUT)!\r\n",Com.number); else cprintf("Unable to write to %s!\r\n",Com.number); } else { textcolor(LIGHTGREEN); cprintf("%s was written to successfully.\r\n",Com.number); } gotoxy(1,9); if ((! ReadFile(Com.handle,Com.received,to_read,&read,NULL)) || (to_read != read)) { textcolor(LIGHTRED); if (to_read != read) cprintf("Received %d bytes from %s, we expected %d (TIME_OUT)!\r\n",read,Com.number,to_read); else cprintf("Unable to read from %s!\r\n",Com.number); } gotoxy(1,10); cprintf("REC: "); for(x = 0; x < to_read; x++) { cprintf("%X ",Com.received[x]); clreol(); } } void CComPort::OpenPort() { DWORD dwInQueue = 1024; // size of input buffer DWORD dwOutQueue = 1024; // size of output buffer /* CREATE A HANDLE TO THE COMPORT */ gotoxy(1,1); handle = CreateFile(number,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL); if (handle == INVALID_HANDLE_VALUE) { textcolor(LIGHTRED); cprintf("Unable to create a valid comport handle!\r\n"); } else { textcolor(LIGHTGREEN); cprintf("Handle to %s created successfully.\r\n",number); } /* SET COMPORT BUFFER SIZE */ gotoxy(1,2); if (! SetupComm(handle,dwInQueue,dwOutQueue)) { textcolor(LIGHTRED); cprintf("Unable to create a %s buffers!\r\n",number); } else { textcolor(LIGHTGREEN); cprintf("%s input/Output buffers created successfully.\r\n",number); } /* READ CURRENT STATE OF COMPORT */ gotoxy(1,3); if (! GetCommState(handle,&dcb)) { textcolor(LIGHTRED); cprintf("Error reading comport state!\r\n"); } else { textcolor(LIGHTGREEN); cprintf("Serial port %s is ready.\r\n",number); } /* SET COMPORT CONFIGURATION */ gotoxy(1,4); dcb.BaudRate = CBR_9600; // current baud rate dcb.ByteSize = 8; // number of bits/byte, 4-8 dcb.Parity = NOPARITY; // 0-4=no,odd,even,mark,space dcb.StopBits = ONESTOPBIT; // 0,1,2 = 1, 1.5, 2 if (! SetCommState(handle,&dcb)) { textcolor(LIGHTRED); cprintf("Unable to set comport control!\r\n"); } else { textcolor(LIGHTGREEN); cprintf("Serial port %s control set successfully.\r\n",number); } /* PURGE COMPORT BUFFERS */ gotoxy(1,5); if (! PurgeComm(handle,PURGE_TXCLEAR | PURGE_RXCLEAR)) { textcolor(LIGHTRED); cprintf("Unable to purge comport buffers!\r\n"); } else { textcolor(LIGHTGREEN); cprintf("Serial port %s buffers purged successfully.\r\n",number); } /* SET COMPORT TIMEOUT CONDITIONS */ gotoxy(1,6); if (! GetCommTimeouts(handle,&tout)) { textcolor(LIGHTRED); cprintf("Unable to read %s time out parameters!\r\n",Com.number); } else { textcolor(LIGHTGREEN); cprintf("%s time out parameters were successfully read.\r\n",Com.number); } tout.ReadIntervalTimeout =160; tout.ReadTotalTimeoutMultiplier = 80; tout.ReadTotalTimeoutConstant = 2000; tout.WriteTotalTimeoutMultiplier = 80; tout.WriteTotalTimeoutConstant = 2000; gotoxy(1,7); if (! SetCommTimeouts(Com.handle,&tout)) { textcolor(LIGHTRED); cprintf("Unable to set %s time out parameter!\r\n",Com.number); } else { textcolor(LIGHTGREEN); cprintf("%s time out parameters were set successfully.\r\n",Com.number); } } .