void TSerialConnection :: Synchronize ( void ) { char b [1] ; DWORD CurrentTicks, LastTicks ; register int i ; * b = SYNC_CHAR ; ResetTimeOutFlag ( ) ; FlushOutput ( ) ; GetCommError ( DeviceId, NULL ) ; for ( i = 0 ; i < SYNC_SIZE ; i ++ ) { CurrentTicks = LastTicks = GetTickCount ( ) ; while ( CurrentTicks - LastTicks < SYNC_INTERVAL ) CurrentTicks = GetTickCount ( ) ; SendData ( b, 1 ) ; } * b = '*' ; SendData ( b, 1 ) ; CurrentTicks = LastTicks = GetTickCount ( ) ; while ( CurrentTicks - LastTicks < 50 ) CurrentTicks = GetTickCount ( ) ; GetCommError ( DeviceId, NULL ) ; }
//------------------------------------------------------------------- // Read an array of bytes to the COM port, verify that it was // sent out. Assume that baud rate has been set and the buffers have // been flushed. // // portnum - number 0 to MAX_PORTNUM-1. This number is provided to // indicate the symbolic port number. // inlen - the length of the data that was read // outbuf - the input data // // Returns number of characters read // int ReadCOM(int portnum, int inlen, uchar *inbuf) { COMSTAT ComStat; short result; ulong m; ulong more; // declare and set the default timeout int timeout = 20 * inlen + 60; //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// #ifdef DODEBUG short i; #endif //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// m = (ulong)msGettick() + (ulong)timeout; do { GetCommError(ComID[portnum],&ComStat); if ((short)ComStat.cbInQue >= inlen) { result = ReadComm(ComID[portnum],inbuf,inlen); if (result == (int)inlen) { GetCommError(ComID[portnum],&ComStat); more = ComStat.cbInQue; //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// #ifdef DODEBUG printf("R["); for (i = 0; i < inlen; i++) printf("%02X ",inbuf[i]); printf("]"); #endif //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// return result; } else return FALSE; } //else // yield this process //Yield(); } while ((ulong)msGettick() <= m); return FALSE; }
void XTTY::CommErrorMessage() { COMSTAT stat; int ce = GetCommError(m_idComDev, &stat); if (ce & CE_BREAK) MessageBox(m_hwnd, "ハードウェアがブレーク状態を検出したことを示します。", NULL, MB_OK | MB_APPLMODAL); if (ce & CE_CTSTO) MessageBox(m_hwnd, "CTS (clear-to-send) がタイムアウトです。文字を送信している間、COMSTAT構造体のfCtsHoldメンバにより指定された期間だけ、CTSがロー レベルです。", NULL, MB_OK | MB_APPLMODAL); if (ce & CE_DNS) MessageBox(m_hwnd, "パラレル デバイスが選択されなかったことを示します。", NULL, MB_OK | MB_APPLMODAL); if (ce & CE_DSRTO) MessageBox(m_hwnd, "DSR (data-set-ready) がタイムアウトです。文字を送信している間、COMSTAT構造体のfDsrHoldメンバにより指定された期間だけ、DSRがロー レベルです。", NULL, MB_OK | MB_APPLMODAL); if (ce & CE_FRAME) MessageBox(m_hwnd, "ハードウェアがフレーミング エラーを検出したことを示します。", NULL, MB_OK | MB_APPLMODAL); if (ce & CE_IOE) MessageBox(m_hwnd, "パラレル デバイスとの通信で、I/Oエラーが発生したことを示します。", NULL, MB_OK | MB_APPLMODAL); if (ce & CE_MODE) MessageBox(m_hwnd, "要求されたモードがサポートされていないか、idComDevパラメータが無効であることを示します。CE_MODEがセットされると、このフラグだけが有効なエラー値になります。", NULL, MB_OK | MB_APPLMODAL); if (ce & CE_OOP) MessageBox(m_hwnd, "パラレル デバイスの用紙切れを示す信号がオンになったことを示します。", NULL, MB_OK | MB_APPLMODAL); if (ce & CE_OVERRUN) MessageBox(m_hwnd, "次の文字を受信する前に、ハードウェアから文字が読み取られなかったことを示します。この文字は失われます。", NULL, MB_OK | MB_APPLMODAL); if (ce & CE_PTO) MessageBox(m_hwnd, "パラレル デバイスとの通信で、タイムアウトが発生したことを示します。", NULL, MB_OK | MB_APPLMODAL); if (ce & CE_RLSDTO) MessageBox(m_hwnd, "RLSD (receive-line-signal-detect) がタイムアウトです。文字を送信している間、COMSTAT構造体のfRlsdHoldメンバにより指定された期間だけ、RLSDがロー レベルです。", NULL, MB_OK | MB_APPLMODAL); if (ce & CE_RXOVER) MessageBox(m_hwnd, "受信キューのオーバフローが発生しました。入力キューに空きがないか、エンド オブ ファイル文字を受信した後も文字を受信したことを示します。", NULL, MB_OK | MB_APPLMODAL); if (ce & CE_RXPARITY) MessageBox(m_hwnd, "ハードウェアがパリティ エラーを検出したことを示します。", NULL, MB_OK | MB_APPLMODAL); if (ce & CE_TXFULL) MessageBox(m_hwnd, "関数が文字をキューに入れようとしたときに、送信キューに空きがなかったことを示します。", NULL, MB_OK | MB_APPLMODAL); }
int TSerialConnection :: SendData ( char * buffer, int buffer_size ) { COMSTAT ComStat ; register int size = buffer_size ; if ( DeviceId < 0 ) return ( 0 ) ; while ( size ) { SetCommEventMask ( DeviceId, EV_TXEMPTY ) ; while ( GetCommEventMask ( DeviceId, EV_TXEMPTY ) ) { SetCommEventMask ( DeviceId, EV_TXEMPTY ) ; } GetCommError ( DeviceId, & ComStat ) ; if ( ComStat. cbOutQue ) continue ; WriteComm ( DeviceId, buffer, 1 ) ; buffer ++, size -- ; } return ( buffer_size ) ; }
//------------------------------------------------------------------- // Description: // flush the rx and tx buffers // // portnum - number 0 to MAX_PORTNUM-1. This number is provided to // indicate the symbolic port number. // void FlushCOM(int portnum) { char ch[] = {"X"}; COMSTAT stat; for (;;) { GetCommError(ComID[portnum],&stat); if (stat.cbInQue) ReadComm(ComID[portnum],ch,1); else break; } FlushComm(ComID[portnum],0); FlushComm(ComID[portnum],1); }
//------------------------------------------------------------------- // Write an array of bytes to the COM port, verify that it was // sent out. Assume that baud rate has been set and the buffers have // been flushed. // // portnum - number 0 to MAX_PORTNUM-1. This number is provided to // indicate the symbolic port number. // outlen - the length of the data to be written // outbuf - the output data // // Returns TRUE for success and FALSE for failure // int WriteCOM(int portnum, int outlen, uchar *outbuf) { short result; COMSTAT ComStat; ulong m; // declare and set the default timeout int timeout = 20 * outlen + 60; //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// #ifdef DODEBUG short i; printf("W["); for (i = 0; i < outlen; i++) printf("%02X ",outbuf[i]); #endif //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// // Send data to write buffer result = WriteComm(ComID[portnum],outbuf,outlen); //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// #ifdef DODEBUG printf("(%d)]",result); #endif //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// // loop to wait for the write to complete if (result == outlen) { m = (ulong)msGettick() + (ulong)timeout; do { // yield this process //Yield(); GetCommError(ComID[portnum],&ComStat); if ((short)ComStat.cbOutQue == 0) return result; } while ((ulong)msGettick() <= m); } else return FALSE; }
BOOL TSerialConnection :: Receive ( void ) { char buffer [ SEND_BUFFER_SIZE_SERIAL / 4 ] ; char inbuf [ RECEIVE_BUFFER_SIZE_SERIAL ] ; int size, count ; int CommError ; DWORD TickCount ; COMSTAT ComStat ; register int i ; if ( DeviceId < 0 ) return ( FALSE ) ; TickCount = GetTickCount ( ) ; // Réception des caractères : CommError = GetCommError ( DeviceId, & ComStat ) ; if ( CommError & ( CE_BREAK | CE_RXOVER ) ) ClearCommBreak ( DeviceId ) ; size = ReadComm ( DeviceId, inbuf, RECEIVE_BUFFER_SIZE_SERIAL ) ; if ( size < 0 ) { size = - size ; GetCommError ( DeviceId, & ComStat ) ; ClearCommBreak ( DeviceId ) ; } for ( i = 0 ; i < size ; i ++ ) { if ( CharactersReceived + 1 >= ReceiveBufferSize ) { DestinationWindow -> HandleMessage ( WM_DATARECEIVED, CharactersReceived, 0L ) ; if ( CharactersReceived + 1 >= ReceiveBufferSize ) MessageBox ( NULL, "Arrghhh !!!", MB_OK | MB_ICONEXCLAMATION, "Trop de caractères reçus!" ) ; } ReceiveBuffer [ ( Origin + CharactersReceived ) % ReceiveBufferSize ] = inbuf [i] ; CharactersReceived = ( CharactersReceived + 1 ) % ReceiveBufferSize ; } // On vérifie s'il y a eu un timeout if ( TimeOutValue && ! size ) { if ( TickCount - TimeOutTick > TimeOutValue ) { ReceiveBuffer [ ( Origin + CharactersReceived ) % ReceiveBufferSize ] = TIMEOUT_CHAR ; CharactersReceived = ( CharactersReceived + 1 ) % ReceiveBufferSize ; size = 1 ; TimeOutFlag = TRUE ; } } if ( size ) TimeOutTick = TickCount ; // Prévenir la fenêtre que des caractères ont été reçus if ( CharactersReceived ) DestinationWindow -> HandleMessage ( WM_DATARECEIVED, CharactersReceived, 0L ) ; // Si des caractères sont à envoyer, on en profite count = ( int ) ( ( ConnectionParameters -> Serial. RealSpeed / 12 ) / ( 1000 / TIMER_INTERVAL ) ) ; count = min ( count, SEND_BUFFER_SIZE_SERIAL / 4 ) ; size = HugeTable -> Copy ( buffer, count ) ; if ( size ) DestinationWindow -> SendMessage ( WM_SENDDATA, size, ( LPARAM ) ( ( char far * ) buffer ) ) ; return ( size ) ; }