1,2对,第3种只能在MSComm.InputMode=0时,发送字符串类型的数据,在MSComm.InputMode=1时可
发送字节类型,不过接收要变换一下,给你个例子吧.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, OleCtrls, MSCommLib_TLB, ExtCtrls, Menus, Unit2, ComCtrls, ToolWin, ImgList;
type
TForm1 = class(TForm)
Panel1: TPanel;
Memo1: TMemo;
Splitter2: TSplitter;
Memo2: TMemo;
MainMenu1: TMainMenu;
ComSet1: TMenuItem;
N1: TMenuItem;
N2: TMenuItem;
N4: TMenuItem;
N5: TMenuItem;
N6: TMenuItem;
N7: TMenuItem;
N8: TMenuItem;
N9: TMenuItem;
N10: TMenuItem;
N11: TMenuItem;
N12: TMenuItem;
N13: TMenuItem;
N14: TMenuItem;
MSComm1: TMSComm;
ControlBar1: TControlBar;
ToolBar1: TToolBar;
ToolButton1: TToolButton;
ToolButton3: TToolButton;
ToolButton4: TToolButton;
ToolButton5: TToolButton;
ImageList1: TImageList;
OpenDialog1: TOpenDialog;
SaveDialog1: TSaveDialog;
ImageList2: TImageList;
StatusBar1: TStatusBar;
procedure FormCreate(Sender: TObject);
procedure MSComm1Comm(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure N13Click(Sender: TObject);
procedure N14Click(Sender: TObject);
procedure N2Click(Sender: TObject);
procedure N4Click(Sender: TObject);
procedure N6Click(Sender: TObject);
procedure N9Click(Sender: TObject);
procedure ToolButton5Click(Sender: TObject);
private
{ Private declarations }
Recvar:variant;
BinaryFlag,TextFlag:Boolean;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
Procedure SendBinary(Str:String);
var
Sendvar:Variant;
Ch1,Ch2:Char;
Byte1,Byte2:Byte;
I:Integer;
begin
Sendvar:=VarArrayCreate([1,Length(Str) div 2],varByte);
Byte1:=0;
Byte2:=0;
for I:=1 to Length(Str) div 2 do
begin
Ch1:=Str[2*I-1];
Ch2:=Str[2*I];
if (Ch1>='0') and (Ch1<='9') then
Byte1:=Ord(Ch1)-48
else if (Ch1>='A') and (Ch1<='F') then
Byte1:=Ord(Ch1)-55;
if (Ch2>='0') and (Ch2<='9') then
Byte2:=Ord(Ch2)-48
else if (Ch2>='A') and (Ch2<='F') then
Byte2:=Ord(Ch2)-55;
Sendvar:=Byte1*16+Byte2;
end;
Form1.MSComm1.Output:=Sendvar;
end;
procedure SendText(Str:String);
begin
Form1.MSComm1.Output:=Str;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
BinaryFlag:=False;
TextFlag:=True;
MsComm1.InBufferCount:=0;
MsComm1.OutBufferCount:=0;
MsComm1.InputLen:=0;
MsComm1.RThreshold:=1;
MsComm1.SThreshold:=1;
Recvar:=VarArrayCreate([0,63],varByte);
end;
procedure TForm1.MSComm1Comm(Sender: TObject);
var
I,J:Integer;
begin
if MsComm1.CommEvent=2 then //串口事件为接收;
begin
if BinaryFlag=True then
begin
I:=MSComm1.InBufferCount; //接收缓冲区内已有数据个数;
MSComm1.InputLen:=I;
Recvar:=MSComm1.Input;
for J:=0 to I-1 do
Memo2.Text:=Memo2.Text+IntToHex(Recvar[J],2);
end;
if TextFlag=True then
Memo2.Text:=Memo2.Text+MsComm1.Input;
end;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if MSComm1.PortOpen=True then
MSComm1.PortOpen:=False;
end;
procedure TForm1.N13Click(Sender: TObject);
begin
BinaryFlag:=False;
TextFlag:=True;
N13.Checked:=True;
N14.Checked:=False;
MSComm1.InputMode:=0; //设置方式为文本方式;
end;
procedure TForm1.N14Click(Sender: TObject);
begin
BinaryFlag:=True;
TextFlag:=False;
N13.Checked:=False;
N14.Checked:=True;
MSComm1.InputMode:=1; //设置方式为二进制方式;
end;
procedure TForm1.N2Click(Sender: TObject);
begin
with Form2 do
if ShowModal=mrOK then
begin
if MSComm1.PortOpen=True then
MSComm1.PortOpen:=False;
if RadioButton1.Checked then
MsComm1.CommPort:=1
else
MsComm1.CommPort:=2;
//设置疮口参数;
MsComm1.Settings:=ComboBox1.Text+ComboBox2.Text+ComboBox3.Text+ComboBox4.Text;
//打开串口;
MsComm1.PortOpen:=True;
end;
end;
procedure TForm1.ToolButton5Click(Sender: TObject);
begin
if MSComm1.PortOpen=False then
begin
MessageDlg('串口未打开',mtError,[mbOK],0);
Exit;
end;
if BinaryFlag then
SendBinary(Memo1.Text); //发送二进制方式,注意去除回车换行符;
if TextFlag then
SendText(Memo1.Text); //发送文本方式;
end;
procedure TForm1.N4Click(Sender: TObject);
begin
if TextFlag=True then
OpenDialog1.InitialDir:=GetCurrentDir()+'/文本文件夹/';
if BinaryFlag=True then
OpenDialog1.InitialDir:=GetCurrentDir()+'/二进制文件夹/';
OpenDialog1.DefaultExt:='.txt';
if OpenDialog1.Execute then
Memo1.Lines.LoadFromFile(OpenDialog1.FileName);
end;
procedure TForm1.N6Click(Sender: TObject);
begin
if TextFlag then
SaveDialog1.InitialDir:=GetCurrentDir()+'/文本文件夹/';
if BinaryFlag then
SaveDialog1.InitialDir:=GetCurrentDir()+'/二进制文件夹/';
SaveDialog1.DefaultExt:='.txt';
if SaveDialog1.Execute then
Memo2.Lines.SaveToFile(SaveDialog1.FileName);
end;
procedure TForm1.N9Click(Sender: TObject);
begin
Close;
end;
end.