Files
EF3-Interface/PcDmis/Base/Interfac/Msi/Hsi/SevenOcean/CMMIO_SERIAL.H
T
2013-05-09 20:29:54 +08:00

192 lines
5.8 KiB
C++

#ifndef CMMIO_SERIAL_H
#define CMMIO_SERIAL_H
#include <WinBase.h>
#include <WinDef.h>
#include "CMMIO_BASE.H "
////////////////////////////////////////////////////////////////////////////////
/*
*$!!***************************************************************************
*$!! MODULE NAME
*$!! Serial.h
*$!!
*$!! DESCRIPTION
*$!! Header file for CSerial.
*$!!
*$!! AUTHOR
*$!! M.J.S.Gooder.
*$!!
*$!! HISTORY
*$!! $Log: /Plugins/BPI/Serial.h $
*$!!
*$!! 28 19/03/98 14:04 Mgooder
*$!!***************************************************************************
*/
///////////////////////////////////////////////////////////////////////////////
// Function for starting serial monitor thread
unsigned int WINAPI CSerialTask(LPVOID CSerialPtr);
///////////////////////////////////////////////////////////////////////////////
// Useful serial codes
const char CS_NUL = 0;
const char CS_SOH = 1;
const char CS_STX = 2;
const char CS_ETX = 3;
const char CS_EOT = 4;
const char CS_ENQ = 5;
const char CS_ACK = 6;
const char CS_BEL = 7;
const char CS_LF = 10;
const char CS_DLE = 16;
const char CS_X_ON = 17;
const char CS_X_OFF = 19;
const char CS_NAK = 21;
const char CS_ETB = 23;
const char CS_CAN = 24;
const char CS_ESC = 27;
///////////////////////////////////////////////////////////////////////////////
// Handshake settings
const int CS_HANDSHAKE_RTSCTS = 1;
const int CS_HANDSHAKE_XONXOFF = 2;
const int CS_HANDSHAKE_NONE = 3;
const int CS_HANDSHAKE_RTSCTS_DTRDSR = 4;
const int CS_HANDSHAKE_FOR_SO7 = 5;
///////////////////////////////////////////////////////////////////////////////
// Debug constants
const int CS_DEBUG_SIZE = 1024;
///////////////////////////////////////////////////////////////////////////////
// Timeouts (ms)
const int CS_DEFAULT_RX_TIMEOUT = 25;
const int CS_DEFAULT_TX_TIMEOUT = 1000;
///////////////////////////////////////////////////////////////////////////////
// The CSerial class
class CSerial : public CMMIO
{
// Construction
public:
CSerial();
// Implementation
public:
virtual ~CSerial();
// Attributes
public:
// Operations
public:
// Open the serial port ( if possible ) using the parameters set by SetPort()
DWORD Open();
// Setup the serial port prior to opening it
int SetPort( int Port, int Baud = 0, char Parity = 0, int Bits = 0,
int StopBits = 0, int HandShake = 0 );
// Set up the port timeouts
void SetTimeouts( int RXTimeout = CS_DEFAULT_RX_TIMEOUT,
int TXTimeout = CS_DEFAULT_TX_TIMEOUT );
// Get the serial port settings
void GetPortData( int *Port, int *Baud, char *Parity, int *Bits,
int *StopBits, int *HandShake );
// Test if the serial port is open
bool IsValid() {
return IsOpen()!=0;
}
int IsOpen( void );
// Close the serial port
DWORD Close( void );
// Flush all data in any serial port buffers
int FlushPort( void );
// Attempt to read the specified number of bytes
DWORD ReadPort( char *Buffer, DWORD Bytes );
DWORD ReadPort( CString &Buffer, DWORD Bytes );
// Attempt to write the specified number of bytes
DWORD WritePort( const char *Buffer, DWORD Bytes );
// Send the specifed number of bytes out when possible
int Transmit( const char *Buffer, DWORD Bytes );
// Add a block of data to the internal list of receieved blocks
int AddReceived( const char *Buffer, DWORD Bytes );
// Get the next block of data added with AddReceived()
// char *GetNextReceived( void );
// Find the maximum port number available
int MaxPort( void );
// Add text to the debug output
void AddToDebug( const char *Ptr, DWORD BytesToCopy, int State );
// Attach this serial port to a debug window
//CSerialRaw *AttachWnd( CWnd *Wnd );
// Convert ascii hex into an int
int HexToInt( char *Data, int Bytes );
virtual DWORD Send(LPCSTR buffer, int l, BOOL needsResponse=FALSE);
//virtual DWORD Send(CString what);
// Called when data is received on the serial port
virtual void OnReceive( void );
// Called when data has been sent using the Transmit() function
virtual void OnTransmit( int Item, DWORD Error );
private:
// Private so no comment :-)
void RegisterDebugWindow(void);
void SendBuffer(int Next);
void ReceiveTask(void);
int ProgramPort(int Port,int Baud,char Parity,int Bits,int StopBits,int HandShake);
// Attributes
protected:
int m_RXTimeout;
int m_TXTimeout;
// Attributes
private:
char m_Parity;
// char m_DebugData[CS_DEBUG_SIZE];
// char m_DebugState[CS_DEBUG_SIZE];
// char *m_RXTempPtr;
int m_Port;
int m_Baud;
int m_Bits;
int m_StopBits;
int m_HandShake;
int m_MaxTXRetries;
int m_DebugInPtr;
int m_DebugCount;
int m_Item;
// struct SerialList *m_TXHead;
// struct SerialList *m_TXTail;
// struct SerialList *m_RXHead;
// struct SerialList *m_RXTail;
OVERLAPPED m_ReceiveOLap;
OVERLAPPED m_TransmitOLap;
OVERLAPPED m_ReadOLap;
OVERLAPPED m_WriteOLap;
HANDLE m_PortHandle;
HANDLE m_ThreadHandle;
HANDLE m_ReceiveTaskEvent;
private:
friend unsigned int WINAPI CSerialTask(LPVOID CSerialPtr);
//Buffer for incomming commands:
int CurrentPointer;
public:
HANDLE m_hWaitCMMResponse;
HANDLE m_hNewRx;
// BV Nov 2001: Made public
// CRITICAL_SECTION m_QueueLock;
BOOL m_iRecvState;
CString m_csRecv;
INT m_iRecvByte;
};
#endif // CMMIO_SERIAL_H