鱼C论坛

 找回密码
 立即注册
查看: 3012|回复: 1

[技术交流] 串口助手——基于spcomm

[复制链接]
发表于 2014-10-18 21:34:54 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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

http://yunpan.cn/csAQN3sN3htqU  
还有很多的bug;哪个大神修改分享下
  1. unit Unit1;

  2. interface

  3. uses
  4.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  5.   Dialogs, SPComm, StdCtrls, ExtCtrls, ToolWin, ComCtrls, Menus,ShellAPI;

  6. type
  7.   TForm1 = class(TForm)
  8.     Comm1: TComm;
  9.     Panel1: TPanel;
  10.     Button1: TButton;
  11.     Edit1: TEdit;
  12.     Button2: TButton;
  13.     Memo1: TMemo;
  14.     MainMenu1: TMainMenu;
  15.     File1: TMenuItem;
  16.     ComboBox1: TComboBox;
  17.     ComboBox2: TComboBox;
  18.     ComboBox3: TComboBox;
  19.     ComboBox4: TComboBox;
  20.     CheckBox1: TCheckBox;
  21.     Set1: TMenuItem;
  22.     N3: TMenuItem;
  23.     OPEN1: TMenuItem;
  24.     Save1: TMenuItem;
  25.     CheckBox2: TCheckBox;
  26.     Image1: TImage;
  27.     N1: TMenuItem;
  28.     N2: TMenuItem;
  29.     Label1: TLabel;
  30.     Label2: TLabel;
  31.     Label3: TLabel;
  32.     Label4: TLabel;
  33.     procedure Button2Click(Sender: TObject);
  34.     procedure Button1Click(Sender: TObject);
  35.     procedure FormClose(Sender: TObject; var Action: TCloseAction);
  36.     procedure Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
  37.       BufferLength: Word);
  38.     procedure FormCreate(Sender: TObject);
  39.     procedure N2Click(Sender: TObject);
  40.     procedure ComboBox4Change(Sender: TObject);
  41.     procedure ComboBox1Change(Sender: TObject);
  42.   private
  43.     { Private declarations }
  44.   public

  45.     { Public declarations }
  46.   end;

  47. var
  48.   Form1: TForm1;
  49.   function StrToHex(Const str: Ansistring): Ansistring;
  50.   function HexToStr(const Str: AnsiString): AnsiString;
  51. implementation

  52. {$R *.dfm}
  53. var
  54.   flag_ComOpen:Boolean = False;
  55.   receivedata:string;

  56. procedure TForm1.Button2Click(Sender: TObject);
  57. begin
  58.     if flag_ComOpen=False then
  59.     Begin
  60.       ComboBox1.Enabled :=False;
  61.       Label1.Enabled :=False;
  62.       ComboBox4.Enabled :=False;
  63.       Label4.Enabled :=False;
  64.       //Comm1.ByteSize := TByteSize(ComboBox2.Items.Strings[ComboBox2.ItemIndex]);
  65.       //Comm1.StopBits := TStopBits(ComboBox3.Items.Strings[ComboBox3.ItemIndex ]);

  66.       SLEEP(10);
  67.       Comm1.StartComm;
  68.       //Button2.Font.Color := clRed;
  69.       Button2.Caption := '关闭串口';
  70.       //Button2.Font.Color := clLime;
  71.       flag_ComOpen:=True
  72.     end
  73.   else
  74.   begin
  75.     ComboBox1.Enabled :=True;
  76.     ComboBox4.Enabled :=True;
  77.     Label1.Enabled :=True;
  78.     Label4.Enabled :=True;

  79.     Comm1.StopComm ;
  80.     Button2.Caption := '打开串口';
  81.     flag_ComOpen:=False;
  82.   end;
  83. end;

  84. procedure TForm1.Button1Click(Sender: TObject);
  85. var
  86. //flag_SendOK:Boolean;
  87. //str:string;
  88. n:integer;
  89. p:pchar;
  90. begin
  91.     if CheckBox2.Checked =true then
  92.       p:=pchar(HexToStr(Edit1.text))
  93.     else
  94.       p:=Pchar(Edit1.text);
  95.     n:=length(p);
  96.     if not (n=0) then
  97.     begin
  98.       //while not  flag_SendOK do
  99.       //flag_SendOK:=Comm1.WriteCommData(p,n);
  100.       Comm1.WriteCommData(p,n);
  101.     end;
  102. end;


  103. procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
  104. begin
  105.   if flag_ComOpen=True then
  106.     Comm1.StopComm ;
  107. end;

  108. procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
  109.   BufferLength: Word);
  110. var
  111.   pStr:pchar;
  112. begin
  113.   pStr := Buffer;
  114.   receivedata:=receivedata+ pStr;

  115.   if CheckBox1.Checked =true  then
  116.    begin
  117.     memo1.Text :=StrToHex(receivedata);
  118.    end
  119.    else
  120.    begin
  121.      memo1.Text :=receivedata;
  122.    end;

  123.    if length(receivedata)>1000 then
  124.      receivedata := '';

  125. end;

  126. procedure TForm1.FormCreate(Sender: TObject);
  127. begin
  128.   ComboBox1.Items.Add('4800');
  129.   ComboBox1.Items.Add('9600');
  130.   ComboBox1.Items.Add('115200');

  131.   //ComboBox2.Items.Add('_7');
  132.   //ComboBox2.Items.Add('_8');

  133.   //ComboBox3.Items.Add('1');
  134.   //ComboBox3.Items.Add('2');

  135.   ComboBox4.Items.Add('com1');
  136.   ComboBox4.Items.Add('com2');
  137.   ComboBox4.Items.Add('com3');
  138.   ComboBox4.Items.Add('com4');
  139.   ComboBox4.Items.Add('com5');

  140. end;

  141. function StrToHex(Const str: Ansistring): Ansistring;
  142. asm
  143.   push ebx
  144.   push esi
  145.   push edi
  146.   test eax,eax
  147.   jz   @@Exit
  148.   mov  esi,edx       //保存edx值,用来产生新字符串的地址
  149.   mov  edi,eax       //保存原字符串
  150.   mov  edx,[eax-4]  //获得字符串长度
  151.   test edx,edx      //检查长度    je   @@Exit      {Length(S) = 0}
  152.   mov  ecx,edx       //保存长度
  153.   Push ecx
  154.   shl  edx,1
  155.   mov  eax,esi
  156.   {$IFDEF VER210}
  157.   movzx ecx, word ptr [edi-12] {需要设置CodePage}
  158.   {$ENDIF}
  159.   call System.@LStrSetLength //设置新串长度
  160.   mov  eax,esi       //新字符串地址
  161.   Call UniqueString  //产生一个唯一的新字符串,串位置在eax中
  162.   Pop   ecx
  163. @@SetHex:
  164.   xor  edx,edx       //清空edx
  165.   mov  dl, [edi]     //Str字符串字符
  166.   mov  ebx,edx       //保存当前的字符
  167.   shr  edx,4         //右移4字节,得到高8位
  168.   mov  dl,byte ptr[edx+@@HexChar] //转换成字符
  169.   mov  [eax],dl      //将字符串输入到新建串中存放
  170.   and  ebx,$0F       //获得低8位
  171.   mov  dl,byte ptr[ebx+@@HexChar] //转换成字符
  172.   inc  eax             //移动一个字节,存放低位
  173.   mov  [eax],dl
  174.   inc  edi
  175.   inc  eax
  176.   loop @@SetHex
  177. @@Exit:
  178.    pop  edi
  179.    pop  esi
  180.    pop  ebx
  181.    ret
  182.    @@HexChar:db '0123456789ABCDEF'
  183. end;

  184. function HexToStr(const Str: AnsiString): AnsiString;
  185. asm
  186.   push ebx
  187.   push edi
  188.   push esi
  189.   test eax,eax //为空串
  190.   jz   @@Exit
  191.   mov  edi,eax
  192.   mov  esi,edx
  193.   mov  edx,[eax-4]
  194.   test edx,edx
  195.   je   @@Exit
  196.   mov  ecx,edx
  197.   push ecx
  198.   shr  edx,1
  199.   mov  eax,esi //开始构造字符串
  200.   {$IFDEF VER210}
  201.   movzx ecx, word ptr [edi-12] {需要设置CodePage}
  202.   {$ENDIF}
  203.   call System.@LStrSetLength //设置新串长度
  204.   mov  eax,esi       //新字符串地址
  205.   Call UniqueString  //产生一个唯一的新字符串,串位置在eax中
  206.   Pop   ecx
  207.   xor  ebx,ebx
  208.   xor  esi,esi
  209. @@CharFromHex:
  210.   xor  edx,edx
  211.   mov  dl, [edi]     //Str字符串字符
  212.   cmp  dl, '0'  //查看是否在0到f之间的字符
  213.   JB   @@Exit   //小于0,退出
  214.   cmp  dl,'9'   //小于=9
  215.   ja  @@DoChar//CompOkNum
  216.   sub  dl,'0'
  217.   jmp  @@DoConvert
  218. @@DoChar:
  219.   //先转成大写字符
  220.   and  dl,$DF
  221.   cmp  dl,'F'
  222.   ja   @@Exit  //大于F退出
  223.   add  dl,10
  224.   sub  dl,'A'
  225. @@DoConvert: //转化
  226.   inc  ebx
  227.   cmp  ebx,2
  228.   je   @@Num1
  229.   xor  esi,esi
  230.   shl  edx,4
  231.   mov  esi,edx
  232.   jmp  @@Num2
  233. @@Num1:
  234.   add  esi,edx
  235.   mov  edx,esi
  236.   mov  [eax],dl
  237.   xor  ebx,ebx
  238.   inc  eax
  239. @@Num2:
  240.   dec  ecx
  241.   inc  edi
  242.   test ecx,ecx
  243.   jnz  @@CharFromHex
  244. @@Exit:
  245.   pop  esi
  246.   pop  edi
  247.   pop  ebx
  248. end;


  249. procedure TForm1.N2Click(Sender: TObject);
  250. begin
  251.   ShellAbout(self.Handle ,
  252.     PChar('v1.0'),
  253.     pchar('By: apo'),
  254.     HICON(0));
  255. end;

  256. procedure TForm1.ComboBox4Change(Sender: TObject);
  257. begin
  258.   Comm1.CommName := ComboBox4.Items.Strings[ComboBox4.ItemIndex ];
  259. end;

  260. procedure TForm1.ComboBox1Change(Sender: TObject);
  261. begin
  262.   Comm1.BaudRate := Cardinal(ComboBox1.Items.Strings[ComboBox1.ItemIndex ]);
  263. end;

  264. end.
复制代码

提取码 98f9
QQ截图20141018213418.png
QQ截图20141018213924.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-10-19 17:33:23 | 显示全部楼层
二楼膜拜大神   。这些台高深 目前还不懂
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-1 06:05

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表