spcomm难题,如何发送16进制数据(200分)

  • 主题发起人 主题发起人 sdonline
  • 开始时间 开始时间
S

sdonline

Unregistered / Unconfirmed
GUEST, unregistred user!
我有一个VB程序是用msComm进行串口控制,设置如下
commport通讯端口:1
settomgs参数:9600,n,8,1
handshaking握手协议:0-comnone
inBufferSize输入缓存:1024
OutBufferSize输出缓存:512
RThreshold R阀值:0
Sthreshold S阀值:0
inputlen 输入长度:0
inputMode:0-comInputModeText
发送控制命令如下
MSComm1.Output = "$015FF" & Chr(13) '不清楚
MSComm1.Output = "#091001" & Chr(13) '开
MSComm1.Output = "#091000" & Chr(13) '关
MSComm1.Output = "#010" & Chr(13) '取值
我觉得$015FF是十六进制,但当设备方说,上面的命令全是十六进制的。我现在用delphi重新开发,用spcomm进行串口控制,怎么也不好用,该怎样进行设置,相应的控制命令怎么发送呀,请各位大侠帮助。马上送分
 
spcomm你可以去google搜一下的,上面有个例子的。
spcomm是用数组来处理发送的数据的
 
没什么要设置的啊?就是直接发嘛。我也正用spcomm发十六进制数据,好象没什么难题啊?
 
这个应该可以,我刚做了是可以的,不知道满足你的要求吗?

变量说明
var
FCOMM: TFCOMM;
Viewstring:string;
i:integer;
rbuf,sbuf:array[1..6] of byte;

打开串口
procedure TFCOMM.FormShow(Sender: TObject);
begin
comm1.StartComm;
end;
关闭串口
procedure TFCOMM.FormClose(Sender: TObject; var Action: TCloseAction);
begin
comm1.StopComm;
end;
发送数据
自定义的发送过程
procedure senddata;
var
i:integer;
commflg:boolean;
begin
viewstring:='';
commflg:=true;
for i:=1 to 6 do
begin
if not fcomm.comm1.writecommdata(@sbuf1) then
begin
commflg:=false;
break;
end;
sleep(2); {发送时字节间的延时}
viewstring:=viewstring+inttohex(sbuf2)+'';
end;
viewstring:="发送"+viewstring;
fcomm.memo1.lines.add(viewstring);
fcomm.memo1.lines.add("");
if not commflg then messagedlg("发送失败!",mterror,[mbyes],0);
end;
procedure TFCOMM.Btn_sendClick(Sender: TObject);{发送按钮的点击事件}
begin
sbuf[1]:=byte($f0); {帧头}
sbuf[2]:=byte($01); {命令号}
sbuf[3]:=byte($ff);
sbuf[4]:=byte($ff);
sbuf[5]:=byte($01);
sbuf[6]:=byte($0f); {帧尾}
senddata;{调用发送函数}
end;
你可以下载一个串口精灵试一下!
 
一叶孤舟:你好,谢谢你,这个例子我也有,但是照这个样子试过没成功,我不知道"$015FF" & Chr(13) 应该怎么发,"#091001" & Chr(13) 在delphi中是十进制还是十六进制,该怎么发。
 
var
numb:string;
sss: String;
sss1: String;
sss2:string;
begin
sss:=#$0;
sss1:=#$55;
sss2:=#$04;
MsComm1.Output:=sss;
sleep(600);
mscomm1.Output:=sss1;
numb:=mscomm1.input;
memo1.Text:=memo1.Text+'这是该设备的号码'+numb+#10#13;
mscomm1.Output:=sss2;
memo1.Text:=memo1.Text +'这是卡号'+trim(mscomm1.input)+#10#13;
我使用如上代码将我所使用的设备号读了出来。希望能对你有所帮助。
有时间qq上聊一会,我也在做一个串口通讯的项目。
qq:93882205
 
各位。我除了 "$015FF" & Chr(13) '不清楚其他已经弄懂了
 
var
Buf: Array[1..3] of Char;
begin
Buf[1]:=$15;
Buf[2]:=$FF;
Buf[3]:=$0D; //13
SpComm.WriteCommData(Buf[1],3);
end;
 
看看,亲自调试过的procedure TForm1.SendHex(S: String);
procedure TForm1.SendHex(S: String);var
s2:string;
buf1:array[0..40000] of char;
i:integer;
begin
s2:='';
for i:=1 to length(s) do
begin
if ((copy(s,i,1)>='0') and (copy(s,i,1)<='9'))or((copy(s,i,1)>='a') and (copy(s,i,1)<='f'))
or((copy(s,i,1)>='A') and (copy(s,i,1)<='F')) then
begin
s2:=s2+copy(s,i,1);
end;
end;
for i:=0 to (length(s2) div 2-1) do
buf1:=char(strtoint('$'+copy(s2,i*2+1,2)));
Comm1.WriteCommData(buf1,(length(s2) div 2));
end;
有什么不懂的可以提出,你可以用这个函数
 
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, SPComm;

type
TForm1 = class(TForm)
Comm1: TComm;
Button1: TButton;
Memo1: TMemo;
Button2: TButton;
Edit1: TEdit;
Button3: TButton;
Button4: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
procedure Comm1ReceiveError(Sender: TObject; EventMask: Cardinal);
private
sBuf,rBuf:array[1..16] of byte;
sBuffer:Pointer;
sBufferLength:Word;
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
comm1.StartComm;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
self.Comm1.StopComm;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
sBuf[1]:=byte($68);
sBuf[2]:=byte($03);
sBuf[3]:=byte($03);
sBuf[4]:=byte($68);
sBuf[5]:=byte($72);
sBuf[6]:=byte($13);
sBuf[7]:=byte($05);
sBuf[8]:=byte($8a);
sBuf[9]:=byte($0d);
end;

procedure TForm1.Button3Click(Sender: TObject);
var
i:integer;
vie:string;
begin
vie:='';
for i:=1 to 9 do
begin
caption:='正在发送'+inttostr(i);
if not self.Comm1.WriteCommData(@sBuf,1) then
begin
showmessage('err');
break;
end;
vie:=vie+inttohex(sbuf,2)+' ';
sleep(2);
end;
memo1.Lines.Add(vie);
if self.Comm1.SendDataEmpty then
showmessage('发送为');
//self.Comm1.
end;

procedure TForm1.Button4Click(Sender: TObject);
var
strReceive:string;

begin

end;

procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
{var
strReceive:string;
begin
SetLength(strReceive,BufferLength);
Move(Buffer^,PChar(StrReceive)^,BufferLength);
Memo1.Lines.Add(strReceive);
} var

i:integer;
strReceive:string;
begin
strReceive:='';
move(buffer^,pchar(@rbuf)^,bufferlength);
for i:=1 to bufferlength do
strReceive:=strReceive+inttohex(rbuf,2)+' ';
strReceive:='接收:=>'+strReceive+' ';
memo1.lines.add(strReceive);
memo1.lines.add('');

end;

procedure TForm1.Comm1ReceiveError(Sender: TObject; EventMask: Cardinal);
begin
showmessage('err');
end;

end.
 
后退
顶部