#include #include void NECStateMachine(const BYTE b); void SonyStateMachine(const BYTE b, const int bits); int OpenPort(CString port); void ClosePort(void); static HANDLE comm_port=NULL; int main(int argc, char *argv[]) { CString port="COM1"; BOOL time=FALSE; BOOL wrap=TRUE; BOOL ascii=FALSE; BOOL nec=FALSE; int sony=0; for(int i=1;i=2 && ( arg[0]=='-' || arg[0]=='/')) { switch(arg[1]) { case 'p': case 'P': if(arglen>2) port=arg+2; break; case 't': case 'T': time=TRUE; break; case 'r': case 'R': wrap=FALSE; break; case 'a': case 'A': ascii=TRUE; case 'n': case 'N': nec=TRUE; break; case 's': case 'S': sony=atoi(arg+2); if(sony!=12 && sony!=15 && sony!=20) sony=0; break; } } else if(arglen>3 && _strnicmp("COM",arg,3)==0) { port=arg; } } if(OpenPort(port)) return -1; do { BYTE by[16]; DWORD read; ReadFile(comm_port,by,16,&read,NULL); for(UINT u=0;u=192 && b<=235) { // 13500 uS typical // Allow 1228 to 15040 // 192 to 235 counts state=1; bit=0; data=0; } break; case 1: if(b>=14 && b<=22) { // Zero bit - 1125 uS typical // Allow 896 to 1408 // 14 to 22 counts ++bit; } else if(b>=30 && b<=40) { // One bit - 2250 uS typical // Allow 1920 to 2560 // 30 to 40 counts data|=(1<=32) { ASSERT(bit==32); printf("\nNEC Code: %08X\n",data); state=2; } break; case 2: // Varies // Total time for states 0, 1 and 2 is typicaly 110000 uS state=3; break; case 3: // 11250 uS typical // Allow 10048 to 12224 // 157 to 191 counts if(b>=157 && b<=191) { state=4; } else { goto state0; } break; case 4: // 98750 uS typical // This can vary quite a bit, so just ignore it // Total time for states 3 and 4 is typicaly 110000 uS printf("Rpt "); state=3; break; } } void SonyStateMachine(const BYTE b, const int bits) { // // Note: The MSB can not be decoded when only the total on/off period is available // static int state=0; static int bit; static UINT data; switch(state) { state0: case 0: if(b>=44 && b<=50) { // 3000 uS // Allow 2816 to 3200 // 44 to 50 counts state=1; bit=0; data=0; } break; case 1: if(b>=17 && b<=21) { // Zero bit - 1200 uS // Allow 1088 to 1344 // 17 to 21 ++bit; } else if(b>=26 && b<=30) { // One bit - 1800 uS // Allow 1664 to 1920 // 26 to 30 data|=(1<=bits-1) { ASSERT(bit==bits-1); printf("\nSony Code: %05X\n",data); state=2; } break; case 2: // Varies state=0; break; } } int OpenPort(CString port) { port="\\\\.\\"+port; comm_port=CreateFile(port,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL); if(comm_port==INVALID_HANDLE_VALUE) { printf("Error opening port: %s (%i)\n",port,GetLastError()); comm_port=NULL; return 1; } // SetupComm(comm_port,RxSize,TxSize); // Setup Rx/Tx buffer sizes (optional) DCB dcb; dcb.DCBlength=sizeof(dcb); GetCommState(comm_port,&dcb); dcb.BaudRate=57600; // bit rate dcb.ByteSize=8; // number of bits/byte, 4-8 dcb.Parity=NOPARITY; // parity dcb.StopBits=ONESTOPBIT;// stop bits dcb.fParity=0; // enable parity checking dcb.fBinary=1; // binary mode, no EOF check dcb.fOutxCtsFlow=0; // CTS output flow control dcb.fOutxDsrFlow=0; // DSR output flow control dcb.fDtrControl=DTR_CONTROL_DISABLE; // DTR flow control type dcb.fRtsControl=RTS_CONTROL_DISABLE; // RTS flow control dcb.fDsrSensitivity=0; // DSR sensitivity dcb.fTXContinueOnXoff=0;// XOFF continues Tx dcb.fOutX=0; // XON/XOFF out flow control dcb.fInX=0; // XON/XOFF in flow control dcb.fNull=0; // enable null stripping dcb.fAbortOnError=0; // abort reads/writes on error dcb.fErrorChar=0; // enable error replacement dcb.ErrorChar=0x00; // error replacement character //WORD XonLim; // transmit XON threshold //WORD XoffLim; // transmit XOFF threshold //char XonChar; // Tx and Rx XON character //char XoffChar; // Tx and Rx XOFF character //char EofChar; // end of input character //char EvtChar; // received event character SetCommState(comm_port,&dcb); COMMTIMEOUTS to; GetCommTimeouts(comm_port,&to); to.ReadIntervalTimeout=20; to.ReadTotalTimeoutMultiplier=2; to.ReadTotalTimeoutConstant=100; //to.WriteTotalTimeoutMultiplier=; //to.WriteTotalTimeoutConstant=; SetCommTimeouts(comm_port,&to); SetCommMask(comm_port,0); Sleep(500); EscapeCommFunction(comm_port,SETRTS); EscapeCommFunction(comm_port,SETDTR); return 0; } void ClosePort(void) { EscapeCommFunction(comm_port,CLRDTR); EscapeCommFunction(comm_port,CLRRTS); CloseHandle(comm_port); comm_port=NULL; }