增加手动机测试
This commit is contained in:
@@ -21,7 +21,7 @@ static char Codes[][6] =
|
||||
*/
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
// CSerial() : Constructor
|
||||
// CPSerial() : Constructor
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
// CSerialTask() : Function to run ReceiveTask() method as a task
|
||||
@@ -31,7 +31,7 @@ unsigned int WINAPI CSerialTask(LPVOID CSerialPtr)
|
||||
{
|
||||
TRACE( TEXT("Serial task has started \n") );
|
||||
// Call the ControlTask function in the specified plugin
|
||||
((CSerial *)CSerialPtr)->ReceiveTask();
|
||||
((CPSerial *)CSerialPtr)->ReceiveTask();
|
||||
|
||||
TRACE( TEXT("Serial task has completed \n") );
|
||||
|
||||
@@ -40,7 +40,7 @@ unsigned int WINAPI CSerialTask(LPVOID CSerialPtr)
|
||||
return(0);
|
||||
}
|
||||
|
||||
CSerial::CSerial()
|
||||
CPSerial::CPSerial()
|
||||
{
|
||||
// Serial port is not open
|
||||
m_PortHandle = INVALID_HANDLE_VALUE;
|
||||
@@ -93,9 +93,9 @@ CSerial::CSerial()
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
// ~CSerial() : Destructor - Close the port and free up the CriticalSection
|
||||
// ~CPSerial() : Destructor - Close the port and free up the CriticalSection
|
||||
|
||||
CSerial::~CSerial()
|
||||
CPSerial::~CPSerial()
|
||||
{
|
||||
if( IsOpen( ) )
|
||||
{
|
||||
@@ -122,7 +122,7 @@ CSerial::~CSerial()
|
||||
// OpenPort() : Opens the serial port using the parameters set by default
|
||||
// or a call to SetPort
|
||||
|
||||
DWORD CSerial::Open()
|
||||
DWORD CPSerial::Open()
|
||||
{
|
||||
CString PortName;
|
||||
COMMTIMEOUTS CommTimeOut;
|
||||
@@ -201,7 +201,7 @@ DWORD CSerial::Open()
|
||||
// IsOpen() : returns true if the serial port is open
|
||||
//
|
||||
|
||||
int CSerial::IsOpen( )
|
||||
int CPSerial::IsOpen( )
|
||||
{
|
||||
return( m_PortHandle != INVALID_HANDLE_VALUE );
|
||||
}
|
||||
@@ -211,7 +211,7 @@ int CSerial::IsOpen( )
|
||||
// SetPort() : Store the serial settings. If the port is open then these
|
||||
// settings are applied now
|
||||
|
||||
int CSerial::SetPort(int Port,int Baud,char Parity,int Bits,int StopBits,int HandShake)
|
||||
int CPSerial::SetPort(int Port,int Baud,char Parity,int Bits,int StopBits,int HandShake)
|
||||
{
|
||||
// Use the current settings if the value has the default of 0
|
||||
m_Port = Port;
|
||||
@@ -243,7 +243,7 @@ int CSerial::SetPort(int Port,int Baud,char Parity,int Bits,int StopBits,int Han
|
||||
// GetPortData() : return the current settings
|
||||
//
|
||||
|
||||
void CSerial::GetPortData(int *Port,int *Baud,char *Parity,int *Bits,int *StopBits,int *HandShake)
|
||||
void CPSerial::GetPortData(int *Port,int *Baud,char *Parity,int *Bits,int *StopBits,int *HandShake)
|
||||
{
|
||||
// return the requested settings
|
||||
if( Port )
|
||||
@@ -265,7 +265,7 @@ void CSerial::GetPortData(int *Port,int *Baud,char *Parity,int *Bits,int *StopBi
|
||||
// ClosePort() : Close the port and shut down the monitoring thread
|
||||
//
|
||||
|
||||
DWORD CSerial::Close()
|
||||
DWORD CPSerial::Close()
|
||||
{
|
||||
//struct SerialList *Free;
|
||||
HANDLE Port;
|
||||
@@ -319,13 +319,13 @@ DWORD CSerial::Close()
|
||||
// Send functions // [8/11/2004]
|
||||
//
|
||||
|
||||
DWORD CSerial::Send(LPCSTR buffer, int l, BOOL /*needsResponse=FALSE*/)
|
||||
DWORD CPSerial::Send(LPCSTR buffer, int l, BOOL /*needsResponse=FALSE*/)
|
||||
{
|
||||
|
||||
return ( WritePort ((const char*) buffer, (DWORD) l));
|
||||
}
|
||||
/*
|
||||
DWORD CSerial::Send(CString buffer)
|
||||
DWORD CPSerial::Send(CString buffer)
|
||||
{
|
||||
char LocBuffer[MAX_OUTPUT_BUFFER_SIZE];
|
||||
int length = buffer.GetLength ();
|
||||
@@ -347,7 +347,7 @@ return res;
|
||||
// WritePort() : Writes the specifed bytes to the serial port
|
||||
//
|
||||
|
||||
DWORD CSerial::WritePort(const char *Buffer,DWORD Bytes)
|
||||
DWORD CPSerial::WritePort(const char *Buffer,DWORD Bytes)
|
||||
{
|
||||
DWORD BytesWritten, TotalWritten, Error;
|
||||
BOOL WriteState;
|
||||
@@ -413,7 +413,7 @@ DWORD CSerial::WritePort(const char *Buffer,DWORD Bytes)
|
||||
// ReceiveTask() : Internal function, this runs as a thread and provides the
|
||||
// OnRecieve and OnTransmit events
|
||||
|
||||
void CSerial::ReceiveTask( void )
|
||||
void CPSerial::ReceiveTask( void )
|
||||
{
|
||||
//DWORD BytesWritten;
|
||||
DWORD Events;
|
||||
@@ -478,7 +478,7 @@ void CSerial::ReceiveTask( void )
|
||||
// OnReceive() : Default OnReceive()
|
||||
// V114
|
||||
|
||||
void CSerial::OnReceive()
|
||||
void CPSerial::OnReceive()
|
||||
{
|
||||
// Dummy OnReceieve if not used
|
||||
char s[255]={0};
|
||||
@@ -510,7 +510,7 @@ void CSerial::OnReceive()
|
||||
// ReadPort() : Read the specifed number of bytes.
|
||||
//
|
||||
|
||||
DWORD CSerial::ReadPort(char *Buffer,DWORD Bytes)
|
||||
DWORD CPSerial::ReadPort(char *Buffer,DWORD Bytes)
|
||||
{
|
||||
DWORD BytesRead,Error;
|
||||
BOOL ReadState;
|
||||
@@ -564,7 +564,7 @@ DWORD CSerial::ReadPort(char *Buffer,DWORD Bytes)
|
||||
// ReadPort() : Read the specifed number of bytes into a CString class.
|
||||
//
|
||||
#if 0
|
||||
DWORD CSerial::ReadPort(CString &Buffer,DWORD Bytes)
|
||||
DWORD CPSerial::ReadPort(CString &Buffer,DWORD Bytes)
|
||||
{
|
||||
DWORD BytesRead;
|
||||
|
||||
@@ -581,7 +581,7 @@ DWORD CSerial::ReadPort(CString &Buffer,DWORD Bytes)
|
||||
// ProgramPort() : Internal function to setup the serial port
|
||||
//
|
||||
|
||||
int CSerial::ProgramPort(int Port,int Baud,char Parity,int Bits,int StopBits,int HandShake)
|
||||
int CPSerial::ProgramPort(int Port,int Baud,char Parity,int Bits,int StopBits,int HandShake)
|
||||
{
|
||||
CString Param,Params;
|
||||
DCB SerialDCB;
|
||||
@@ -728,7 +728,7 @@ int CSerial::ProgramPort(int Port,int Baud,char Parity,int Bits,int StopBits,int
|
||||
// OnTransmit() : Default OnTransmit()
|
||||
//
|
||||
|
||||
void CSerial::OnTransmit(int /*Item*/, DWORD /*Error*/)
|
||||
void CPSerial::OnTransmit(int /*Item*/, DWORD /*Error*/)
|
||||
{
|
||||
// Dummy OnTransmit if not used
|
||||
}
|
||||
@@ -738,7 +738,7 @@ void CSerial::OnTransmit(int /*Item*/, DWORD /*Error*/)
|
||||
// SetTimeouts() : Sets the rx and tx timeouts
|
||||
//
|
||||
|
||||
void CSerial::SetTimeouts( int RXTimeout, int TXTimeout )
|
||||
void CPSerial::SetTimeouts( int RXTimeout, int TXTimeout )
|
||||
{
|
||||
COMMTIMEOUTS CommTimeOut;
|
||||
|
||||
@@ -764,7 +764,7 @@ void CSerial::SetTimeouts( int RXTimeout, int TXTimeout )
|
||||
// AddToDebug() : Add the data to the debug output. State is 1 = rx 2 = tx
|
||||
// 3 = user
|
||||
|
||||
void CSerial::AddToDebug( const char * /*Ptr*/, DWORD /*BytesToCopy*/, int /*State*/ )
|
||||
void CPSerial::AddToDebug( const char * /*Ptr*/, DWORD /*BytesToCopy*/, int /*State*/ )
|
||||
{
|
||||
//ZH
|
||||
#if 0
|
||||
@@ -831,7 +831,7 @@ void CSerial::AddToDebug( const char * /*Ptr*/, DWORD /*BytesToCopy*/, int /*Sta
|
||||
// FlushPort() : Removes all characters in the serial buffer
|
||||
//
|
||||
/*
|
||||
int CSerial::FlushPort(void)
|
||||
int CPSerial::FlushPort(void)
|
||||
{
|
||||
int TXTimeout,RXTimeout;
|
||||
char FlushBuffer[256];
|
||||
@@ -879,7 +879,7 @@ return(BytesTotal);
|
||||
// MaxPort() :
|
||||
//
|
||||
|
||||
int CSerial::MaxPort()
|
||||
int CPSerial::MaxPort()
|
||||
{
|
||||
// return the max port, :-)
|
||||
return(8);
|
||||
@@ -892,7 +892,7 @@ int CSerial::MaxPort()
|
||||
// data sent.
|
||||
//
|
||||
|
||||
int CSerial::Transmit(const char * /*Buffer*/,DWORD /*Bytes*/)
|
||||
int CPSerial::Transmit(const char * /*Buffer*/,DWORD /*Bytes*/)
|
||||
{
|
||||
/*
|
||||
struct SerialList *Ptr;
|
||||
@@ -952,7 +952,7 @@ int CSerial::Transmit(const char * /*Buffer*/,DWORD /*Bytes*/)
|
||||
//
|
||||
|
||||
/*
|
||||
int CSerial::AddReceived( const char *Buffer,DWORD Bytes)
|
||||
int CPSerial::AddReceived( const char *Buffer,DWORD Bytes)
|
||||
{
|
||||
DWORD index = 0; //primary buffer index
|
||||
struct SerialList *Ptr;
|
||||
@@ -1081,7 +1081,7 @@ return(TRUE);
|
||||
//
|
||||
//ZH
|
||||
/*
|
||||
char *CSerial::GetNextReceived(void)
|
||||
char *CPSerial::GetNextReceived(void)
|
||||
{
|
||||
struct SerialList *Free;
|
||||
|
||||
@@ -1120,7 +1120,7 @@ return( m_RXTempPtr );
|
||||
// ascii hex
|
||||
//
|
||||
|
||||
int CSerial::HexToInt(char *Data, int Bytes)
|
||||
int CPSerial::HexToInt(char *Data, int Bytes)
|
||||
{
|
||||
int Byte;
|
||||
int HexChar, Value;
|
||||
@@ -1150,7 +1150,7 @@ int CSerial::HexToInt(char *Data, int Bytes)
|
||||
//
|
||||
//ZH
|
||||
/*
|
||||
void CSerial::RegisterDebugWindow()
|
||||
void CPSerial::RegisterDebugWindow()
|
||||
{
|
||||
// Register the CSerialRaw window for future use
|
||||
WNDCLASS wndcls;
|
||||
@@ -1160,7 +1160,7 @@ wndcls.hInstance = AfxGetInstanceHandle( );
|
||||
wndcls.hCursor = LoadCursor( NULL, IDC_ARROW );
|
||||
wndcls.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
|
||||
wndcls.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
|
||||
wndcls.lpszClassName = TEXT("CSerial");
|
||||
wndcls.lpszClassName = TEXT("CPSerial");
|
||||
AfxRegisterClass( &wndcls );
|
||||
}
|
||||
*/
|
||||
@@ -1172,7 +1172,7 @@ AfxRegisterClass( &wndcls );
|
||||
// SendBuffer() : Internal function, this writes the next block of data
|
||||
// queued to the serial port.
|
||||
|
||||
void CSerial::SendBuffer(int Next)
|
||||
void CPSerial::SendBuffer(int Next)
|
||||
{
|
||||
struct SerialList *Free;
|
||||
// DWORD BytesWritten;
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
*$!! Serial.h
|
||||
*$!!
|
||||
*$!! DESCRIPTION
|
||||
*$!! Header file for CSerial.
|
||||
*$!! Header file for CPSerial.
|
||||
*$!!
|
||||
*$!! AUTHOR
|
||||
*$!! M.J.S.Gooder.
|
||||
@@ -69,17 +69,17 @@ const int CS_DEFAULT_RX_TIMEOUT = 25;
|
||||
const int CS_DEFAULT_TX_TIMEOUT = 1000;
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// The CSerial class
|
||||
// The CPSerial class
|
||||
|
||||
class CSerial : public CMMIO
|
||||
class CPSerial : public CMMIO
|
||||
{
|
||||
// Construction
|
||||
public:
|
||||
CSerial();
|
||||
CPSerial();
|
||||
|
||||
// Implementation
|
||||
public:
|
||||
virtual ~CSerial();
|
||||
virtual ~CPSerial();
|
||||
|
||||
// Attributes
|
||||
public:
|
||||
|
||||
@@ -0,0 +1,170 @@
|
||||
// Serial.cpp
|
||||
|
||||
#include "stdafx.h"
|
||||
#include<WinBase.h>
|
||||
#include<WinDef.h>
|
||||
#include "Serial.h"
|
||||
|
||||
CSerial::CSerial()
|
||||
{
|
||||
|
||||
memset( &m_OverlappedRead, 0, sizeof( OVERLAPPED ) );
|
||||
memset( &m_OverlappedWrite, 0, sizeof( OVERLAPPED ) );
|
||||
m_hIDComDev = NULL;
|
||||
m_bOpened = FALSE;
|
||||
|
||||
}
|
||||
|
||||
CSerial::~CSerial()
|
||||
{
|
||||
|
||||
Close();
|
||||
|
||||
}
|
||||
|
||||
BOOL CSerial::Open( int nPort, int nBaud )
|
||||
{
|
||||
|
||||
if( m_bOpened ) return( TRUE );
|
||||
|
||||
TCHAR szPort[15];
|
||||
TCHAR szComParams[50];
|
||||
DCB dcb;
|
||||
|
||||
wsprintf( szPort, _T("COM%d"), nPort );
|
||||
m_hIDComDev = CreateFile( szPort, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL );
|
||||
if( m_hIDComDev == NULL ) return( FALSE );
|
||||
|
||||
memset( &m_OverlappedRead, 0, sizeof( OVERLAPPED ) );
|
||||
memset( &m_OverlappedWrite, 0, sizeof( OVERLAPPED ) );
|
||||
|
||||
COMMTIMEOUTS CommTimeOuts;
|
||||
CommTimeOuts.ReadIntervalTimeout = 0xFFFFFFFF;
|
||||
CommTimeOuts.ReadTotalTimeoutMultiplier = 0;
|
||||
CommTimeOuts.ReadTotalTimeoutConstant = 0;
|
||||
CommTimeOuts.WriteTotalTimeoutMultiplier = 0;
|
||||
CommTimeOuts.WriteTotalTimeoutConstant = 5000;
|
||||
SetCommTimeouts( m_hIDComDev, &CommTimeOuts );
|
||||
|
||||
wsprintf( szComParams, _T("COM%d:%d,n,8,1"), nPort, nBaud );
|
||||
|
||||
m_OverlappedRead.hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
|
||||
m_OverlappedWrite.hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
|
||||
|
||||
dcb.DCBlength = sizeof( DCB );
|
||||
GetCommState( m_hIDComDev, &dcb );
|
||||
dcb.BaudRate = nBaud;
|
||||
dcb.ByteSize = 8;
|
||||
unsigned char ucSet;
|
||||
ucSet = (unsigned char) ( ( FC_RTSCTS & FC_DTRDSR ) != 0 );
|
||||
ucSet = (unsigned char) ( ( FC_RTSCTS & FC_RTSCTS ) != 0 );
|
||||
ucSet = (unsigned char) ( ( FC_RTSCTS & FC_XONXOFF ) != 0 );
|
||||
if( !SetCommState( m_hIDComDev, &dcb ) ||
|
||||
!SetupComm( m_hIDComDev, 10000, 10000 ) ||
|
||||
m_OverlappedRead.hEvent == NULL ||
|
||||
m_OverlappedWrite.hEvent == NULL ){
|
||||
DWORD dwError(0);
|
||||
dwError = GetLastError();
|
||||
if( m_OverlappedRead.hEvent != NULL ) CloseHandle( m_OverlappedRead.hEvent );
|
||||
if( m_OverlappedWrite.hEvent != NULL ) CloseHandle( m_OverlappedWrite.hEvent );
|
||||
CloseHandle( m_hIDComDev );
|
||||
return( FALSE );
|
||||
}
|
||||
|
||||
m_bOpened = TRUE;
|
||||
|
||||
return( m_bOpened );
|
||||
|
||||
}
|
||||
|
||||
BOOL CSerial::Close( void )
|
||||
{
|
||||
|
||||
if( !m_bOpened || m_hIDComDev == NULL ) return( TRUE );
|
||||
|
||||
if( m_OverlappedRead.hEvent != NULL ) CloseHandle( m_OverlappedRead.hEvent );
|
||||
if( m_OverlappedWrite.hEvent != NULL ) CloseHandle( m_OverlappedWrite.hEvent );
|
||||
CloseHandle( m_hIDComDev );
|
||||
m_bOpened = FALSE;
|
||||
m_hIDComDev = NULL;
|
||||
|
||||
return( TRUE );
|
||||
|
||||
}
|
||||
|
||||
BOOL CSerial::WriteCommByte( unsigned char ucByte )
|
||||
{
|
||||
BOOL bWriteStat;
|
||||
DWORD dwBytesWritten;
|
||||
|
||||
bWriteStat = WriteFile( m_hIDComDev, (LPSTR) &ucByte, 1, &dwBytesWritten, &m_OverlappedWrite );
|
||||
if( !bWriteStat && ( GetLastError() == ERROR_IO_PENDING ) ){
|
||||
if( WaitForSingleObject( m_OverlappedWrite.hEvent, 1000 ) ) dwBytesWritten = 0;
|
||||
else{
|
||||
GetOverlappedResult( m_hIDComDev, &m_OverlappedWrite, &dwBytesWritten, FALSE );
|
||||
m_OverlappedWrite.Offset += dwBytesWritten;
|
||||
}
|
||||
}
|
||||
|
||||
return( TRUE );
|
||||
|
||||
}
|
||||
|
||||
int CSerial::SendData( const char *buffer, int size )
|
||||
{
|
||||
|
||||
if( !m_bOpened || m_hIDComDev == NULL ) return( 0 );
|
||||
|
||||
DWORD dwBytesWritten = 0;
|
||||
int i;
|
||||
for( i=0; i<size; i++ ){
|
||||
WriteCommByte( buffer[i] );
|
||||
dwBytesWritten++;
|
||||
}
|
||||
|
||||
return( (int) dwBytesWritten );
|
||||
|
||||
}
|
||||
|
||||
int CSerial::ReadDataWaiting( void )
|
||||
{
|
||||
|
||||
if( !m_bOpened || m_hIDComDev == NULL ) return( 0 );
|
||||
|
||||
DWORD dwErrorFlags;
|
||||
COMSTAT ComStat;
|
||||
|
||||
ClearCommError( m_hIDComDev, &dwErrorFlags, &ComStat );
|
||||
|
||||
return( (int) ComStat.cbInQue );
|
||||
|
||||
}
|
||||
|
||||
int CSerial::ReadData( void *buffer, int limit )
|
||||
{
|
||||
|
||||
if( !m_bOpened || m_hIDComDev == NULL ) return( 0 );
|
||||
|
||||
BOOL bReadStatus;
|
||||
DWORD dwBytesRead, dwErrorFlags;
|
||||
COMSTAT ComStat;
|
||||
|
||||
ClearCommError( m_hIDComDev, &dwErrorFlags, &ComStat );
|
||||
if( !ComStat.cbInQue ) return( 0 );
|
||||
|
||||
dwBytesRead = (DWORD) ComStat.cbInQue;
|
||||
if( limit < (int) dwBytesRead ) dwBytesRead = (DWORD) limit;
|
||||
|
||||
bReadStatus = ReadFile( m_hIDComDev, buffer, dwBytesRead, &dwBytesRead, &m_OverlappedRead );
|
||||
if( !bReadStatus ){
|
||||
if( GetLastError() == ERROR_IO_PENDING ){
|
||||
WaitForSingleObject( m_OverlappedRead.hEvent, 2000 );
|
||||
return( (int) dwBytesRead );
|
||||
}
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
return( (int) dwBytesRead );
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
// Serial.h
|
||||
|
||||
#ifndef __SERIAL_H__
|
||||
#define __SERIAL_H__
|
||||
|
||||
#define FC_DTRDSR 0x01
|
||||
#define FC_RTSCTS 0x02
|
||||
#define FC_XONXOFF 0x04
|
||||
#define ASCII_BEL 0x07
|
||||
#define ASCII_BS 0x08
|
||||
#define ASCII_LF 0x0A
|
||||
#define ASCII_CR 0x0D
|
||||
#define ASCII_XON 0x11
|
||||
#define ASCII_XOFF 0x13
|
||||
|
||||
class CSerial
|
||||
{
|
||||
|
||||
public:
|
||||
CSerial();
|
||||
~CSerial();
|
||||
|
||||
BOOL Open( int nPort = 2, int nBaud = 9600 );
|
||||
BOOL Close( void );
|
||||
|
||||
int ReadData( void *, int );
|
||||
int SendData( const char *, int );
|
||||
int ReadDataWaiting( void );
|
||||
|
||||
BOOL IsOpened( void ){ return( m_bOpened ); }
|
||||
|
||||
protected:
|
||||
BOOL WriteCommByte( unsigned char );
|
||||
|
||||
HANDLE m_hIDComDev;
|
||||
OVERLAPPED m_OverlappedRead, m_OverlappedWrite;
|
||||
BOOL m_bOpened;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user