Home > Software engineering >  Made of PC MSCOMM control problem
Made of PC MSCOMM control problem

Time:09-26

Just learning computer programming, using MSCOMM control made a PC, using the timer to send data every 100 ms 55 ba 00 00 00 ff every 100 ms send 45 to 55 fb,
Received with spontaneous since closed, turn the usb serial port should be 55 55 ba fb 00 00 ff 45 00, but the received data is changed, and there will be repeated, as received a lot of ba, strives for the great god help to look at the code,
 
Void PASCAL CallBackFunc (UINT wTimerID, UINT MSG, dwords dwUser, dwords DWL, dwords dw2)
{
DLG CLinMasterNodeTestDlg *=(CLinMasterNodeTestDlg *) dwUser;

if(! DLG - & gt; M_mscomm. GetPortOpen ())
{
If (NOTopenCOMflagonce==0)
{
NOTopenCOMflagonce=1;

}
return;
}
If (stream==0)
{

DLG - & gt; M_mscomm. SetBreak (1);

The stream=1;
}
Else if (stream==1)
{



If (DLG - & gt; M_Frei. GetCheck ())
{
LIN_txdata1 |=1 & lt; <1;
}
The else
{
LIN_txdata1 & amp;=~ (1 & lt; <1);
}
If (DLG - & gt; M_Autark. GetCheck ())
{
LIN_txdata1 |=1 & lt; <0;
}
The else
{
LIN_txdata1 & amp;=~ (1 & lt; <0);
}
If (DLG - & gt; M_Vorw. GetCheck ())
{
LIN_txdata1 |=1 & lt; <2;
}
The else
{
LIN_txdata1 & amp;=~ (1 & lt; <2);
}
DLG - & gt; M_mscomm. SetBreak (0);
CByteArray array2;

Static int x=0;
x++;
If (x& 1)
{

Array2. SetSize (5);
Array2. SetAt (0, 0 x55);
T_u8 PIDresult=0;
PIDresult=IDconvertPID (58);
Array2. SetAt (1, PIDresult);
Array2. SetAt (2, LIN_txdata1);
Array2. SetAt (3, Send59dataarray [1]).
Send59dataarray [0]=LIN_txdata1;
T_u8 Enhancecheksum1=0;
Enhancecheksum1=EnhancedChecksum (Send59dataarray, 2, PIDresult);
Array2. SetAt (4, Enhancecheksum1);
DLG - & gt; M_mscomm. SetOutput (COleVariant (array2));
}
The else
{
//DLG - & gt; M_mscomm. SetOutBufferCount (0);
Array2. SetSize (2);
Array2. SetAt (0, 0 x55);
T_u8 PIDresult1=0;
PIDresult1=IDconvertPID (59);
Array2. SetAt (1, PIDresult1);
DLG - & gt; M_mscomm. SetOutput (COleVariant (array2));
Requestflag=1;
}


Delay=100;
The stream=2;
}

Else if (stream==2)
{
If (delay)
Delay -;
The else
{
The stream=0;
//while (DLG - & gt; M_mscomm. GetOutBufferCount () & gt; 5);
//DLG - & gt; M_mscomm. SetSettings (" 19200, n, 8, 1 ");
//delay=100;
}
}

}
Void CLinMasterNodeTestDlg: : OnComm ()
{
//TODO: Add your the control notification handler code here
The VARIANT variant_inp;
COleSafeArray safearray_inp;
LONG len, k;
Unsigned char rxdata [30].//set An 8-bit BYTE array integerthat is not signed.
Cstrings strtemp;
Cstrings recd;
Int arayindextemp=0;
The static char recordlength=0;
//static char streamRx=0;
Unsigned char rxarray [30].
//int rxdindex=0;
//int rxaindex=0;
//a Boolean flag=0;

The switch (m_mscomm GetCommEvent ())
{
Case 1://comEvSend send data
break;
Case 2://comEvReceive read
{
//MessageBox (_T (" read data event "), _T (" TRACE "), MB_OK);

Variant_inp=m_mscomm. GetInput ();//read buffer
Safearray_inp=variant_inp;//the VARIANT type variables into COleSafeArray type
Len=safearray_inp. GetOneDimSize ();//to get effective data length
//to accept data

//m_www=len;
//the UpdateData (0);
For (k=0; K{
//unsigned char Hexchange=0;
Safearray_inp. GetElement (& amp; K, rxarray + k);//type into a BYTE array
}
If (Requestflag==1)
{
If (recordlength<9)
{
Int templength=recordlength + len;
For (long under-16=recordlength; Under-16 & lt; Templength; Under-16 + +)
{
//safearray_inp. GetElement (& amp; Under-16 rxarray + under-16);//type into a BYTE array
Aray [under-16]=* (unsigned char *) (rxarray + (under-16 - recordlength));
Recordlength++;
Watchstr. The Format (_T (" % 02 x "), aray [under-16]);//hexadecimal display

Watchstrun +=Watchstr;

Watchstrun +="";

}
}
The else
{
Requestflag=0;
M_qqqq=Watchstrun;
The UpdateData (0);
Watchstrun="";
Recordlength=0;


}


}
break;
}

CodePudding user response:

In native serial try

USB devices, itself is the way of bulk, small data, frequency,, the efficiency is very low, may also be lost data

CodePudding user response:

Void CTabPage1: : readComm2 (CByteArray& Arr)
{
If (h_mscomm1 get_PortOpen ()) {
If (h_mscomm1 get_InBufferCount () & gt; 0)
{
H_mscomm1. Put_InputMode (1);//input binary mode
The VARIANT var1.
COleSafeArray arr1;
Var1=h_mscomm1. Get_Input ();
Arr1=var1;
DWORD len=arr1. GetOneDimSize ();
Long I=0;
For (; i{
BYTE byte_1;
Arr1. GetElement (& amp; I, & amp; Byte_1);
Arr. Add (byte_1);
}
H_mscomm1. Put_InBufferCount (0);//empty the receive buffer
}
}
}

Cstrings CTabPage1: : readComm ()
{
Cstrings str1=_T (" ");
If (h_mscomm1 get_PortOpen ()) {
If (h_mscomm1 get_InBufferCount () & gt; 0)
{
H_mscomm1. Put_InputMode (0);//input text mode
The VARIANT val=h_mscomm1. Get_Input ();
Str1=val. BstrVal;
H_mscomm1. Put_InBufferCount (0);//empty the receive buffer
}
}
Return the str1;
}

Void CTabPage1: : writeComm (CByteArray& nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related