Home > Back-end >  Comm port problems help have a look at the code
Comm port problems help have a look at the code

Time:10-01

The unit MainUnit;

Interface

USES the
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, OleCtrls MSCommLib_TLB;

Type
TForm1=class (TForm)
Comm: TMSComm;
Procedure CommComm (Sender: TObject);
Procedure FormCreate (Sender: TObject);
Private
{Private declarations}
Public
Procedure initComm;
end;

TScanComData=https://bbs.csdn.net/topics/class (TThread)
Private
Protected
Procedure the Execute; Override.
Public
The constructor Create;
The destructor Destroy; Override.
end;


Var
Form1: TForm1;
ScanComData: TScanComData;


Implementation

{$R *. DFM}

{TScanComData}

The constructor TScanComData. Create;
The begin
Inherited the Create (True);
FreeOnTerminate:=True;
end;

Destructor TScanComData. Destroy;
The begin
Inherited Destroy;
end;

Procedure TScanComData. Execute;
Var
Ole: Olevariant;
I: Integer;
The begin
//while True do
//the begin
Form1. Initcomm;
Form1.com m.R Threshold:=7;//23;
If Form1.com m.P ortOpen=false then
Form1.com m.P ortOpen:=true;
Form1.com m. TREnable:=true;
Form1.com m.R TSEnable:=true;
//ReadComCount:=0;
//bIfSend:=True;
Ole:=VarArrayCreate ([0, 7], varByte);
Ole [0] :=$02;
Ole [1] :=$01;
Ole [2] :=$8;
Ole [3] :=$00;
Ole [4] :=$00;
Ole [5] :=$02;
Ole [6] :=$00;
Ole [7] :=0;
For I:=0 to 6 do
Ole [7] :=[7] xor ole ole [I];
//ole [7] :=$9;
//Self. Caption:=IntToStr (ole [7]);
Form1.com m.O utput:=ole;
//end;
end;

{TForm1}

Procedure TForm1.Com mComm (Sender: TObject);
Var
Ole: Olevariant;
STR: String;
I: Integer;
The begin
If mEvent=2 (Comm.Com) then
The begin
//read the card number (scan)
If Comm. RThreshold=7 then
The begin
Ole:=Comm. Input;
For I:=0 to Comm. Do RThreshold - 1
The begin
STR:=STR + ', '+ IntToHex (ole [I], 2);
end;
Self. Caption:=String (STR);
end;
end;
end;

Procedure TForm1. FormCreate (Sender: TObject);
The begin
ScanComData:=TScanComData. Create;
ScanComData. Resume;
end;

Procedure TForm1. InitComm;
The begin
If comm. PortOpen=true then
Comm. PortOpen:=false;
Comm. Settings:='38400, n, 8, 1;
Comm. InBufferCount:=0;//to empty buffer
Comm. OutBufferCount:=0;
Comm. InputLen:=0;
Comm.Com mPort:=2;
end;

End.

Why not go to commcomm ah should trigger this event!

CodePudding user response:

How no one? Have expert help look at ah!

CodePudding user response:

What are each function for you also do not comment

CodePudding user response:

Do not use thread, you can only have this thread under the serial port to send data, then use a serial port debug program data, see you have not return data,
If you want to use thread, put on the thread all serial operation, dynamically create a serial port in the thread, send and receive data, and then through the message to inform the main thread,
There will be a serial port control suggested CPort, Spcomm

CodePudding user response:

MSComm has the properties of a data length, forgot to call what name, is used to trigger the Oncomm event,
Check whether the attribute set has a problem

CodePudding user response:

Suggest using spcomm

CodePudding user response:

CodePudding user response:

The
reference 4 floor seleron reply:
MSComm has the properties of a data length, forgot to call what name, is used to trigger the Oncomm event,
Check whether the attribute set has a problem


BufferLength

Check this property is set, isn't it too much
  • Related