unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, SPComm, StdCtrls, ExtCtrls, ToolWin, ComCtrls, Menus,ShellAPI;
type
TForm1 = class(TForm)
Comm1: TComm;
Panel1: TPanel;
Button1: TButton;
Edit1: TEdit;
Button2: TButton;
Memo1: TMemo;
MainMenu1: TMainMenu;
File1: TMenuItem;
ComboBox1: TComboBox;
ComboBox2: TComboBox;
ComboBox3: TComboBox;
ComboBox4: TComboBox;
CheckBox1: TCheckBox;
Set1: TMenuItem;
N3: TMenuItem;
OPEN1: TMenuItem;
Save1: TMenuItem;
CheckBox2: TCheckBox;
Image1: TImage;
N1: TMenuItem;
N2: TMenuItem;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
procedure FormCreate(Sender: TObject);
procedure N2Click(Sender: TObject);
procedure ComboBox4Change(Sender: TObject);
procedure ComboBox1Change(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
function StrToHex(Const str: Ansistring): Ansistring;
function HexToStr(const Str: AnsiString): AnsiString;
implementation
{$R *.dfm}
var
flag_ComOpen:Boolean = False;
receivedata:string;
procedure TForm1.Button2Click(Sender: TObject);
begin
if flag_ComOpen=False then
Begin
ComboBox1.Enabled :=False;
Label1.Enabled :=False;
ComboBox4.Enabled :=False;
Label4.Enabled :=False;
//Comm1.ByteSize := TByteSize(ComboBox2.Items.Strings[ComboBox2.ItemIndex]);
//Comm1.StopBits := TStopBits(ComboBox3.Items.Strings[ComboBox3.ItemIndex ]);
SLEEP(10);
Comm1.StartComm;
//Button2.Font.Color := clRed;
Button2.Caption := '关闭串口';
//Button2.Font.Color := clLime;
flag_ComOpen:=True
end
else
begin
ComboBox1.Enabled :=True;
ComboBox4.Enabled :=True;
Label1.Enabled :=True;
Label4.Enabled :=True;
Comm1.StopComm ;
Button2.Caption := '打开串口';
flag_ComOpen:=False;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
//flag_SendOK:Boolean;
//str:string;
n:integer;
p:pchar;
begin
if CheckBox2.Checked =true then
p:=pchar(HexToStr(Edit1.text))
else
p:=Pchar(Edit1.text);
n:=length(p);
if not (n=0) then
begin
//while not flag_SendOK do
//flag_SendOK:=Comm1.WriteCommData(p,n);
Comm1.WriteCommData(p,n);
end;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if flag_ComOpen=True then
Comm1.StopComm ;
end;
procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
var
pStr:pchar;
begin
pStr := Buffer;
receivedata:=receivedata+ pStr;
if CheckBox1.Checked =true then
begin
memo1.Text :=StrToHex(receivedata);
end
else
begin
memo1.Text :=receivedata;
end;
if length(receivedata)>1000 then
receivedata := '';
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
ComboBox1.Items.Add('4800');
ComboBox1.Items.Add('9600');
ComboBox1.Items.Add('115200');
//ComboBox2.Items.Add('_7');
//ComboBox2.Items.Add('_8');
//ComboBox3.Items.Add('1');
//ComboBox3.Items.Add('2');
ComboBox4.Items.Add('com1');
ComboBox4.Items.Add('com2');
ComboBox4.Items.Add('com3');
ComboBox4.Items.Add('com4');
ComboBox4.Items.Add('com5');
end;
function StrToHex(Const str: Ansistring): Ansistring;
asm
push ebx
push esi
push edi
test eax,eax
jz @@Exit
mov esi,edx //保存edx值,用来产生新字符串的地址
mov edi,eax //保存原字符串
mov edx,[eax-4] //获得字符串长度
test edx,edx //检查长度 je @@Exit {Length(S) = 0}
mov ecx,edx //保存长度
Push ecx
shl edx,1
mov eax,esi
{$IFDEF VER210}
movzx ecx, word ptr [edi-12] {需要设置CodePage}
{$ENDIF}
call System.@LStrSetLength //设置新串长度
mov eax,esi //新字符串地址
Call UniqueString //产生一个唯一的新字符串,串位置在eax中
Pop ecx
@@SetHex:
xor edx,edx //清空edx
mov dl, [edi] //Str字符串字符
mov ebx,edx //保存当前的字符
shr edx,4 //右移4字节,得到高8位
mov dl,byte ptr[edx+@@HexChar] //转换成字符
mov [eax],dl //将字符串输入到新建串中存放
and ebx,$0F //获得低8位
mov dl,byte ptr[ebx+@@HexChar] //转换成字符
inc eax //移动一个字节,存放低位
mov [eax],dl
inc edi
inc eax
loop @@SetHex
@@Exit:
pop edi
pop esi
pop ebx
ret
@@HexChar:db '0123456789ABCDEF'
end;
function HexToStr(const Str: AnsiString): AnsiString;
asm
push ebx
push edi
push esi
test eax,eax //为空串
jz @@Exit
mov edi,eax
mov esi,edx
mov edx,[eax-4]
test edx,edx
je @@Exit
mov ecx,edx
push ecx
shr edx,1
mov eax,esi //开始构造字符串
{$IFDEF VER210}
movzx ecx, word ptr [edi-12] {需要设置CodePage}
{$ENDIF}
call System.@LStrSetLength //设置新串长度
mov eax,esi //新字符串地址
Call UniqueString //产生一个唯一的新字符串,串位置在eax中
Pop ecx
xor ebx,ebx
xor esi,esi
@@CharFromHex:
xor edx,edx
mov dl, [edi] //Str字符串字符
cmp dl, '0' //查看是否在0到f之间的字符
JB @@Exit //小于0,退出
cmp dl,'9' //小于=9
ja @@DoChar//CompOkNum
sub dl,'0'
jmp @@DoConvert
@@DoChar:
//先转成大写字符
and dl,$DF
cmp dl,'F'
ja @@Exit //大于F退出
add dl,10
sub dl,'A'
@@DoConvert: //转化
inc ebx
cmp ebx,2
je @@Num1
xor esi,esi
shl edx,4
mov esi,edx
jmp @@Num2
@@Num1:
add esi,edx
mov edx,esi
mov [eax],dl
xor ebx,ebx
inc eax
@@Num2:
dec ecx
inc edi
test ecx,ecx
jnz @@CharFromHex
@@Exit:
pop esi
pop edi
pop ebx
end;
procedure TForm1.N2Click(Sender: TObject);
begin
ShellAbout(self.Handle ,
PChar('v1.0'),
pchar('By: apo'),
HICON(0));
end;
procedure TForm1.ComboBox4Change(Sender: TObject);
begin
Comm1.CommName := ComboBox4.Items.Strings[ComboBox4.ItemIndex ];
end;
procedure TForm1.ComboBox1Change(Sender: TObject);
begin
Comm1.BaudRate := Cardinal(ComboBox1.Items.Strings[ComboBox1.ItemIndex ]);
end;
end.
提取码 98f9