MSCOMM发送通讯协议的困惑,拜托,分数全奉上了, (60分)

  • 主题发起人 主题发起人 Z_JJ
  • 开始时间 开始时间
Z

Z_JJ

Unregistered / Unconfirmed
GUEST, unregistred user!
具体参见 http://delphibbs.com/delphibbs/dispq.asp?lid=919099
1、计算机要求收费机上传最近一次刷卡的卡号及扣款额:计算机向收费机发送 16 + 2 + 1 三个整数字节。
请问如果我用MSCOMM,具体应该怎么写呢?是 MSCOMM.OUTPUT:='1621',还是MSCOMM.OUTPUT:='160201',
还是with mscomm do begin output:='16'
output:='2'
output:='1' end;
 
Buffer = Chr$(16)
MSComm1.Output = Buffer
Buffer = Chr$(2)
MSComm1.Output = Buffer
Buffer = Chr$(1)
MSComm1.Output = Buffer
 
我是用DELPHI,不是VB
 
从你的要求上看应该是MSCOMM.OUTPUT:='160201';
但是具体格式与你的通信协议有关。
 
delphi也有MSCOMM啊
那就:
output:=#16#2#1
 
庆贺,现在我已经成功实现了串口通讯,将记费部分的源代码贴出供大家指教。
欢迎来访原创软件区 http://ycrj.delphibbs.com
使用了MSCOMM32.OCX控件,下面是ONCOMM事件的源代码,即实时记费过程,其中要判断是否为员工卡,
如果是员工卡中餐消费有相应的补贴。通讯协议就是上面说的。
procedure TForm1.MSComm1Comm(Sender: TObject);
var icn,icno,icxfs,dataH,ss:string;
buffer:variant
ovTmp:olevariant;
readdr:byte
i:integer
isbt:boolean;
begin
Case MSComm1.CommEvent of
comEvReceive:begin
ovTmp:=vararrayCreate([0,13],varbyte);
MSComm1.InputLen:=13;
ovTmp:=MSComm1.Input;
icno:='';
icn:='';
icxfs:='';
buffer:=ovTmp[0];
If length(Buffer)=0 Then MSComm1.InBufferCount:=0;
readdr:=Buffer;
If (readdr<>tb_info.Fieldbyname('地址2').AsInteger)
or (readdr<>tb_info.Fieldbyname('地址3').Asinteger)
Then begin
MSComm1.InBufferCount:=0
MSComm1.OutBufferCount:=0;
end;
for i:=1 to 5 do
begin
buffer:=ovTmp;
dataH:=inttostr(trunc(buffer/16)*10+buffer Mod 16);
If Length(dataH)=1 Then dataH:='0'+dataH;
icno:=icno+dataH;
end;
for i:=6 to 10 do
begin
buffer:=ovTmp;
dataH:=inttostr(buffer);
icxfs:=icxfs+dataH;
End;
buffer:=ovTmp[11];
dataH:=inttostr(buffer);
If datah<>'0' Then icxfs:=icxfs+'.'+dataH;
if (icxfs<>'') And (icno<>'121121121121121')
And (Length(icno)=10) Then
begin
label4.Caption:=inttostr(readdr);
label5.Caption:=icno;
label6.Caption:=icxfs;
qu_jbxx.SQL.Text:='select * from jbxx where 有效=1 and 卡号='''+icno+''''
+' and 余额>='+icxfs;
qu_jbxx.Open;
if qu_jbxx.IsEmpty then mscomm1.Output:=chr(readdr)+chr(2)+chr(5)
else begin
if strtocurr(icxfs)>0 then
edit1.Text:=inttostr(strtoint(edit1.Text)+1);
isbt:=(copy(panel1.Caption,1,2)='中')
and (copy(qu_jbxx.fieldbyname('人员编号').AsString,1,1)='L') and
((tb_info.Fieldbyname('优惠金额').AsCurrency+qu_jbxx.Fieldbyname('补贴金额').AsCurrency)
<=tb_info.Fieldbyname('每月优惠').AsCurrency) and (not qu_jbxx.Fieldbyname('当日补贴').asboolean);
if strtocurr(icxfs)>0 then
begin
qu_jbxx.Edit;
qu_jbxx.Fieldbyname('余额').AsCurrency:=qu_jbxx.Fieldbyname('余额').AsCurrency-strtofloat(icxfs);
ado_com.CommandText:='insert into zw values('''+icno+''','''+datetimetostr(now())
+''',''支出'','''+copy(panel1.Caption,1,2)+''',-'+icxfs+')';
ado_com.Execute;
edit2.Text:=currtostr(strtocurr(edit2.Text)+strtocurr(icxfs));
if isbt then begin
qu_jbxx.Fieldbyname('当日补贴').asboolean:=true;
ado_com.CommandText:='insert into zw values('''+icno+''','''+datetimetostr(now())
+''',''补贴'',''无'','+tb_info.fieldbyname('优惠金额').AsString+')';
ado_com.Execute;
qu_jbxx.Fieldbyname('补贴金额').AsCurrency:=qu_jbxx.Fieldbyname('补贴金额').AsCurrency
+tb_info.fieldbyname('优惠金额').AsCurrency;
qu_jbxx.Fieldbyname('余额').AsCurrency:=qu_jbxx.Fieldbyname('余额').AsCurrency
+tb_info.fieldbyname('优惠金额').AsCurrency;
edit2.Text:=currtostr
(strtocurr(edit2.Text)-tb_info.fieldbyname('优惠金额').ascurrency);
end;
qu_jbxx.Post;
end;
ss:=qu_jbxx.fieldbyname('余额').AsString;
if copy(ss,length(ss)-1,1)='.' then
ss:=copy(ss,1,length(ss)-2)+copy(ss,length(ss),1)
else ss:=ss+'0';
if length(ss)<6 then
for i:=1 to 6-length(ss) do ss:='0'+ss;
mscomm1.Output:=chr(readdr)+chr(5)+chr(2)+chr(strtoint(copy(ss,1,2)))
+chr(strtoint(copy(ss,3,2)))+chr(strtoint(copy(ss,5,2)));
end;
end;
end;
end;
end;
 
谢谢pipi,我已经测试通过,请指教源代码
 
后退
顶部