📄 tty.cpp
字号:
//调读并口EPP(0x378H)的数据函数
OpenLPTConnection();
// get rid of event handle
CloseHandle( os.hEvent ) ;
// clear information in structure (kind of a "we're done flag")
THREADID( npTTYInfo ) = 0 ;
HTHREAD( npTTYInfo ) = NULL ;
ExitThread(0);
return 0;
}
else
{
AfxMessageBox("重新进行自校验...");
// get rid of event handle
CloseHandle( os.hEvent ) ;
// clear information in structure (kind of a "we're done flag")
THREADID( npTTYInfo ) = 0 ;
HTHREAD( npTTYInfo ) = NULL ;
ExitThread(0);
return 0;
}
// force a paint
}
}
while ( nLength > 0 ) ;
}
}
// get rid of event handle
CloseHandle( os.hEvent ) ;
// clear information in structure (kind of a "we're done flag")
THREADID( npTTYInfo ) = 0 ;
HTHREAD( npTTYInfo ) = NULL ;
*/
return( TRUE ) ;
} // end of CommWatchProc()
//---------------------------------------------------------------------------
// int NEAR ReadCommBlock( LPSTR lpszBlock, int nMaxLength )
//
// Description:
// Reads a block from the COM port and stuffs it into
// the provided buffer.
//
// Parameters:
// HWND hWnd
// handle to TTY window
//
// LPSTR lpszBlock
// block used for storage
//
// int nMaxLength
// max length of block to read
//
// Win-32 Porting Issues:
// - ReadComm() has been replaced by ReadFile() in Win-32.
// - Overlapped I/O has been implemented.
//
//---------------------------------------------------------------------------
int NEAR ReadCommBlock(NPTTYINFO npTTYInfo, LPSTR lpszBlock, int nMaxLength )
{
BOOL fReadStat ;
COMSTAT ComStat ;
DWORD dwErrorFlags;
DWORD dwLength;
DWORD dwError;
char szError[ 10 ] ;
if (NULL == npTTYInfo)
return ( FALSE ) ;
// only try to read number of bytes in queue
ClearCommError( COMDEV( npTTYInfo ), &dwErrorFlags, &ComStat ) ;
dwLength = min( (DWORD) nMaxLength, ComStat.cbInQue ) ;
if (dwLength > 0)
{
fReadStat = ReadFile( COMDEV( npTTYInfo ), lpszBlock,
dwLength, &dwLength, &READ_OS( npTTYInfo ) ) ;
if (!fReadStat)
{
if (GetLastError() == ERROR_IO_PENDING)
{
OutputDebugString("\n\rIO Pending");
// We have to wait for read to complete.
// This function will timeout according to the
// CommTimeOuts.ReadTotalTimeoutConstant variable
// Every time it times out, check for port errors
while(!GetOverlappedResult( COMDEV( npTTYInfo ),
&READ_OS( npTTYInfo ), &dwLength, TRUE ))
{
dwError = GetLastError();
if(dwError == ERROR_IO_INCOMPLETE)
// normal result if not finished
continue;
else
{
// an error occurred, try to recover
wsprintf( szError, "<CE-%u>", dwError ) ;
//WriteTTYBlock( npTTYInfo, szError, lstrlen( szError ) ) ;
ClearCommError( COMDEV( npTTYInfo ), &dwErrorFlags, &ComStat ) ;
/*if ((dwErrorFlags > 0) && DISPLAYERRORS( npTTYInfo ))
{
wsprintf( szError, "<CE-%u>", dwErrorFlags ) ;
//WriteTTYBlock( npTTYInfo, szError, lstrlen( szError ) ) ;
}*/
break;
}
}
}
else
{
// some other error occurred
dwLength = 0 ;
ClearCommError( COMDEV( npTTYInfo ), &dwErrorFlags, &ComStat ) ;
/*if ((dwErrorFlags > 0) && DISPLAYERRORS( npTTYInfo ))
{
wsprintf( szError, "<CE-%u>", dwErrorFlags ) ;
//WriteTTYBlock( npTTYInfo, szError, lstrlen( szError ) ) ;
}*/
}
}
}
return ( dwLength ) ;
} // end of ReadCommBlock()
//---------------------------------------------------------------------------
// BOOL NEAR WriteCommBlock( BYTE *pByte )
//
// Description:
// Writes a block of data to the COM port specified in the associated
// TTY info structure.
//
// Parameters:
//
// BYTE *pByte
// pointer to data to write to port
//
// Win-32 Porting Issues:
// - WriteComm() has been replaced by WriteFile() in Win-32.
// - Overlapped I/O has been implemented.
//
//---------------------------------------------------------------------------
BOOL NEAR WriteCommBlock( NPTTYINFO npTTYInfo, BYTE *lpByte , DWORD dwBytesToWrite, DWORD *dwBytesWritten )
{
BOOL fWriteStat ;
DWORD dwErrorFlags;
DWORD dwError;
DWORD dwBytesSent=0;
COMSTAT ComStat;
char szError[ 128 ] ;
if (NULL == npTTYInfo)
return ( FALSE ) ;
fWriteStat = WriteFile( COMDEV( npTTYInfo ), lpByte, dwBytesToWrite,
dwBytesWritten, &WRITE_OS( npTTYInfo ) ) ;
// Note that normally the code will not execute the following
// because the driver caches write operations. Small I/O requests
// (up to several thousand bytes) will normally be accepted
// immediately and WriteFile will return true even though an
// overlapped operation was specified
if (!fWriteStat)
{
if(GetLastError() == ERROR_IO_PENDING)
{
// We should wait for the completion of the write operation
// so we know if it worked or not
// This is only one way to do this. It might be beneficial to
// place the write operation in a separate thread
// so that blocking on completion will not negatively
// affect the responsiveness of the UI
// If the write takes too long to complete, this
// function will timeout according to the
// CommTimeOuts.WriteTotalTimeoutMultiplier variable.
// This code logs the timeout but does not retry
// the write.
while(!GetOverlappedResult( COMDEV( npTTYInfo ),
&WRITE_OS( npTTYInfo ), dwBytesWritten, TRUE ))
{
dwError = GetLastError();
if(dwError == ERROR_IO_INCOMPLETE)
{
// normal result if not finished
dwBytesSent += *dwBytesWritten;
continue;
}
else
{
// an error occurred, try to recover
wsprintf( szError, "<CE-%u>", dwError ) ;
//WriteTTYBlock( npTTYInfo, szError, lstrlen( szError ) ) ;
ClearCommError( COMDEV( npTTYInfo ), &dwErrorFlags, &ComStat ) ;
/*if ((dwErrorFlags > 0) && DISPLAYERRORS( npTTYInfo ))
{
wsprintf( szError, "<CE-%u>", dwErrorFlags ) ;
//WriteTTYBlock( npTTYInfo, szError, lstrlen( szError ) ) ;
}*/
break;
}
}
dwBytesSent += *dwBytesWritten;
if( dwBytesSent != dwBytesToWrite )
wsprintf(szError,"\nProbable Write Timeout: Total of %ld bytes sent", dwBytesSent);
else
wsprintf(szError,"\n%ld bytes written", dwBytesSent);
OutputDebugString(szError);
}
else
{
// some other error occurred
ClearCommError( COMDEV( npTTYInfo ), &dwErrorFlags, &ComStat ) ;
/*if ((dwErrorFlags > 0) && DISPLAYERRORS( npTTYInfo ))
{
wsprintf( szError, "<CE-%u>", dwErrorFlags ) ;
//WriteTTYBlock( npTTYInfo, szError, lstrlen( szError ) ) ;
}*/
return ( FALSE );
}
}
return ( TRUE ) ;
} // end of WriteCommBlock()
//---------------------------------------------------------------------------
// BOOL NEAR WriteTTYBlock( HWND hWnd, LPSTR lpBlock, int nLength )
//
// Description:
// Writes block to TTY screen. Nothing fancy - just
// straight TTY.
// gl
// Parameters:
// HWND hWnd
// handle to TTY window
//
// LPSTR lpBlock
// far pointer to block of data
//
// int nLength
// length of block
//
//---------------------------------------------------------------------------
#define WM_MYMESSAGE WM_USER + 101
BOOL NEAR WriteTTYBlock( NPTTYINFO npTTYInfo, LPSTR lpBlock, int nLength )
{
int i ;
if (NULL == npTTYInfo)
return ( FALSE ) ;
for(i=0; i<nLength; i++)
{
; // write string to file here,写到链表类
}
return ( TRUE ) ;
} // end of WriteTTYBlock()
BOOL NEAR CTTY::DestroyTTYInfo(void)
{
if (NULL == npTTYInfo)
return ( FALSE ) ;
// force connection closed (if not already closed)
if (CONNECTED( npTTYInfo ))
CloseConnection( );
// clean up event objects
CloseHandle( READ_OS( npTTYInfo ).hEvent ) ;
CloseHandle( WRITE_OS( npTTYInfo ).hEvent ) ;
LocalFree( npTTYInfo ) ;
return ( TRUE ) ;
} // end of DestroyTTYInfo()
//---------------------------------------------------------------------------
// BOOL NEAR CloseConnection( HWND hWnd )
//
// Description:
// Closes the connection to the port. Resets the connect flag
// in the TTYINFO struct.
//
// Parameters:
// HWND hWnd
// handle to TTY window
//
// Win-32 Porting Issues:
// - Needed to stop secondary thread. SetCommMask() will signal the
// WaitCommEvent() event and the thread will halt when the
// CONNECTED() flag is clear.
// - Use new PurgeComm() API to clear communications driver before
// closing device.
//
//---------------------------------------------------------------------------
BOOL NEAR CTTY::CloseConnection( void )
{
if (NULL == npTTYInfo)
return ( FALSE ) ;
if (COMDEV(npTTYInfo) == NULL)
return ( FALSE ) ;
// set connected flag to FALSE
CONNECTED( npTTYInfo ) = FALSE ;
SetCommMask( COMDEV( npTTYInfo ), 0 ) ;
// block until thread has been halted
while(THREADID(npTTYInfo) != 0);
// drop DTR
EscapeCommFunction( COMDEV( npTTYInfo ), CLRDTR ) ;
// purge any outstanding reads/writes and close device handle
PurgeComm( COMDEV( npTTYInfo ), PURGE_TXABORT | PURGE_RXABORT |
PURGE_TXCLEAR | PURGE_RXCLEAR ) ;
CloseHandle( COMDEV( npTTYInfo ) ) ;
// change the selectable items in the menu
return ( TRUE ) ;
} // end of CloseConnection()
// port: EPP port address 并口
// pbresult: store reading bytes from port
// number: 2*number bytes to read
// return: readed byte number
int fnEppRoutine(int port, BYTE *pbResult, int number)
{
int modus = 96; // port transfer rate 1000kB/s
int integration = 15; // integration time 150ms
//int status;
BYTE status;
int counter=0;
// initiation port
// reading from EPP port
do
{
do
{
//test com2
// _outp(0x2f8,0x84);
// status = _inp(0x2f8); // read the status
//_outp(port+1,0x00);
status = _inp(port+1); // read the status
} while ((status & 0x80) != 0);
// status setting, now can read port + 4
//BYTE bresult = _inp(port+4);
pbResult[counter] = _inp(port + 4); //read port+4
counter = counter+1;
status = 0; // reset status = 0 ;
// read next byte
do
{
status = _inp(port+1); // read status
} while ((status & 0x80) != 0);
// status setting, now can read port + 4
pbResult[counter] = _inp(port + 3); //read port+4
counter = counter+1;
status = 0; // reset status = 0 ;
//处理并口数据
number = number-1;
} while (number > 0);
return counter; // return reading bytes number
}
// reading from EPP port
BOOL OpenLPTConnection()
{
BYTE pbResult[4096]; // store reading bytes
int readednum;
// fnEppRoutine(port,pbresult,number): function calling from DLL
// port: EPP port address
// pbresult: store reading bytes from port
// number: 2*number bytes to read
// return: readed byte number
readednum = fnEppRoutine(0x378, pbResult, 2); //0x378 EPP基地址 COMS 中设置
if ( readednum > 0 )
return true; // success
else
return false; // false
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -