给我写一段串口发送接受的delphi代码
时间: 2024-05-25 13:19:39 浏览: 10
好的,以下是一段串口发送接收的Delphi代码:
unit SerialCommunication;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Memo1: TMemo;
Edit1: TEdit;
Label1: TLabel;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
hComm: THandle;
DCB: TDCB;
TimeOuts: TCommTimeouts;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
begin
hComm := 0;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Port: string;
Baud: Integer;
ComStat: TComStat;
BytesRead: DWORD;
Str: string;
begin
Port := 'COM1';
Baud := 9600;
hComm := CreateFile(PChar(Port), GENERIC_READ or GENERIC_WRITE, 0, nil,
OPEN_EXISTING, 0, 0);
if hComm = INVALID_HANDLE_VALUE then
ShowMessage('无法打开串口')
else
begin
DCB.DCBlength := SizeOf(DCB);
GetCommState(hComm, DCB);
DCB.BaudRate := Baud;
DCB.ByteSize := 8;
DCB.Parity := NOPARITY;
DCB.StopBits := ONESTOPBIT;
SetCommState(hComm, DCB);
TimeOuts.ReadIntervalTimeout := 50;
TimeOuts.ReadTotalTimeoutMultiplier := 10;
TimeOuts.ReadTotalTimeoutConstant := 100;
TimeOuts.WriteTotalTimeoutMultiplier := 10;
TimeOuts.WriteTotalTimeoutConstant := 100;
SetCommTimeouts(hComm, TimeOuts);
ClearCommError(hComm, DWORD(nil^), @ComStat);
Str := Format('串口已打开,波特率:%d,数据位:%d,停止位:%d,奇偶校验:%d',
[Baud, DCB.ByteSize, DCB.StopBits, DCB.Parity]);
Memo1.Lines.Add(Str);
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
Str: string;
BytesWritten, BytesRead: DWORD;
Buff: array[0..1023] of Char;
begin
if hComm <> 0 then
begin
Str := Edit1.Text;
if Length(Str) > 0 then
begin
WriteFile(hComm, Str[1], Length(Str), BytesWritten, nil);
Sleep(20);
ReadFile(hComm, Buff, SizeOf(Buff), BytesRead, nil);
Buff[BytesRead] := #0;
Memo1.Lines.Add('发送数据:' + Str);
Memo1.Lines.Add('接收数据:' + Buff);
end
else
begin
Memo1.Lines.Add('请输入要发送的数据');
end;
end
else
begin
Memo1.Lines.Add('请先打开串口');
end;
end;
end.
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)