空瓶氧气 发表于 2014-10-18 21:34:54

串口助手——基于spcomm

本帖最后由 空瓶氧气 于 2014-10-18 21:55 编辑

http://yunpan.cn/csAQN3sN3htqU
还有很多的bug;哪个大神修改分享下
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);
      //Comm1.StopBits := TStopBits(ComboBox3.Items.Strings);

      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 notflag_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 =truethen
   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
movesi,edx       //保存edx值,用来产生新字符串的地址
movedi,eax       //保存原字符串
movedx,//获得字符串长度
test edx,edx      //检查长度    je   @@Exit      {Length(S) = 0}
movecx,edx       //保存长度
Push ecx
shledx,1
moveax,esi
{$IFDEF VER210}
movzx ecx, word ptr {需要设置CodePage}
{$ENDIF}
call System.@LStrSetLength //设置新串长度
moveax,esi       //新字符串地址
Call UniqueString//产生一个唯一的新字符串,串位置在eax中
Pop   ecx
@@SetHex:
xoredx,edx       //清空edx
movdl,    //Str字符串字符
movebx,edx       //保存当前的字符
shredx,4         //右移4字节,得到高8位
movdl,byte ptr //转换成字符
mov,dl      //将字符串输入到新建串中存放
andebx,$0F       //获得低8位
movdl,byte ptr //转换成字符
inceax             //移动一个字节,存放低位
mov,dl
incedi
inceax
loop @@SetHex
@@Exit:
   popedi
   popesi
   popebx
   ret
   @@HexChar:db '0123456789ABCDEF'
end;

function HexToStr(const Str: AnsiString): AnsiString;
asm
push ebx
push edi
push esi
test eax,eax //为空串
jz   @@Exit
movedi,eax
movesi,edx
movedx,
test edx,edx
je   @@Exit
movecx,edx
push ecx
shredx,1
moveax,esi //开始构造字符串
{$IFDEF VER210}
movzx ecx, word ptr {需要设置CodePage}
{$ENDIF}
call System.@LStrSetLength //设置新串长度
moveax,esi       //新字符串地址
Call UniqueString//产生一个唯一的新字符串,串位置在eax中
Pop   ecx
xorebx,ebx
xoresi,esi
@@CharFromHex:
xoredx,edx
movdl,    //Str字符串字符
cmpdl, '0'//查看是否在0到f之间的字符
JB   @@Exit   //小于0,退出
cmpdl,'9'   //小于=9
ja@@DoChar//CompOkNum
subdl,'0'
jmp@@DoConvert
@@DoChar:
//先转成大写字符
anddl,$DF
cmpdl,'F'
ja   @@Exit//大于F退出
adddl,10
subdl,'A'
@@DoConvert: //转化
incebx
cmpebx,2
je   @@Num1
xoresi,esi
shledx,4
movesi,edx
jmp@@Num2
@@Num1:
addesi,edx
movedx,esi
mov,dl
xorebx,ebx
inceax
@@Num2:
dececx
incedi
test ecx,ecx
jnz@@CharFromHex
@@Exit:
popesi
popedi
popebx
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;
end;

procedure TForm1.ComboBox1Change(Sender: TObject);
begin
Comm1.BaudRate := Cardinal(ComboBox1.Items.Strings);
end;

end.
提取码 98f9

qq351317878 发表于 2014-10-19 17:33:23

二楼膜拜大神   。这些台高深 目前还不懂
页: [1]
查看完整版本: 串口助手——基于spcomm