c++ - Modem does not return my program's echo request -
typing
at
+[enter]
on putty modem's com3 port gives meok
response.i used char-sending-and-recieving program analyze putty's output when sends data.
at
+[enter]
makes putty sendat\r
through port.
(\r
meaning carriage return ascii value of 13.)
i used program send same stream of chars com3 port.
modem didn't respond
ok
. doesn't send response program @ all. ever. noterror
messages.(i tested program, , can recieve data right after sending data of own, unlikely didn't catch
ok
in time, since requested longer strings response - strings hard miss.)(it has no problems communicating through ports program , putty.)
what cause this? how can recieve modem's ok
response?
(i tested strings ending \n
, \r\n
, \n\r
advised, in case. didn't work.)
code (ports com8 , com9 connected btw):
#include <iostream> #include <windows.h> #include <conio.h> using namespace std; bool writecomport(lpctstr portspecifier, lpctstr data) ; int readbyte(lpctstr portspecifier) ; void transciever( lpctstr portspecifier ) ; int main () { lpctstr portspecifier = "" ; string temp = "" ; { cout << "\ncom8 or com9 or com3? " ; getline(cin,temp) ; if( temp == "com8" || temp == "com9" || temp == "com3" ) portspecifier = temp.c_str() ; } while( temp != "com8" && temp != "com9" && temp != "com3" ) ; transciever(portspecifier) ; cout << "[program terminated.]\n" ; cin.get() ; return 0 ; } void transciever(lpctstr portspecifier ) { string text ; lpcstr data ; bool writesuccess = true ; char output ; { if( _kbhit() ) { text = _getch() ; data = text.c_str() ; writesuccess = writecomport(portspecifier,data) ; if(!writesuccess) cout << "[sending error.]\n" ; else { cout << text ; } } output = (char) readbyte(portspecifier) ; if( output != '_' ) { cout << output ; if( output == '=' ) { data = "*" ; writesuccess = writecomport(portspecifier,data) ; if(!writesuccess) cout << "\n[ fail: sending * autoresponse = ]\n" ; else cout << "\n[ success: sending * autoresponse = ]\n" ; } } } while( text != "#" && output != '#' ) ; } bool writecomport(lpctstr portspecifier, lpctstr data) { dcb dcb; dword byteswritten; handle hport = createfile( portspecifier, generic_write, 0, null, open_existing, 0, null ); if (!getcommstate(hport,&dcb)) { cout << "failed getting " << portspecifier << " port's state.\n" ; return false; } dcb.baudrate = cbr_9600; //9600 baud dcb.bytesize = 7; //7 data bits dcb.parity = true; dcb.stopbits = onestopbit; //1 stop if (!setcommstate(hport,&dcb)) { cout << "[failed setting " << portspecifier << "'s state.]\n" ; return false ; } int bytestowrite = 1 ; bool retval = writefile(hport,data,bytestowrite,&byteswritten,null); closehandle(hport); //close handle return retval; } int readbyte(lpctstr portspecifier) { dcb dcb; int retval; byte byte = '_' ; dword dwbytestransferred; handle hport = createfile ( portspecifier, generic_read, 0, null, open_existing, 0, null ); if (!getcommstate(hport,&dcb)) { cout << "\n[getcommstate fail]\n" ; return '_' ; } commtimeouts timeouts={0}; timeouts.readintervaltimeout=50; timeouts.readtotaltimeoutconstant=50; timeouts.readtotaltimeoutmultiplier=10; timeouts.writetotaltimeoutconstant=50; timeouts.writetotaltimeoutmultiplier=10; if(!setcommtimeouts(hport, &timeouts)) { cout << "\n[setcommtimeouts fail]\n" ; return '_' ; } dcb.baudrate = cbr_9600; //9600 baud dcb.bytesize = 7; //7 data bits dcb.parity = true; //parity dcb.stopbits = onestopbit; //1 stop if (!setcommstate(hport,&dcb)) { cout << "\n[setcommstate fail]\n" ; return '_' ; } if (!setcommmask (hport, ev_rxchar | ev_err) ) { cout << "\n[setcommmask fail]\n" ; return '_' ; } if( !readfile (hport, &byte, 1, &dwbytestransferred, 0) ) { cout << "\n[readfile fail]\n" ; return '_' ; } retval = byte; if (!closehandle(hport)) { cout << "\n[closehandle fail]\n" ; return '_' ; } return retval; }
.
also alternative version of readbyte, reads many bytes @ once:
string readbyte(lpctstr portspecifier) { dcb dcb; dword dwbytestransferred; handle hport = createfile ( portspecifier, generic_read, 0, null, open_existing, 0, null ); if (!getcommstate(hport,&dcb)) { cout << "\n[readbyte: getcommstate fail]\n" ; return "-1" ; } commtimeouts timeouts={0}; timeouts.readintervaltimeout=50; timeouts.readtotaltimeoutconstant=50; timeouts.readtotaltimeoutmultiplier=10; timeouts.writetotaltimeoutconstant=50; timeouts.writetotaltimeoutmultiplier=10; if(!setcommtimeouts(hport, &timeouts)) { cout << "\n[readbyte: setcommtimeouts fail]\n" ; return "-1" ; } dcb.baudrate = cbr_9600; //9600 baud dcb.bytesize = 7; //7 data bits dcb.parity = evenparity; //parity dcb.stopbits = onestopbit; //1 stop if (!setcommstate(hport,&dcb)) { cout << "\n[readbyte: setcommstate fail]\n" ; return "-1" ; } if (!setcommmask (hport, ev_rxchar | ev_err) ) { cout << "\n[readbyte: setcommmask fail]\n" ; return "-1" ; } const int bytestoread = 30 ; char messagerecieved[bytestoread+1] = {0}; //&byte if( !readfile (hport, &messagerecieved, bytestoread, &dwbytestransferred, 0) ) { cout << "\n[readbyte: readfile fail]\n" ; return "-1" ; } if (!closehandle(hport)) { cout << "\n[readbyte: closehandle fail]\n" ; return "-1" ; } if( dwbytestransferred == bytestoread ) { cout << "\n[readbyte: possible message cut off @ end." ; } return string(messagerecieved) ; }
Comments
Post a Comment