Menu

SPCOMM for MFC v3(vc++) serial port class

1.Use nullptr instead of NULL
If you do not support nullptr pointer, use the macro definition

#define nullptr NULL

2.Updated InputLen (v2 version InBufferSize), support openning port to change the accepted length. Changes will automatically open the serial port to perform ClearInBuffer () method

3.Add InBufferSize attributes used to set the receive buffer size, is valid only when the serial port is opened

4.Join the clean-up receive buffer method, ClearInBuffer (), only open the serial

5.To join the communications timeout event OnCommTimeOut, send completed (use WriteCommData ()) exceeds the set time CommTimeOut (in ms) does not receive any trigger

The structure of the event function

void CALLBACK OnCommTimeOut(void *pObj)
{
}

6.Modify OnReceiveData callback function.

typedef void (CALLBACK TReceiveDataEvent)(void pObj, void *pBuffer, WORD BufferLength, bool Abnormal);

You can know whether normal reception by the Abnormal,such as to achieve set length of InputLen,return false; timeout receiver will return true.The structure of the event function

void CALLBACK OnReceiveData(void pObj,void Buffer, WORD BufferLength, bool Abnormal)
{
}

7.Declaration
(1) It can be statically declared.Static variables will be released with the main form do not have to worry about the memory leak problem.
TComm RefreshIO;
RefreshIO.Create(this);
RefreshIO.CommName = L"COM3";
RefreshIO.BaudRate = 9600;
RefreshIO.ByteSize = TComm::_7;
RefreshIO.Parity = TComm::Even;
RefreshIO.StopBits = TComm::_1;
RefreshIO.OnReceiveData = RefreshReceiveData;
RefreshIO.OnCommTimeOut = RefreshCommTimeOut;
RefreshIO.ReadIntervalTimeout = 50;
RefreshIO.CommTimeOut = 100;
RefreshIO.InputLen = 2;
RefreshIO.StartComm();

(2) Dynamic declaring variables
TComm *Serial;
Serial = new TComm(this);
Serial->CommName = L"COM3";
Serial->BaudRate = 9600;
Serial->ByteSize = TComm::_8;
Serial->Parity = TComm::None;
Serial->StopBits = TComm::_1;
Serial->OnReceiveData = ReceiveData;
Serial->ReadIntervalTimeout = 50;
Serial->StartComm();

The variables release, need to perform delete
delete Serial;

8 supports more than COM10 serial port (v2 support)

  1. Add IsOpen () function returns true is already open (v2 support)

10 append additional properties tab int Tag; the void * pTag; facilitate the dynamic establishment of multi-serial port (v2 support)

11 the debug error detection function to further strengthen

Other description of the event

  1. OnSendDataEmpty: triggered after the end of transmission

  2. OnReceiveError: receiving data format error trigger

  3. RequestHangup: Communication is a serious error, the receiver or the end of the thread to terminate.

Program example
#include "Spcomm.h"
char rev[100];
TComm Serial;
char rev[100];
void CALLBACK ReceiveData(void obj,void *Buffer, WORD BufferLength, bool Abnormal)
{
memcpy_s(rev,100,Buffer,BufferLength)
}

void SPCommDlg::OnBnClickedButton1()
{
Serial = new TComm(this);
Serial->CommName = L"COM3";
Serial->BaudRate = 9600;
Serial->ByteSize = TComm::_8;
Serial->Parity = TComm::None;
Serial->StopBits = TComm::_1;
Serial->OnReceiveData = ReceiveData;
Serial->ReadIntervalTimeout = 50;
Serial->StartComm();
}

void SPCommDlg::OnBnClickedButton2()
{
char bb[8];
bb[0]= 0x02;
bb[1]= 'E';
bb[2]= '0';
bb[3]= '0';
bb[4]= '4';
bb[5]= '0';
bb[6]= '0';
bb[7]= '0';
Serial->WriteCommData(bb,8);
}

Posted by b1ackn3k0 2013-01-20 Labels: spcomm mfc serial port vc++

Log in to post a comment.