当前位置:编程学习 > Delphi >>

delphi串口编程 实现数据发送

Delphi串口编程  怎样实现 上面两个框中的数据

转换成16进制  发送出去  有发送函数 不知道如何修改

procedure senddatatime; //发送函数 (时间)
var
i:integer;
commflg:boolean;
begin
viewstring:='';
commflg:=true;
for i:=1 to 4 do
begin
if not form1.comm1.writecommdata(@sbuf[i],1) then
begin
commflg:=false;
break;
end;
//发送时字节间的延时
sleep(2);
viewstring:=viewstring+inttohex(sbuf[i],2)+''; end;
viewstring:='发送指令 '+ viewstring;
form1.memo1.lines.add(viewstring);
form1.memo1.lines.add('');
if not commflg then messagedlg('发送失败 !' ,mterror,[mbyes],0);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
j:integer;
begin
if RadioButton1.Checked then
begin
i:=combobox1.ItemIndex;
if i=0 then
begin
sbuf[1]:=byte($57); //帧头
sbuf[2]:=byte($52); //命令号
sbuf[3]:=byte($0D);
sbuf[4]:=byte($0A); //帧尾
senddata;//调用发送函数
end
else if i=1 then
begin
sbuf[1]:=byte($57); //帧头
sbuf[2]:=byte($47); //命令号
sbuf[3]:=byte($0D);
sbuf[4]:=byte($0A); //帧尾
senddata;//调用发送函数
end else if i=2 then
begin
sbuf[1]:=byte($57); //帧头
sbuf[2]:=byte($59); //命令号
sbuf[3]:=byte($0D);
sbuf[4]:=byte($0A); //帧尾
senddata;//调用发送函数
end else if i=3 then
begin
sbuf[1]:=byte($57); //帧头
sbuf[2]:=byte($41); //命令号
sbuf[3]:=byte($0D);
sbuf[4]:=byte($0A); //帧尾
senddata;//调用发送函数
end else if i=4 then
begin
sbuf[1]:=byte($57); //帧头
sbuf[2]:=byte($50); //命令号
sbuf[3]:=byte($0D);
sbuf[4]:=byte($0A); //帧尾
senddata;//调用发送函数
end;

补充:

急!!!!!!!!

答案:

把下面的函数加进去,用它在senddata函数中处理字符串

function StrToHexStr(const S:string):string;
//字符串转换成16进制字符串
var
  I:Integer;
begin
  for I:=1 to Length(S) do
  begin
    if I=1 then
      Result:=IntToHex(Ord(S[1]),2)
    else Result:=Result+' '+IntToHex(Ord(S[I]),2);
  end;
end;

刹那 直接读取两个edit中数据 怎样转 再发送

上一个:delphi7 编程问题
下一个:我学Delphi编程两年了,但一直没多大进步,有经验的说说学习经验啊。

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,