Home > Back-end >  Why can only accept a Delphi Spcomm serial interface communication data?
Why can only accept a Delphi Spcomm serial interface communication data?

Time:09-16

Why can only accept a Delphi Spcomm serial interface communication data?
Problem description:
Under a machine sends a Duan Zhongwen string, 51 single-chip microcomputer with serial debugging assistant program (PC), can see a line of Chinese continuously collected
Proof: 51 microcontroller serial program is normal, the PC serial port communication and normal

I'm in Delphi programs:
Procedure SpCommInit ();//com3 what baud rate, data bits are set and serial debugging assistants as
The begin
COM3 form1.Comm1.Com mName:=' ';
Form1.Com m1. BaudRate:=4800;
Form1.Com m1. Parity:=None;
Form1.Com m1. ByteSize:=_8;
Form1.Com m1. StopBits:=_1;
Form1.com m1. ReadIntervalTimeout:=100;
Form1.Com m1. StartComm;
//form1.Com m1. Inx_XonXoffFlow:=False;
//form1.Com m1. Outx_XonXoffFlow:=False;
end;

Procedure TForm1. BitBtn1Click (Sender: TObject);//set the button of a connection Com, namely connection Com3 button automatic failure
The begin
SpCommInit ();
Form1. BitBtn1. Enabled:=false;
end;

Procedure TForm1.Com m1ReceiveData (Sender: TObject; Buffer: Pointer;//Comm1ReceiveData event to get a phone number//according to preach, and appended to the Memo
BufferLength: Word);

Var
Receivedata: an array of byte;
STR: string;
The begin
//sleep (100);//wait for 100 ms, ensure to receive all data
SetLength (STR, BufferLength);
Move (Buffer ^, pchar (@ STR [1]) ^, BufferLength);//copy the data from the buffer out
Form1. Memo1. Lines. The Add (STR);
end;
////////////////the question now is, after I press a button, Memo sometimes show a whole article pass under a machine to complete string, sometimes is half of the incomplete string
///sometimes simply don't show
///but the trouble is Comm1ReceiveData event triggered only once, what also have no, microcontroller continued to upload data, but is can't see the triggering event produce any change in the Memo, hope you can give to solve

CodePudding user response:

A pair of serial port has been simulated with virtual serial port, and then open a serial port (COM2) write data, program open COM1 receiving, your code to run normally,
You can use the portMon tool, monitoring equipment is not normal to send data,

CodePudding user response:

Form1.com m1. ReadIntervalTimeout:=100;
Put this line commented out

CodePudding user response:

To ensure that equipment normal communication; ReadIntervalTimeout set point

CodePudding user response:

Try sleep (100), 100 to 200300400
No. Best Comm1ReceiveData (receive), open a thread to read data manually, can judge the buffer to receive data (such as the data length, frame marker, etc.), and then read

CodePudding user response:

According to the limited experience, discuss with you:
The begin
//sleep (100);//wait for 100 ms, ensure to receive all data=======this bear in mind that do not use
SetLength (STR, BufferLength);===========
Move (Buffer ^, pchar (@ STR [1]) ^, BufferLength);//copy the data from the buffer out
Form1. Memo1. Lines. The Add (STR);===========more than three lines, should not appear in the event, you should write a procedure or function, the buffer can be transmitted in the past, to write this type of code in the triggering event, extremely easy to cause you now this effect
end;
  • Related