引入ACS库,代码格式整理

This commit is contained in:
zhengxuan.zhang
2022-10-12 10:18:46 +08:00
parent 6d2b284f36
commit 82115577c2
40 changed files with 9905 additions and 3556 deletions
+188 -186
View File
@@ -14,244 +14,247 @@
/////////////////////////////////////////////////////////////////////////////
void CMMIO::Init()
{
m_RXTempPtr = NULL;
m_RXHead=NULL;
m_RXTail=NULL;
m_SXTempPtr = NULL;
m_SXHead=NULL;
m_SXTail=NULL;
CurrentPointer=0;
m_iNbMsgWaiting=0;
m_terminator='\0';
m_usesTerminator=FALSE;
m_pReceiveNotify = NULL;
m_RXTempPtr = nullptr;
m_RXHead = nullptr;
m_RXTail = nullptr;
m_SXTempPtr = nullptr;
m_SXHead = nullptr;
m_SXTail = nullptr;
CurrentPointer = 0;
m_iNbMsgWaiting = 0;
m_terminator = '\0';
m_usesTerminator = FALSE;
m_pReceiveNotify = nullptr;
}
void CMMIO::SetCallback(CMMIO::p_fstr ptr)
void CMMIO::SetCallback(p_fstr ptr)
{
m_pReceiveNotify = ptr;
m_pReceiveNotify = ptr;
}
DWORD CMMIO::Send(CString buffer, BOOL needsResponse/*=FALSE*/)
{
//ZH 12-12-05 EnterCriticalSection(&m_QueueLock); //ZH-122904
//ZH 12-12-05 EnterCriticalSection(&m_QueueLock); //ZH-122904
char LocBuffer[MAX_OUTPUT_BUFFER_SIZE];
int length = buffer.GetLength ();
if (length >MAX_OUTPUT_BUFFER_SIZE)
{
length = MAX_OUTPUT_BUFFER_SIZE;
}
char LocBuffer[MAX_OUTPUT_BUFFER_SIZE];
int length = buffer.GetLength();
if (length > MAX_OUTPUT_BUFFER_SIZE)
{
length = MAX_OUTPUT_BUFFER_SIZE;
}
unsigned short* ptr = (unsigned short*)buffer.GetBuffer (MAX_OUTPUT_BUFFER_SIZE);
auto ptr = (unsigned short*)buffer.GetBuffer(MAX_OUTPUT_BUFFER_SIZE);
for (int i=0;i<length;i++)
{
LocBuffer[i] = (char)(ptr[i] & 0xff);
}
DWORD res = Send(LocBuffer,length, needsResponse);
for (int i = 0; i < length; i++)
{
LocBuffer[i] = static_cast<char>(ptr[i] & 0xff);
}
DWORD res = Send(LocBuffer, length, needsResponse);
//ZH 12-12-05 LeaveCriticalSection( &m_QueueLock ); //ZH-122904
//ZH 12-12-05 LeaveCriticalSection( &m_QueueLock ); //ZH-122904
return res;
return res;
}
// GetNextReceived() : Helper function, rreturns receives messages placed in the queue
// by LineReceive()
//
int CMMIO::GetNextReceived(char *inputBuf)
int CMMIO::GetNextReceived(char* inputBuf)
{
struct SerialList *Free;
int cnt=0;
struct SerialList* Free;
int cnt = 0;
// If there is a previous block then delete it
if (NULL != m_RXTempPtr)
delete[] m_RXTempPtr;
m_RXTempPtr = NULL;
// If there is a previous block then delete it
if (nullptr != m_RXTempPtr)
delete[] m_RXTempPtr;
m_RXTempPtr = nullptr;
// We are messing with pointers so use the CriticalSection
// EnterCriticalSection(&m_QueueLock);
// We are messing with pointers so use the CriticalSection
// EnterCriticalSection(&m_QueueLock);
// If there any more to return
if( m_RXHead )
{
Free = m_RXHead;
m_RXHead = m_RXHead->Next;
// If there any more to return
if (m_RXHead)
{
Free = m_RXHead;
m_RXHead = m_RXHead->Next;
// Point the temp pointer at the block
m_RXTempPtr = Free->Buffer;
cnt = Free->Bytes;
// delete the list entry
delete Free;
--m_iNbMsgWaiting; // mp
// Point the temp pointer at the block
m_RXTempPtr = Free->Buffer;
cnt = Free->Bytes;
// delete the list entry
delete Free;
--m_iNbMsgWaiting; // mp
// move over the data to the user's buffer
if (NULL != inputBuf)
memcpy (inputBuf, m_RXTempPtr, cnt);
}
// move over the data to the user's buffer
if (nullptr != inputBuf)
memcpy(inputBuf, m_RXTempPtr, cnt);
}
if( m_RXHead == NULL )
m_RXTail = NULL;
if (m_RXHead == nullptr)
m_RXTail = nullptr;
// All done so out of the CriticalSection
// LeaveCriticalSection( &m_QueueLock );
// All done so out of the CriticalSection
// LeaveCriticalSection( &m_QueueLock );
return(cnt);
return (cnt);
}
int CMMIO::AddReceived( const char *Buffer,DWORD Bytes)
int CMMIO::AddReceived(const char* Buffer, DWORD Bytes)
{
DWORD index = 0; //primary buffer index
struct SerialList *Ptr;
static char Buffer2[MAX_RECIEVE_BUFFER_SIZE]; // result buffer
static char* pBuffer2 = &Buffer2[0];
unsigned char c;
bool bArmed;
/************************************************************************/
/* Greg Guilbeau - 2011/08/23 */
/* The following line(s) have been modified to handle x64 conversion */
/************************************************************************/
/* int count; */
INT_PTR count;
static int escape = 0;
static int tilde = 0;
static int tildeseqcount = 0;
DWORD index = 0; //primary buffer index
struct SerialList* Ptr;
static char Buffer2[MAX_RECIEVE_BUFFER_SIZE]; // result buffer
static char* pBuffer2 = &Buffer2[0];
unsigned char c;
bool bArmed;
/************************************************************************/
/* Greg Guilbeau - 2011/08/23 */
/* The following line(s) have been modified to handle x64 conversion */
/************************************************************************/
/* int count; */
INT_PTR count;
static int escape = 0;
static int tilde = 0;
static int tildeseqcount = 0;
bool bDone = false;
bool bEventRequest = false;
bool bDone = false;
bool bEventRequest = false;
//TRACE(_T("AddReceived> pBuffer2 = %x\n"),pBuffer2);
// TRACE(_T("Content %s\n"),Buffer);
if (FALSE /*Bytes==0*/)
{
TRACE(_T("CMMIO> Exiting , no real input"));
return TRUE;
}
do
{
bArmed = false;
//TRACE(_T("AddReceived> pBuffer2 = %x\n"),pBuffer2);
// TRACE(_T("Content %s\n"),Buffer);
if (FALSE /*Bytes==0*/)
{
TRACE(_T("CMMIO> Exiting , no real input"));
return TRUE;
}
do
{
bArmed = false;
for (;index<Bytes;index++)
{
c=Buffer[index] & 0xff;
//TRACE(_T("== %02x% ==\n"),c); //copy char one by one
*(pBuffer2++) = c ; //copy char one by one
for (; index < Bytes; index++)
{
c = Buffer[index] & 0xff;
//TRACE(_T("== %02x% ==\n"),c); //copy char one by one
*(pBuffer2++) = c; //copy char one by one
// check for end of packet. ignore if this is the last char anyways
if (m_usesTerminator && c == m_terminator && (index < Bytes - 1))
{
bArmed = TRUE;
index++;
break;
}
}
// check for end of packet. ignore if this is the last char anyways
if (m_usesTerminator && c == m_terminator && (index < Bytes - 1))
{
bArmed = TRUE;
index++;
break;
}
}
if (index == Bytes)
bArmed = bDone = true;
if (index == Bytes)
bArmed = bDone = true;
// We are messing with pointers so use the CriticalSection
if (bArmed )
{
// EnterCriticalSection(&m_QueueLock);
//Allocate a new list and add it in
count = pBuffer2-(&Buffer2[0]);
if (m_pReceiveNotify)
{ // send a string to callback or,
char* pLocalBuffer = new char[count+1];
if (pLocalBuffer)
{
memcpy (pLocalBuffer,Buffer2,count);
pLocalBuffer[count] = 0;
CString LocalStr(pLocalBuffer);
(*m_pReceiveNotify)(LocalStr);
delete pLocalBuffer;
}
}
else
{ // add it as before to received data ....
Ptr = new struct SerialList;
Ptr->Buffer = new char[count + 1 ];
/************************************************************************/
/* Greg Guilbeau - 2011/08/23 */
/* The following line(s) have been modified to handle x64 conversion */
/************************************************************************/
/* Ptr->Bytes = count; */
// We are messing with pointers so use the CriticalSection
if (bArmed)
{
// EnterCriticalSection(&m_QueueLock);
//Allocate a new list and add it in
count = pBuffer2 - (&Buffer2[0]);
if (m_pReceiveNotify)
{
// send a string to callback or,
auto pLocalBuffer = new char[count + 1];
if (pLocalBuffer)
{
memcpy(pLocalBuffer, Buffer2, count);
pLocalBuffer[count] = 0;
CString LocalStr(pLocalBuffer);
(*m_pReceiveNotify)(LocalStr);
delete pLocalBuffer;
}
}
else
{
// add it as before to received data ....
Ptr = new struct SerialList;
Ptr->Buffer = new char[count + 1];
/************************************************************************/
/* Greg Guilbeau - 2011/08/23 */
/* The following line(s) have been modified to handle x64 conversion */
/************************************************************************/
/* Ptr->Bytes = count; */
#ifdef _WIN64
Ptr->Bytes = WAI64bit::to32bit(count,__FILE__,__LINE__);
Ptr->Bytes = WAI64bit::to32bit(count,__FILE__,__LINE__);
#else
Ptr->Bytes = count,__FILE__,__LINE__;
#endif
Ptr->Next = NULL;
memcpy( Ptr->Buffer, Buffer2, count );
Ptr->Buffer[count] = 0;
Ptr->Next = nullptr;
memcpy(Ptr->Buffer, Buffer2, count);
Ptr->Buffer[count] = 0;
memcpy(m_sLastMessage,Buffer2,count); //copy to last message
m_sLastMessage[count]=0;
memcpy(m_sLastMessage, Buffer2, count); //copy to last message
m_sLastMessage[count] = 0;
if( m_RXTail )
m_RXTail->Next = Ptr;
else
m_RXHead = Ptr;
m_RXTail = Ptr;
if (m_RXTail)
m_RXTail->Next = Ptr;
else
m_RXHead = Ptr;
m_RXTail = Ptr;
// All done so out of the CriticalSection
++m_iNbMsgWaiting;
}
// LeaveCriticalSection( &m_QueueLock );
pBuffer2=&Buffer2[0]; // reset out buffer
count = 0;
bEventRequest = true;
}
}
while (!bDone);
//TRACE (_T("CMMIO> Done\n"));
// All done so out of the CriticalSection
++m_iNbMsgWaiting;
}
// LeaveCriticalSection( &m_QueueLock );
pBuffer2 = &Buffer2[0]; // reset out buffer
count = 0;
bEventRequest = true;
}
}
while (!bDone);
//TRACE (_T("CMMIO> Done\n"));
return(TRUE);
return (TRUE);
}
void CMMIO::LineReceive(char* s, int nbCharAvail, BOOL ignoreDelimiter /*= FALSE*/)
{
if (nbCharAvail != -1)
{
//TRACE(_T("LineReceive got %d chars \n"),nbCharAvail);
char c;
for (int i=0 ; i<nbCharAvail ; i++ )
{
c=s[i];
m_InputBuffer[CurrentPointer++] = c;
if (nbCharAvail != -1)
{
//TRACE(_T("LineReceive got %d chars \n"),nbCharAvail);
char c;
for (int i = 0; i < nbCharAvail; i++)
{
c = s[i];
m_InputBuffer[CurrentPointer++] = c;
// only add a packet if we have a delimiter
if ((!m_usesTerminator && i == nbCharAvail -1) || (m_usesTerminator && c == m_terminator) || ignoreDelimiter)
{
m_InputBuffer[CurrentPointer] = '\0';
AddReceived(m_InputBuffer, CurrentPointer);
CurrentPointer = 0;
}
}
}
// only add a packet if we have a delimiter
if ((!m_usesTerminator && i == nbCharAvail - 1) || (m_usesTerminator && c == m_terminator) ||
ignoreDelimiter)
{
m_InputBuffer[CurrentPointer] = '\0';
AddReceived(m_InputBuffer, CurrentPointer);
CurrentPointer = 0;
}
}
}
}
////////////////////////////////////////////////////////
DWORD CMMIO::Close()
{
struct SerialList *Free;
struct SerialList* Free;
// Delete the contents of the temp rx pointer if any
delete[] m_RXTempPtr;
m_RXTempPtr = NULL;
// Delete the contents of the temp rx pointer if any
delete[] m_RXTempPtr;
m_RXTempPtr = nullptr;
// Clear down all internal lists
// EnterCriticalSection( &m_QueueLock );
while( m_RXHead )
{
Free = m_RXHead;
m_RXHead = m_RXHead->Next;
delete[] Free->Buffer;
delete Free;
}
m_RXHead = NULL;
// Clear down all internal lists
// EnterCriticalSection( &m_QueueLock );
while (m_RXHead)
{
Free = m_RXHead;
m_RXHead = m_RXHead->Next;
delete[] Free->Buffer;
delete Free;
}
m_RXHead = nullptr;
// for now we are not using the Transmit list
// for now we are not using the Transmit list
#if 0
while( m_TXHead )
@@ -263,11 +266,10 @@ DWORD CMMIO::Close()
}
m_TXHead = NULL;
#endif
// LeaveCriticalSection( &m_QueueLock );
return(TRUE);
// LeaveCriticalSection( &m_QueueLock );
return (TRUE);
}
///////////////////////////////////////////////////////////////////////////////
// END OF BASE CLASS CMMIO
///////////////////////////////////////////////////////////////////////////////
+88 -77
View File
@@ -7,109 +7,120 @@
#define MAX_OUTPUT_BUFFER_SIZE 2048
#define MAX_RECIEVE_BUFFER_SIZE 3000
// TCP, serial style routines
struct SerialList
{
struct SerialList *Next;
DWORD Bytes;
int Item;
char *Buffer;
struct SerialList* Next;
DWORD Bytes;
int Item;
char* Buffer;
};
class CMMIO
{
private:
// For now we are not using the transmit list
// struct SerialList *m_TXHead;
char m_terminator;
BOOL m_usesTerminator;
char m_InputBuffer[MAX_RECIEVE_BUFFER_SIZE];
int CurrentPointer;
char m_sLastMessage[MAX_RECIEVE_BUFFER_SIZE];
// For now we are not using the transmit list
// struct SerialList *m_TXHead;
char m_terminator;
BOOL m_usesTerminator;
char m_InputBuffer[MAX_RECIEVE_BUFFER_SIZE];
int CurrentPointer;
char m_sLastMessage[MAX_RECIEVE_BUFFER_SIZE];
struct SerialList *m_RXHead;
struct SerialList *m_RXTail;
char *m_RXTempPtr;
CString m_LastError;
BOOL m_hasError;
struct SerialList* m_RXHead;
struct SerialList* m_RXTail;
char* m_RXTempPtr;
CString m_LastError;
BOOL m_hasError;
protected:
CString m_lastErrMsg;
int m_iNbMsgWaiting;
BOOL m_UseBuffferedSend;
CRITICAL_SECTION m_QueueLock;
CRITICAL_SECTION m_ReadLock;
CRITICAL_SECTION m_WriteLock;
CString m_lastErrMsg;
int m_iNbMsgWaiting;
BOOL m_UseBuffferedSend;
CRITICAL_SECTION m_QueueLock; //ÁÙ½çÇø
CRITICAL_SECTION m_ReadLock;
CRITICAL_SECTION m_WriteLock;
struct SerialList *m_SXHead;
struct SerialList *m_SXTail;
char *m_SXTempPtr;
struct SerialList* m_SXHead;
struct SerialList* m_SXTail;
char* m_SXTempPtr;
protected:
virtual void Init();
int AddReceived( const char *Buffer,DWORD Bytes); // [7/30/2004]
void Error(CString sErr) {
TRACE(sErr);
m_LastError = sErr;
m_hasError = TRUE;
}
virtual void Init();
int AddReceived(const char* Buffer, DWORD Bytes); // [7/30/2004]
void Error(CString sErr)
{
TRACE(sErr);
m_LastError = sErr;
m_hasError = TRUE;
}
virtual DWORD Close();
virtual DWORD Close();
public:
CMMIO()
{
InitializeCriticalSection( &m_QueueLock ); // Sean Flynn mutex needs to be initialized prior to attempting to lock otherwise it will core
InitializeCriticalSection( &m_ReadLock ); // initializing here will ensure the mutex is always initialized
InitializeCriticalSection( &m_WriteLock );
Init();
};
CMMIO()
{
InitializeCriticalSection(&m_QueueLock);
// Sean Flynn mutex needs to be initialized prior to attempting to lock otherwise it will core
InitializeCriticalSection(&m_ReadLock); // initializing here will ensure the mutex is always initialized
InitializeCriticalSection(&m_WriteLock);
Init();
};
virtual ~CMMIO()
{
DeleteCriticalSection( &m_QueueLock ); // Clean up the mutexes resources when the destructor is called.
DeleteCriticalSection( &m_ReadLock );
DeleteCriticalSection( &m_WriteLock );
};
virtual ~CMMIO()
{
DeleteCriticalSection(&m_QueueLock); // Clean up the mutexes resources when the destructor is called.
DeleteCriticalSection(&m_ReadLock);
DeleteCriticalSection(&m_WriteLock);
};
virtual DWORD Open() {
return 0;
}
virtual DWORD Open()
{
return 0;
}
BOOL getError(CString& sErr) {
BOOL hasError = m_hasError;
if (m_hasError)
{
sErr = m_LastError;
m_hasError = FALSE;
}
return (hasError);
}
BOOL getError(CString& sErr)
{
BOOL hasError = m_hasError;
if (m_hasError)
{
sErr = m_LastError;
m_hasError = FALSE;
}
return (hasError);
}
virtual int GetNextReceived(char *inputBuf=NULL);
virtual int GetNextReceived(char* inputBuf = nullptr);
virtual int HasNextReceived(void) {
return (m_RXHead ? m_RXHead->Bytes : FALSE);
}
virtual void SetTerminateChar(char ch) {
m_terminator=ch;
m_usesTerminator=TRUE;
}
virtual void LineReceive(char* s, int nbCharAvail, BOOL ignoreDelimiter = FALSE);
virtual CString GetLastErrStr() {
return m_lastErrMsg;
}
virtual int HasNextReceived(void)
{
return (m_RXHead ? m_RXHead->Bytes : FALSE);
}
typedef void(*p_fstr)(CString);
void SetCallback(p_fstr);
virtual void SetTerminateChar(char ch)
{
m_terminator = ch;
m_usesTerminator = TRUE;
}
virtual DWORD Send(LPCSTR buffer, int l, BOOL needsResponse=FALSE) = 0; // [8/11/2004]
virtual DWORD Send(CString buffer, BOOL needsResponse=FALSE); // [8/11/2004]
virtual void LineReceive(char* s, int nbCharAvail, BOOL ignoreDelimiter = FALSE);
virtual bool IsValid() = 0; // pure virtual function
virtual CString GetLastErrStr()
{
return m_lastErrMsg;
}
using p_fstr = void(*)(CString);
void SetCallback(p_fstr);
virtual DWORD Send(LPCSTR buffer, int l, BOOL needsResponse = FALSE) = 0; // [8/11/2004]
virtual DWORD Send(CString buffer, BOOL needsResponse = FALSE); // [8/11/2004]
virtual bool IsValid() = 0; // pure virtual function
protected:
p_fstr m_pReceiveNotify;
p_fstr m_pReceiveNotify;
};
#endif // CMMIO_BASE_H
#endif // CMMIO_BASE_H
File diff suppressed because it is too large Load Diff
+126 -126
View File
@@ -31,166 +31,166 @@ 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;
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;
const int CS_HANDSHAKE_FOR_TRESASTR_E = 6;
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;
const int CS_HANDSHAKE_FOR_TRESASTR_E = 6;
///////////////////////////////////////////////////////////////////////////////
// Debug constants
const int CS_DEBUG_SIZE = 1024;
const int CS_DEBUG_SIZE = 1024;
///////////////////////////////////////////////////////////////////////////////
// Timeouts (ms)
const int CS_DEFAULT_RX_TIMEOUT = 25;
const int CS_DEFAULT_TX_TIMEOUT = 1000;
const int CS_DEFAULT_RX_TIMEOUT = 25;
const int CS_DEFAULT_TX_TIMEOUT = 1000;
///////////////////////////////////////////////////////////////////////////////
// The CPSerial class
class CPSerial : public CMMIO
{
// Construction
// Construction
public:
CPSerial();
CPSerial();
// Implementation
// Implementation
public:
virtual ~CPSerial();
~CPSerial() override;
// Attributes
// Attributes
public:
// Operations
// 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 );
DWORD SendWriteFile(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 );
// Open the serial port ( if possible ) using the parameters set by SetPort()
DWORD Open() override;
// 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() override
{
return IsOpen() != 0;
}
virtual DWORD Send(LPCSTR buffer, int l, BOOL needsResponse=FALSE);
//virtual DWORD Send(CString what);
int IsOpen(void);
// Close the serial port
DWORD Close(void) override;
// 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);
DWORD SendWriteFile(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);
// 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 );
DWORD Send(LPCSTR buffer, int l, BOOL needsResponse = FALSE) override;
//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);
int ReadBlock(BYTE *abIn, int MaxLength);
// Attributes
// 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);
int ReadBlock(BYTE* abIn, int MaxLength);
// Attributes
protected:
int m_RXTimeout;
int m_TXTimeout;
int m_RXTimeout;
int m_TXTimeout;
// Attributes
// 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;
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);
friend unsigned int WINAPI CSerialTask(LPVOID CSerialPtr);
//Buffer for incomming commands:
int CurrentPointer;
BOOL m_IsWrtingData;
//Buffer for incomming commands:
int CurrentPointer;
BOOL m_IsWrtingData;
public:
HANDLE m_hWaitCMMResponse;
HANDLE m_hNewRx;
HANDLE m_hWaitCMMResponse;
HANDLE m_hNewRx;
// BV Nov 2001: Made public
// CRITICAL_SECTION m_QueueLock;
BOOL m_iRecvState;
INT m_iRecvBytes;
INT m_iRecvCount;
unsigned char m_RecvData[MAX_RECIEVE_BUFFER_SIZE];
// BV Nov 2001: Made public
// CRITICAL_SECTION m_QueueLock;
BOOL m_iRecvState;
INT m_iRecvBytes;
INT m_iRecvCount;
unsigned char m_RecvData[MAX_RECIEVE_BUFFER_SIZE];
};
#endif // CMMIO_SERIAL_H
#endif // CMMIO_SERIAL_H