Home > Back-end >  Help: communication MScomm serial interface control problem!
Help: communication MScomm serial interface control problem!

Time:09-18

Has always been a bottom, the machine program, learning c + + Builder serial port programming, using MScomm serial port control sending the message, as follows:
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Void __fastcall TForm1: : Btn_SendClick (TObject * Sender)//button to send
{
int i;
Int iSendCnt;//send the packet number of bytes
OleVariant send;
//AnsiString STR.
//char * buff;

Int pc_send [5]={0 x01, 0 x31, 0 x00 , 0 x35, 0 x9b};//PC packets from
The send=VarArrayCreate (OPENARRAY (int, (0, 5-1)), varByte);//create a dynamic array
for(i=0; I & lt; 5; I++)
{
Send. PutElement (pc_send [I], I);//fill sent data element
}
MSComm1 - & gt; The Output=send;//from the serial port to send
}
1, this program can be sent, but have a question: if pc_send with 0 x00 in [5] value, lower machine pick what to what, return
Below 0 x00 have no, I don't know is PC didn't sent out, or under a machine not received?
2, in addition, as long as pc_send [5] array elements are & gt; 0, can return to normal, with other serial port software under test machine, also is normal,
This judgment, PC didn't send!!!!! Don't "send PutElement (pc_send [I], I);//fill sent data element ", this has a problem?

CodePudding user response:

Serial communication program, it is recommended that you install a Eltima product Serial Port Monitor, used to Monitor and simulate a Serial Port to send and receive data, look at the data from the upper machine whether you expected, and then analysis,

CodePudding user response:

I use MSCOMM serial port, I have compiled a small project, is to use event read

Void __fastcall TForm1: : MSComm1Comm (TObject * Sender)
{//serial port using asynchronous communication, at the same time according to different port charge -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
TMSComm * mscomm1=(TMSComm *) Sender;
Int port=mscomm1 - & gt; CommPort;
TChannel * c=Channels [port];
OleVariant buf0=mscomm1 - & gt; The Input;
if (! C - & gt; IsIdle)
{
BYTE * buf1=c - & gt; Buf.
Int cn=buf0. ArrayHighBound () + 1;
for (int i=0; IBuf1 [c - & gt; bufLen++]=buf0. GetElement (I);
If (c - & gt; ChCount!=0 & amp; & C - & gt; BufLen>=c - & gt; ChCount)
{//buy some state
C - & gt; BufLen=c - & gt; ChCount;
Points [c - & gt; pointIdx] - & gt; DataStat=3;//finish, can handle the
Points [c - & gt; pointIdx] - & gt; DataLen=c - & gt; BufLen;
Points [c - & gt; pointIdx] - & gt; DataTick1=GetTickCount ();
C - & gt; IsIdle=true;//release channel
}
}
}
  • Related