Home > Software engineering >  MFC programming, serial communication, data read MSCOMM serial buffer failed, the VARIANT InputData.
MFC programming, serial communication, data read MSCOMM serial buffer failed, the VARIANT InputData.

Time:09-26


/* * * * * * * * * * * * * * * the following is the serial code * * * * * * * * * * * * * * * * * * * * * */
Void C serial communication Dlg: : OnBnClickedButtonOpen ()
{
Cstrings STR, str1, n.
GetDlgItemText (IDC_BUTTON_OPEN, STR);
The CWnd * h1.
H1=GetDlgItem (IDC_BUTTON_OPEN);

if (! M_mscom. GetPortOpen ())
{
M_mscom. SetCommPort (m_comb1. GetCurSel () + 1);//select serial port
if (! M_mscom. GetPortOpen ())
M_mscom. SetPortOpen (TRUE);//open the serial port
The else
Always open a MessageBox (_T (" serial port "));
M_comb2. GetLBText (m_comb2. GetCurSel (), str1);//choose the baud rate of the information and put it in to the str1
Str1=str1 + ', '+' n '+', '+' 8 '+', '+' 1 './/this sentence is very key
M_mscom. SetSettings (str1);//set the baud rate
M_mscom. SetInputMode (1);//set the input mode 1 for binary input mode
M_mscom. SetRThreshold (1);//buffer "=1 characters triggering event
M_mscom. SetInputLen (0);//set the current receive buffer data length of 1024
//m_mscom. SetRTSEnable (1);//set the RT allows
If (m_mscom GetPortOpen ())
{
STR="close serial port";
The UpdateData (true);
H1 - & gt; SetWindowText (STR);
}
Unsigned char InstStartRs [14]={238, 170, 01, 0, 01, 02, 0, 0, 01, 0, 0, 0, 0, 187};
SendDataToCom (InstStartRs);
M_mscom. GetInput ();//to proofread the first buffer to remove residual data
}
The else
{
M_mscom. SetPortOpen (false);
If (STR!="open the serial port")
{
STR="open the serial port";
The UpdateData (true);
H1 - & gt; SetWindowText (STR);
}
}
}
/* * * * * * * * * * * * * * * * * send data code * * * * * * * * * * * * * * * * * * * * * * * * */
Void C serial communication Dlg: : OnBnClickedButtonSend ()//send data button
{
//TODO: add the control notification handler code
The UpdateData (true);
M_mscom. SetOutput (COleVariant (m_Editsend));
}

/* * * * * * * * * * * * * * * * * * * * * * * to receive data trigger OnCommMscomm1 () event * * * * * * * * * * * * * * * * * * * * * * */
Void C serial communication Dlg: : OnCommMscomm1 ()
{
//TODO: add message handler code here
The VARIANT InputData;
COleSafeArray fs.
BYTE rxdata [1024]={0};
Long len, k;
If (m_mscom GetCommEvent ()==2)
{
InputData=(https://bbs.csdn.net/topics/m_mscom.GetInput);//read buffer
Fs=InputData;//the VARIANT type variable converting a quantity COleSafeArray type variable
Len=fs. GetOneDimSize ();
For (k=0; k Fs. GetElement (& amp; K, rxdata + k);//type into a BYTE array
BYTE bt;

For (k=0; k {
Bt=* (rxdata + k);
OnComByte (bt);
}

The UpdateData (false);
}
}


Void C serial communication Dlg: : OnComByte (byte data)
{
If (data=0 xee https://bbs.csdn.net/topics/=&& End_RData_Flag)
{
HeadFlag=true;//file header received
M_nRxDataLength=0;
M_strRXDataCOM [0]=data & amp; 0 XFF.
End_RData_Flag=false;
}
Else if (HeadFlag & amp; & Data=0 XFF)
https://bbs.csdn.net/topics/={
Start_Rece_Flag=true;//can receive data
M_strRXDataCOM [1]=data & amp; 0 XFF.
M_nRxDataLength=2;
HeadFlag=false;
}
Else if (Start_Rece_Flag & amp; & (! HeadFlag))
{
M_strRXDataCOM [m_nRxDataLength]=data & amp; 0 XFF.
M_nRxDataLength + +;
}
If (m_nRxDataLength==18)
{
End_RData_Flag=true;
M_nRxDataLength=0;
HeadFlag=false;
Start_Rece_Flag=false;
START_FLAG=true;
}
}


Void C serial communication Dlg: : SendDataToCom (unsigned char InstSend [14])
{
CByteArray hexdata;
For (int k=0; k <=14; K++)
Hexdata. Add (InstSend [k]);
Hexdata. SetSize (14);

M_mscom. SetOutput (COleVariant (hexdata));//send the hexadecimal data
}

CodePudding user response:

Who is a great god used MSCOMM serial communication trouble to guide this to me! thank you

CodePudding user response:

It is recommended to use serial debugging assistant,

CodePudding user response:

Hello, the problem solved, can say about how to solve

CodePudding user response:

BYTE rxdata [1024]
For (k=0; k Fs. GetElement (& amp; K, rxdata + k);

A cross-border problems when len is greater than 1024

CodePudding user response:

Big brother, can you tell me the serial port you accept data that part of the solution,,, how to solve ah,, refer to

CodePudding user response:

Str1=str1 + ', '+' n '+', '+' 8 '+', '+' 1 './/what is the effect? What a great god give a reply

CodePudding user response:

This sentence is: baud rate parameter combination

CodePudding user response:

Generally if use virtual serial port debugging can accept you, went down a machine's serial port driver in a new, usually drive problem

CodePudding user response:

The building Lord, please solve the problem? Meet the same problems getinput receive function is out of the news

CodePudding user response:

This kind of things are on your own debugging,
First separation problem: first use virtual serial port debugging PC program, and then debugging the machine program, look at the problem in where,

CodePudding user response:

InputData=(https://bbs.csdn.net/topics/m_mscom.GetInput);//read buffer,
Should be a com error occurred here, you are not using the try catch, submitted to the superior to the error handling, so you the following code is not in the execution of

Can you use the try catch see capture the error

CodePudding user response:

The guest officer with see chicken peck rice tutorial

CodePudding user response:

Said met the same problem, I took the two USB serial port, but I just for a serial port, change the serial port can receive, but download serial assistant two serial port can be normal receiving data,

CodePudding user response:

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related