庆贺,现在我已经成功实现了串口通讯,将记费部分的源代码贴出供大家指教。
欢迎来访原创软件区 http://ycrj.delphibbs.com
使用了MSCOMM32.OCX控件,下面是ONCOMM事件的源代码,即实时记费过程,其中要判断是否为员工卡,
如果是员工卡中餐消费有相应的补贴。通讯协议就是上面说的。
procedure TForm1.MSComm1Comm(Sender: TObject);
var icn,icno,icxfs,dataH,ss:string;
buffer:variant
ovTmp
levariant;
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;