| Цитата(EEEEEE @ 30.4.2008, 10:30) | xvr , спасибо за совет. А кода нет посмотреть, извини за нескромность. |
Кода под Builder с собой нет, есть код под VC (thread для приема организуется через _beginthread, метод ConfMgr::rs232_recver вызывается в нем) shutdown_event - Event который взводится для прерывания цикла приема
| Код | void ConfMgr::rs232_recver() { int tso=tstamp_options; int tso1=tstamp_options_rs; int sh=(tso>>18)&1023; HANDLE h[2]={shutdown_event,CreateEvent(NULL,TRUE,FALSE,NULL)}; OVERLAPPED ovr={0,0,0,0,h[1]};
char nm[5]="COM1"; nm[4]+=(tso>>28)&3;
HANDLE comp=CreateFile(nm,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,0); if (comp==INVALID_HANDLE_VALUE) { error(1,"RS232 TS Recv: Can't open COM port %s",nm); return; } DCB dcb={ sizeof(DCB),//DWORD DCBlength; // sizeof(DCB) tso1&0xFFFFF, //DWORD BaudRate; // current baud rate TRUE, //DWORD fBinary: 1; // binary mode, no EOF check FALSE, //DWORD fParity: 1; // enable parity checking FALSE, //DWORD fOutxCtsFlow:1; // CTS output flow control FALSE, //DWORD fOutxDsrFlow:1; // DSR output flow control DTR_CONTROL_DISABLE,// DWORD fDtrControl:2; // DTR flow control type FALSE, //DWORD fDsrSensitivity:1; // DSR sensitivity TRUE, //DWORD fTXContinueOnXoff:1; // XOFF continues Tx FALSE, //DWORD fOutX: 1; // XON/XOFF out flow control FALSE, //DWORD fInX: 1; // XON/XOFF in flow control FALSE, //DWORD fErrorChar: 1; // enable error replacement FALSE, //DWORD fNull: 1; // enable null stripping RTS_CONTROL_DISABLE,// DWORD fRtsControl:2; // RTS flow control FALSE, //DWORD fAbortOnError:1; // abort reads/writes on error 0, //DWORD fDummy2:17; // reserved 0, //WORD wReserved; // not currently used 0, //WORD XonLim; // transmit XON threshold 0, //WORD XoffLim; // transmit XOFF threshold 8, //BYTE ByteSize; // number of bits/byte, 4-8 (tso1>>20)&3,//BYTE Parity; // 0-4=no,odd,even,mark,space TWOSTOPBITS,//BYTE StopBits; // 0,1,2 = 1, 1.5, 2 0, //char XonChar; // Tx and Rx XON character 0, //char XoffChar; // Tx and Rx XOFF character 0, //char ErrorChar; // error replacement character 0, //char EofChar; // end of input character 0, //char EvtChar; // received event character 0 //WORD wReserved1; // reserved; do not use }; if (!SetCommState(comp,&dcb)) { CloseHandle(comp); error(1,"RS232 TS Recv: Can't set COM port parameters"); return; }
COMMTIMEOUTS ctout = {0,0,0,0,0}; if (!SetCommTimeouts(comp,&ctout)) error(1,"RS232 TS Recv: Can't set COM port timeouts"); SetupComm(comp,1,1); int last_recv=-1; unsigned lr_ts=0; bool is_error=false; unsigned error_count=0;
for(;;) { char buf; unsigned ts; DWORD rd;
if (!is_error) error_count>>=1; else { is_error=false; error_count++; if (error_count>1000) { error(1,"RS232 TS Recv: Error rate too high, giving up!"); break; } }
ResetEvent(h[1]); if (ReadFile(comp,&buf,1,&rd,&ovr)) ts=get_tmark(NULL,0); else if (GetLastError()==ERROR_IO_PENDING) { DWORD res=WaitForMultipleObjects(2,h,FALSE,INFINITE); ts=get_tmark(NULL,0); if (res!=WAIT_OBJECT_0+1) {CancelIo(comp); break;} if (!GetOverlappedResult(comp,&ovr,&rd,FALSE)) { error(1,"RS232 TS Recv: Error reading COM port"); is_error=true; continue; } } else { error(1,"RS232 TS Recv: Error reading COM port"); is_error=true; continue; } if (!rd) continue; ts-=sh; DWORD cerr=0; ClearCommError(comp,&cerr,0); if (cerr) { is_error=true; if (cerr!=CE_RXPARITY) {error(1,"RS232 TS Recv: Error (%x) on RS232 port detected",cerr); continue;} error(1,"RS232 TS Recv: Parity error detected"); if (last_recv!=-1) { send_mic_ts(last_recv,lr_ts); last_recv=-1; } continue; } if (!(buf&0x80)) {last_recv=buf; lr_ts=ts; continue;} buf&=0x7F; if (last_recv==-1) {send_mic_ts(buf,ts); continue;} if (buf!=last_recv) { error(1,"RS232 TS Recv: MIC indexes missmatch %d/%d",last_recv,buf); last_recv=-1; is_error=true; continue; } send_mic_ts(buf,eval_tmark(lr_ts,ts)); last_recv=-1; } CloseHandle(comp); }
|
|