c - Serial port, WriteFile affects ReadFile -
i'm having problem serial port code.
i do:
opencomm(); send(); closecomm(); and clearcommerror() (inside recv()) returns in comstat.cbinque same amount sent.
so, if sizeof (sendbuff) 100, 100 in comstat.cbinque.
after reading 1 byte readfile, comstat.cbinque decrements (after subsequent clearcommerror(), of course).
the values read not ones written. there no device connected port.
the strangest thing this code used work, not anymore.
word sendbuff[128]; static handle hcomm; static void opencomm (void) { static commtimeouts timeouts = {0,0,0,0,0}; static dcb dcb = { sizeof (dcb), // dcblength 115200, // * baudrate 1, // fbinary 0, // * fparity 0, // foutxctsflow 0, // foutxdsrflow 0, // fdtrcontrol 0, // fdsrsensitivity 1, // ftxcontinueonxoff 0, // foutx 0, // finx 0, // ferrorchar 0, // fnull 0, // frtscontrol 0, // fabortonerror 0, // fdummy2 0, // wreserved 8*k, // xonlim 2*k, // xofflim 8, // * bytesize 0, // * parity 0, // * stopbits 0, // xonchar 1, // xoffchar 0, // errorchar 0, // eofchar 0, // evtchar 0 // wreserved1 }; hcomm = createfile("com1", generic_read | generic_write, 0, null, open_existing, 0, null); if (hcomm != invalid_handle_value) { setupcomm(hcomm, 16*k, 16*k); setcommstate(hcomm, &dcb); setcommtimeouts(hcomm, &timeouts); } } static void closecomm (void) { closehandle(hcomm); } static byte recv (void) { byte text; dword temp; comstat comstat; while (1) { clearcommerror(hcomm, &temp, &comstat); if (comstat.cbinque != 0) break; sleep(1); } readfile(hcomm, &text, 1, &temp, null); return text; } static void send (void) { dword temp; // send other comp writefile(hcomm, sendbuff, sizeof (sendbuff), &temp, null); // check other comp done if (recv() != 0xaa) { beep(1000, 100); quit(); // comm error } }
it cable. not shielded , long.
Comments
Post a Comment