Home > Back-end >  Delphi about serial communication error
Delphi about serial communication error

Time:10-02

Is to use SPCOMM controls for a serial port communication, but always quote us access violation when running the program at the address in the module such mistakes, don't know what's going on? Single-step operation because I don't understand, so you couldn't see the problem in where, can master to solve, a serial port for the first time I do this thing, also is not very familiar with! Thank you, and that's my code

The unit Unit1;

Interface

USES the
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, SPComm StdCtrls;

Type
TForm1=class (TForm)
For: TButton;
Button2: TButton;
Comm1: TComm;
Memo1: TMemo;
Procedure Button2Click (Sender: TObject);
Procedure Button1Click (Sender: TObject);
Private
{Private declarations}
Public
{Public declarations}
The end;

Var
Form1: TForm1;
Comm1: TComm;
Memo1: TMemo;
Viewstring: string;
I: integer;
Rbuf, sbuf: an array of byte [1.. 256];

Implementation

{$R *. DFM}





Procedure senddata publishes the event;
Var
I: integer;
Commflg: Boolean;
The begin
Viewstring:=';
Commflg:=true;
For I:=1 to 6 do
The begin
If not comm1. Writecommdata (@ sbuf [I], 1) then
The begin
Commflg:=false;
break;
The end;
//send time delay between the bytes
Sleep (2);
Viewstring:=viewstring + inttohex (sbuf [I], 2) + ";
The end;
Viewstring:='send' + viewstring;
Memo1. Lines. The add (viewstring);
Memo1. Lines. The add (");
If not commflg then messagedlg (' failure ', mterror, [mbyes], 0).
The end;


Procedure TForm1. Button2Click (Sender: TObject);
The begin
Sbuf [1] :=byte ($f0);//frame head
Sbuf [2] :=byte ($01); No.//command
Sbuf [3] :=byte ($ff);
Sbuf [4] :=byte ($ff);
Sbuf [5] :=byte ($01);
Sbuf [6] :=byte ($f0);//frame tail
Senddata publishes the event;//send a function called
The end;

Procedure TForm1. Button1Click (Sender: TObject);
The begin
Comm1. StopComm;
Comm1. StartComm;
The end;

End.
Just a small example

CodePudding user response:

The code itself is no problem. Want to see other places!

CodePudding user response:

This error is often a pointer not allocate memory or cross-border access,

CodePudding user response:

You see a serial port is already occupied by other program to open

CodePudding user response:

refer to the second floor JohnYale response:
appear this kind of mistake is often a pointer not allocate memory or cross-border access

Have all sorts of situation well, the original poster to solve?

CodePudding user response:

Estimation is a serial port to take up?
Turn off other programs with serial port

CodePudding user response:

Install a Eurekalog 7.0, can locate memory error function, is very convenient,
  • Related