关于线程的问题.(50分)

  • 主题发起人 DiamondKing
  • 开始时间
在非主线程中用ADO可以这样:
在线程执行中,
CoInitliaze
动态创建ADOConnction和ADOQuery.
执行ADOQuery.
释放内存
UnCoInitliaze

 
在非主线程里用类似哈欠的方法,create一个TADOdataset,在excute里完成写入access里
已经连续测试了一周了,数据库有80M,似乎没有什么问题
 
哈欠:CoInitliaze ,UnCoInitliaze 主要起什么作用呢?
我直接动态创建Ado好像也可以运行.不过当前客户数只有几个.
 
我刚做了一个类似的项目,也时多线程的,我觉得还是挺好使的。
是关于串口通讯的,其中也有ADO数据处理和BDE数据处理!
unit pos_thread;
interface
uses
Classes,Graphics,Qforms,sysutils,variants,windows;
type
pos_cj = class(TThread)
private
COM:string;
//flag_true:boolean;
pos_handle:integer;
sys_232:string;
{ Private declarations }
protected
procedure sjcj;
procedure Execute;
override;
public
constructor create(COM_232:string);
end;

implementation
uses Pos, Urfdll, fun, datamodulefrm, report, datamoduledic_frm;
{ Important: Methods and properties of objects in VCL or CLX can only be used
in a method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure pos.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end;
}
{ pos }
constructor pos_cj.create(COM_232:string);
begin
COM:=COM_232;
inherited create(true);
freeonterminate:=true;
end;

procedure pos_cj.sjcj;
var
code,i,j,k,count_cs,len,count_i,count_jyzm1,count_jyzm2:integer;
jyzm_count:integer;
sj:string;
retp:array [0..255] of char;
send:pchar;
retstring:string;
blh,stbh,spfebh,rq,ccbh,cc,cpbh,dcybh,zzzbh,lbbh,zfbz,bzbh:string;
kh,xm,place,bq,bf,bz,spfl,messname,zfs,menuname,zzzname:string;
danjia,xfje:real;
stmc,dclb,dcyxm,space,down_cs1,csbm,str:string;
insertstr,jyzmstr:string;
fs,xfe,xfye:real;
flag_loop:boolean;
chr1,chr2:char;
libhandle:Thandle;
begin
try
count_jyzm1:=0;
count_jyzm2:=0;
//pos_frm.flag_true:=true;
pos_frm.startbtn.Enabled:=false;
// pos_frm.statusm.Lines.Clear;
pos_frm.public_flag_true:=true;
pos_handle:=link_open(COM,19200,0,1);
libhandle:=loadlibrary('easytalk.dll');
//getprocessaddress
if pos_handle<0 then
begin
pos_frm.statusm.Lines.Add(COM+'链路不通,不能通讯!');
//warndataerror('链路不通,不能通讯!');
link_close(pos_handle);
exit;
end
else
begin
pos_frm.statusM.Lines.Add(inttostr(pos_handle));
pos_frm.statusm.Lines.Add(COM+'打开成功!');
end;
//pos_frm.jyzm.Lines.Clear;
count_i:=0;
//------采集数据----------------------------------
if pos_frm.gatherrb.Checked then
begin
pos_frm.statusm.Font.Color:=clblue;
pos_frm.statusm.Lines.Add('正在准备接受数据标示(订餐记录)');;
while pos_frm.flag_truedo
begin
application.ProcessMessages;
begin
if pos_frm.flag_true=false then
break;
end;
code:=link_read(pos_handle,retp,12,1000);
if code<=0 then
begin
if count_i=10 then
break;
count_i:=count_i+1;
sleep(100);
pos_frm.statusM.Lines.Clear;
pos_frm.statusM.Lines.Add('采集数据等待...'+inttostr(count_i));
continue;
end;
if code>0 then
break;
end;
//while
retstring:=string(retp);
if ((code>0) and (copy(retstring,1,8)='订餐记录')) then
begin
pos_frm.statusm.Font.Color:=clgreen;
pos_frm.statusm.Lines.Add('上传标示成功--订餐记录');
jyzm_count:=strtoint(copy(retstring,9,4));
end
else
begin
pos_frm.statusm.Font.Color:=clred;
pos_frm.statusm.Lines.Add('上传标示失败--订餐记录');
link_close(pos_handle);
exit;
end;
//if
pos_frm.statusbar1.Panels[2].Text:='正在发送计算机应答标记<OK>';
while pos_frm.flag_truedo
begin
application.ProcessMessages;
begin
if pos_frm.flag_true=false then
break;
end;
code:=link_write(pos_handle,'OK',2);
if code<0 then
begin
// pos_frm.statusM.Lines.Add('2');
continue;
end;
if code>=0 then
begin
pos_frm.statusm.Font.Color:=clgreen;
pos_frm.statusm.Lines.Add('计算机应答成功<ok>');
pos_frm.statusm.lines.Add('计算机准备接受'+inttostr(jyzm_count)+'条交易帐目');
break;
end;
end;
//while
if code<0 then
begin
pos_frm.statusm.Font.Color:=clred;
pos_frm.statusm.Lines.Add('计算机应答不成功<OK>');
link_close(pos_handle);
exit;
end;
pos_frm.statusm.Lines.Add('正在接受交易帐目数据......');
with datamodulefrm1.jyzmQ do
begin
close;sql.Clear;
sql.Text:='delete from jyzm where 采集日期<'+''''+
formatdatetime('yyyy.mm.dd',now)+'''';
execsql;
end;
for k:=1 to 2do
begin
for i:=1 to jyzm_countdo
begin
while pos_frm.flag_truedo
begin
application.ProcessMessages;
begin
if pos_frm.flag_true=false then
break;
end;
code:=link_read(pos_handle,retp,60,1000);
pos_frm.statusM.Lines.Add(':'+string(retp)+inttostr(jyzm_count));
for j:=0 to 59do
begin
if retp[j]=#0 then
retp[j]:=#30;
chr1:=retp[14];
chr2:=retp[15];
sj:=copy('0'+inttostr(ord(chr1)),length(inttostr(ord(chr1))),2)+':'+
copy('0'+inttostr(ord(chr2)),length(inttostr(ord(chr2))),2)+':'+'00';
end;
pos_frm.statusm.Font.Color:=clblue;
//pos_frm.statusm.Lines.Add(inttostr(length(string(retp))));
// pos_frm.statusm.Lines.Add(inttostr(code));
if (code<=0)or(trim(string(retp))='') then
begin
count_jyzm1:=count_jyzm1+1;
if count_jyzm1>=2 then
break;
// pos_frm.statusM.Lines.Add(inttostr(count_jyzm1)+' 3');
pos_frm.statusM.Lines.Add('稍侯......');
continue;
end;
if (code<=0) then
begin
count_jyzm2:=count_jyzm2+1;
if count_jyzm2>=2 then
break;
//pos_frm.statusM.Lines.Add('4');
continue;
end;
if (code>0)and(trim(retp)='') then
begin
pos_frm.statusM.Lines.Add(inttostr(code));
// pos_frm.statusM.Lines.Add(':::::::');
continue;
end;
if (code>0) then
begin
//pos_frm.statusM.Lines.Add('::;;;;::::');
break;
end;
end;
//while
while pos_frm.flag_truedo
begin
begin
application.ProcessMessages;
if pos_frm.flag_true=false then
break;
end;
code:=link_write(pos_handle,'OK',2);
if code<>2 then
begin
pos_frm.statusM.Lines.Add('5');
continue;
end;
if code=2 then
begin
//pos_frm.statusM.Lines.Add('!!!!!!!!!!!!!!!!!');
break;
end;
end;
//while
if code<0 then
begin
pos_frm.statusm.Font.Color:=clred;
pos_frm.statusm.Lines.Add('采集交易帐目数据失败');
link_close(pos_handle);
exit;
end;
pos_frm.statusm.Lines.Add('采集中.......');
retstring:=trim(retp);
jyzmstr:=copy(retstring,1,14)+sj+copy(retstring,17,44);
if length(retstring)=60 then
begin
//pos_frm.statusm.Lines.Add('采集中2.......');
blh:=copy(retstring,1,14);
//病例号
with datamodulefrm1.ADOQ_temp1do
begin
close;sql.Clear;
sql.Text:='select * from persondocument where 病例号='+''''+ blh+'''';
open;
if recordcount>0 then
begin
if fieldvalues['挂失标记']='Y' then
begin
kh:=fieldvalues['卡号'];
xm:='挂失卡消费';
place:=fieldvalues['地点'];
bq:=fieldvalues['病区'];
bf:=fieldvalues['病房号'];
bz:=fieldvalues['病床号'];
end
else
begin
kh:=fieldvalues['卡号'];
xm:=fieldvalues['姓名'];
place:=fieldvalues['地点'];
bq:=fieldvalues['病区'];
bf:=fieldvalues['病房号'];
bz:=fieldvalues['病床号'];
end;
end
else
begin
xm:='黑卡消费';
end;
//if
end;
//with
// pos_frm.statusm.Lines.Add('采集1.......');
spfebh:=trim(copy(retstring,17,2));
//食品分类编号
with datamodulefrm1.ADOQ_temp1do
begin
close;sql.Clear;
sql.Text:='select * from food_lb where 食品编码='+''''+spfebh+'''';
open;
if recordcount>0 then
spfl:=fieldvalues['食品种类']
else
spfl:='未设定';
end;
stbh:=copy(retstring,19,1);
//食堂编号
with datamodulefrm1.ADOQ_temp1do
begin
close;sql.Clear;
sql.Text:='select * from mess where 食堂编码='+''''+stbh+'''';
open;
if recordcount>0 then
messname:=fieldvalues['食堂名称']
else
messname:='未设定';
end;
//spfebh:=copy(retstring,20,1);
//食品分类编号
rq:=formatdatetime('yyyy.',now)+copy(retstring,21,2)+'.'+copy(retstring,23,2);
//日期
ccbh:=copy(retstring,25,1);
//餐次
with datamodulefrm1.ADOQ_temp1do
begin
close;sql.Clear;
sql.Text:='select 餐次 from dining where 餐次编码='+''''+ccbh+'''';
open;
if recordcount>0 then
cc:=fieldvalues['餐次']
else
cc:='未设定';
end;
cpbh:=copy(retstring,26,4);
//菜品编号
try
if not varisnull(trim(copy(retstring,37,7))) then
xfe:=strtofloat(trim(copy(retstring,37,7)))//消费额
else
xfe:=0;//消费额
except
xfe:=0;
end;
//pos_frm.statusm.Lines.Add('采集2.......');
with datamodulefrm1.ADOQ_temp1do
begin
close;sql.Clear;
sql.Text:='select * from m_food where 主食编码='+''''+cpbh+'''';
open;
if recordcount>0 then
zfs:='主食';
close;sql.Clear;
sql.Text:='select * from f_food where 副食编码='+''''+cpbh+'''';
open;
if recordcount>0 then
zfs:='副食';
close;sql.Clear;
sql.Text:='select * from food_tc where 套餐编码='+''''+cpbh+'''';
open;
if recordcount>0 then
zfs:='套餐';
close;sql.Clear;
sql.Text:='select * from menu_sc where 食谱编码='+''''+cpbh+'''';
open;
if recordcount>0 then
begin
menuname:=fieldvalues['名称'];
danjia:=fieldvalues['单价'];
end
else
begin
if cpbh='9999' then
begin
zfs:='未知';
menuname:='机动菜';
danjia:=xfe;
end
else
begin
menuname:='无此菜';
danjia:=1;
end;
end;
end;
//with
//pos_frm.statusm.Lines.Add('采集3.......');
if varisnull(trim(copy(retstring,30,4))) then
fs:=1
else
fs:=strtofloat(trim(copy(retstring,30,4)));//份数
xfje:=fs*danjia;
dcybh:=copy(retstring,34,2);
//订餐员编号
//pos_frm.statusm.Lines.Add('采集4.......');
with datamodulefrm1.ADOQ_temp1do
begin
close;sql.Clear;
sql.Text:='select 订餐员姓名 from food_operator where 订餐员编码='+
''''+dcybh+'''';
open;
if recordcount>0 then
begin
if varisnull(fieldvalues['订餐员姓名']) then
dcyxm:='未设定'
else
dcyxm:=fieldvalues['订餐员姓名'];
end
else
dcyxm:='未设定';
end;
//pos_frm.statusm.Lines.Add('采集中3.......');
zzzbh:=copy(retstring,36,1);//制作组编号
with datamodulefrm1.ADOQ_temp1do
begin
close;sql.Clear;
sql.Text:='select * from class_group where 编码='+''''+zzzbh+'''';
open;
if recordcount>0 then
zzzname:=fieldvalues['组']
else
zzzname:='未设定';
end;
try
if varisnull(trim(copy(retstring,44,7))) then
xfye:=0
else
xfye:=strtofloat(trim(copy(retstring,44,7)));//消费余额
except
xfye:=0;
end;
lbbh:=copy(retstring,51,1);//定餐类别
with datamodulefrm1.ADOQ_temp1do
begin
close;sql.Clear;
sql.Text:='select 类别 from orbit_lb where 类别编码='+''''+lbbh+'''';
open;
if recordcount>0 then
dclb:=fieldvalues['类别']
else
dclb:='未设定';
end;
zfbz:=copy(retstring,52,1);//正负标志
bzbh:='BY'+copy(retstring,53,8);//病房编号
flag_loop:=false;//**********************************
with datamodulefrm1.jyzmQ do
begin
close;
sql.Clear;
sql.Text:=' select * from jyzm where 交易帐目='+''''+jyzmstr+'''';
open;
if Recordcount>0 then
continue;
close;
sql.Clear;
sql.Text:=' insert into jyzm values('+
''''+trim(jyzmstr)+''''+','+
''''+trim(blh)+''''+','+
''''+trim(ccbh)+''''+','+
''''+trim(dclb)+''''+','+
''''+formatdatetime('yyyy.mm.dd',now)+''''+','+
''''+formatdatetime('hh:mm:ss',now)+''''+','+
''''+trim(rq)+''''+','+''''+sj+''''+','+
''''+trim(dcyxm)+''''+')';
execsql;
end;
//with jyzmQ
space:=' ';
//jyzm.Lines.Add(retstring);
blh:=copy(blh+space,1,16);
rq:=copy(rq+space,1,10);
cc:=copy(cc+space,1,6);
dcyxm:=copy(dcyxm+space,1,12);
insertstr:=(blh+' '+rq+' '+cc+' '+
copy(floattostr(xfe)+space,1,7)+' '+
copy(floattostr(xfye)+space,1,7)+' '+dcyxm);
if insertstr<>'' then
pos_frm.jyzm.Lines.Add(insertstr+' '+COM);
//-----------------------------consum-----------------------------
with datamodulefrm1.ADOQ_temp2do
begin
close;sql.Clear;
if (lbbh='0')or(lbbh='1') then
begin
sql.Text:='insert into consum values('+
''''+blh+''''+','+''''+kh+''''+','+''''+xm+''''+','+
''''+place+''''+','+''''+bq+''''+','+''''+bf+''''+','+
''''+bz+''''+','+''''+bzbh+''''+','+''''+rq+''''+','+
''''+sj+''''+','+
''''+cc+''''+','+''''+zfs+''''+','+''''+cpbh+''''+','+
''''+menuname+''''+','+''''+spfl+''''+','+
''+floattostr(fs)+''+','+''+floattostr(danjia)+''+','+
''+floattostr(xfe)+''+','+''+floattostr(xfye)+''+','+''''+dcyxm+''''+','+
''''+zzzname+''''+','+''''+messname+''''+','+
''''+dclb+''''+','+'''N'''+','+''''+pos_frm.sys_operator+''''+
')';
end;
if lbbh='2' then
begin
sql.Text:='insert into consum values('+
''''+blh+''''+','+''''+kh+''''+','+''''+xm+''''+','+
''''+place+''''+','+''''+bq+''''+','+''''+bf+''''+','+
''''+bz+''''+','+''''+bzbh+''''+','+''''+rq+''''+','+
''''+sj+''''+','+
''''+cc+''''+','+''''+zfs+''''+','+''''+cpbh+''''+','+
''''+menuname+''''+','+''''+spfl+''''+','+
''+floattostr(-fs)+''+','+''+floattostr(danjia)+''+','+
''+floattostr(-xfe)+''+','+''+floattostr(xfye)+''+','+''''+dcyxm+''''+','+
''''+zzzname+''''+','+''''+messname+''''+','+
''''+dclb+''''+','+'''N'''+','+''''+pos_frm.sys_operator+''''+
')';
end;
execsql;
end;
//with
end;
//if length(retstring)=60
end;
//for
//pos_frm.statusm.Lines.Add('采集交易帐目中1......');
with datamodulereport.ADOSP_consum_fldo
begin
close;
execproc;
end;
//freemem(retp,2000);
if i=jyzm_count then
begin
sleep(1000);
while truedo
begin
application.ProcessMessages;
begin
if pos_frm.flag_true=false then
break;
end;
code:=link_write(handle,'OK',2);
pos_frm.statusm.Lines.Add('code='+inttostr(code));
if code<>2 then
continue;
if code=2 then
begin
if k=2 then
begin
pos_frm.statusM.Lines.Add('COM'+inttostr(count_i)+'采集完毕');
break;
end;
end;
end;
end;
end;
//for k:=1 to 2
pos_frm.statusM.Lines.Add(COM+'采集完毕');
if pos_frm.autochk.Checked then
pos_frm.down_menu.Checked:=true;
end;
//if 采集
//-------------下载参数--------------------------------------
//----------餐次-------------
if pos_frm.down_cs.Checked then
begin
pos_frm.statusm.Font.Color:=clblue;
pos_frm.statusm.Lines.Add('下载参数');
count_i:=0;
while pos_frm.flag_truedo
begin
application.ProcessMessages;
begin
if pos_frm.flag_true=false then
break;
end;
code:=link_read(pos_handle,retp,12,1000);
if code<=0 then
begin
if count_i=10 then
break;
count_i:=count_i+1;
sleep(100);
pos_frm.statusM.Lines.Clear;
pos_frm.statusM.Lines.Add('下载参数等待...'+inttostr(count_i));
continue;
end;
if code>0 then
break;
end;
if copy(retp,1,8)='餐次定义' then
begin
pos_frm.statusm.Font.Color:=clblue;
pos_frm.statusm.Lines.Add('读取标示成功[餐次定义]');
with datamodulefrm1.ADOQ_temp1do
begin
close;sql.Clear;
sql.Text:='select count(餐次) 个数 from dining';
open;
if recordcount<=0 then
begin
pos_frm.statusm.Font.Color:=clred;
pos_frm.statusm.Lines.Add('餐次未设定');
link_close(pos_handle);
exit;
end;
count_cs:=fieldvalues['个数'];
end;
//with
space:=' ';
getmem(send,20);
send:=pchar('OK'+copy(space+inttostr(count_cs),length(inttostr(count_cs))+1,4));
code:=link_write(pos_handle,send,6);
if code<0 then
begin
pos_frm.statusm.Font.Color:=clred;
pos_frm.statusm.Lines.Add('计算机应答不通[发送餐次个数]');
link_close(handle);
exit;
end;
pos_frm.statusm.Font.Color:=clblue;
pos_frm.statusm.Lines.Add('计算机应答成功[发送餐次个数]');
pos_frm.statusm.Lines.Add('准备发送餐次个数'+inttostr(count_cs)+'个');
while truedo
begin
application.ProcessMessages;
begin
if pos_frm.flag_true=false then
break;
end;
code:=link_read(pos_handle,retp,2,100);
//statusm.Lines.Add('code='+inttostr(code));
if copy(string(retp),1,2)<>'OK' then
continue;
if copy(string(retp),1,2)='OK' then
break;
end;
//while
if copy(string(retp),1,2)='OK' then
begin
pos_frm.statusm.Font.Color:=clblue;
pos_frm.statusm.Lines.Add('手持机应答成功[ok]');
with datamodulefrm1.ADOQ_temp2do
begin
close;sql.Clear;
sql.Text:='select 餐次,餐次编码 from dining';
open;
first;
while not eofdo
begin
do
wn_cs1:=fieldvalues['餐次'];
csbm:=fieldvalues['餐次编码'];
retp:='';
code:=link_read(pos_handle,retp,2,1000);
if copy(string(retp),1,2)='OK' then
begin
space:=' ';
send:=pchar(trim(csbm)+copy(down_cs1+space,1,6));
pos_frm.jyzm.Lines.Add('下载:'+send);
while pos_frm.flag_truedo
begin
application.ProcessMessages;
begin
if pos_frm.flag_true=false then
break;
end;
code:=link_write(pos_handle,send,7);
if code=7 then
begin
pos_frm.statusm.Font.Color:=clblue;
pos_frm.statusm.Lines.Add('餐次下载成功');
break;
end;
end;
end
else
begin
pos_frm.statusm.Font.Color:=clred;
pos_frm.statusm.Lines.Add('手持机应答不成功[ok]');
link_close(handle);
exit;
end;
next;
end;
//while
end;
end
else
begin
pos_frm.statusm.Font.Color:=clred;
pos_frm.statusm.Lines.Add('手持机应答不成功[下载餐次]');
link_close(pos_handle);
exit;
end;
//---------------制作组----------------------
while pos_frm.flag_truedo
begin
application.ProcessMessages;
begin
if pos_frm.flag_true=false then
break;
end;
code:=link_read(pos_handle,retp,12,1000);
if code<=0 then
continue;
if code>0 then
break;
end;
if copy(retp,1,8)='制作定义' then
begin
pos_frm.statusm.Font.Color:=clblue;
pos_frm.statusm.Lines.Add('读取标示成功[制作组定义]');
with datamodulefrm1.ADOQ_temp1do
begin
close;sql.Clear;
sql.Text:='select count(组) 个数 from class_group';
open;
if recordcount<=0 then
begin
pos_frm.statusm.Font.Color:=clred;
pos_frm.statusm.Lines.Add('制作组未设定');
link_close(pos_handle);
exit;
end;
count_cs:=fieldvalues['个数'];
end;
//with
space:=' ';
getmem(send,20);
send:=pchar('OK'+copy(space+inttostr(count_cs),length(inttostr(count_cs))+1,4));
code:=link_write(pos_handle,send,6);
if code<0 then
begin
pos_frm.statusm.Font.Color:=clred;
pos_frm.statusm.Lines.Add('计算机应答不通[发送制作组个数]');
link_close(handle);
exit;
end;
pos_frm.statusm.Font.Color:=clblue;
pos_frm.statusm.Lines.Add('计算机应答成功[发送制作组个数]');
pos_frm.statusm.Lines.Add('准备发送制作组个数'+inttostr(count_cs)+'个');
while truedo
begin
application.ProcessMessages;
begin
if pos_frm.flag_true=false then
break;
end;
code:=link_read(pos_handle,retp,2,100);
//statusm.Lines.Add('code='+inttostr(code));
if copy(string(retp),1,2)<>'OK' then
continue;
if copy(string(retp),1,2)='OK' then
break;
end;
//while
if copy(string(retp),1,2)='OK' then
begin
pos_frm.statusm.Font.Color:=clblue;
pos_frm.statusm.Lines.Add('手持机应答成功[ok]');
with datamodulefrm1.ADOQ_temp2do
begin
close;sql.Clear;
sql.Text:='select 组,编码 from class_group';
open;
first;
while not eofdo
begin
do
wn_cs1:=fieldvalues['组'];
csbm:=fieldvalues['编码'];
retp:='';
code:=link_read(pos_handle,retp,2,1000);
if copy(string(retp),1,2)='OK' then
begin
space:=' ';
send:=pchar(trim(csbm)+copy(down_cs1+space,1,6));
pos_frm.jyzm.Lines.Add('下载:'+send);
while pos_frm.flag_truedo
begin
application.ProcessMessages;
begin
if pos_frm.flag_true=false then
break;
end;
code:=link_write(pos_handle,send,7);
if code=7 then
begin
pos_frm.statusm.Font.Color:=clblue;
pos_frm.statusm.Lines.Add('制作组下载成功');
break;
end;
end;
end
else
begin
pos_frm.statusm.Font.Color:=clred;
pos_frm.statusm.Lines.Add('手持机应答不成功[ok]');
link_close(pos_handle);
exit;
end;
next;
end;
//while
end;
end
//------------------
end
else
begin
pos_frm.statusm.Font.Color:=clred;
pos_frm.statusm.Lines.Add('手持机应答不成功[制作组定义]');
link_close(pos_handle);
exit;
end;
//----------------食品分类------------------------------
while pos_frm.flag_truedo
begin
application.ProcessMessages;
begin
if pos_frm.flag_true=false then
break;
end;
code:=link_read(pos_handle,retp,12,1000);
if code<=0 then
begin
continue;
end;
if code>0 then
break;
end;
if copy(retp,1,8)='食品分类' then
begin
pos_frm.statusm.Font.Color:=clblue;
pos_frm.statusm.Lines.Add('读取标示成功[食品分类]');
with datamodulefrm1.ADOQ_temp1do
begin
close;sql.Clear;
sql.Text:='select count(食品种类) 个数 from food_lb';
open;
if recordcount<=0 then
begin
pos_frm.statusm.Font.Color:=clred;
pos_frm.statusm.Lines.Add('食品种类设定');
link_close(pos_handle);
exit;
end;
count_cs:=fieldvalues['个数'];
end;
//with
space:=' ';
getmem(send,20);
send:=pchar('OK'+copy(space+inttostr(count_cs),length(inttostr(count_cs))+1,4));
code:=link_write(pos_handle,send,6);
if code<0 then
begin
pos_frm.statusm.Font.Color:=clred;
pos_frm.statusm.Lines.Add('计算机应答不通[发送食品种类个数]');
link_close(pos_handle);
exit;
end;
pos_frm.statusm.Font.Color:=clblue;
pos_frm.statusm.Lines.Add('计算机应答成功[发送食品种类个数]');
pos_frm.statusm.Lines.Add('准备发送食品种类个数'+inttostr(count_cs)+'个');
while truedo
begin
application.ProcessMessages;
begin
if pos_frm.flag_true=false then
break;
end;
code:=link_read(pos_handle,retp,2,100);
//statusm.Lines.Add('code='+inttostr(code));
if copy(string(retp),1,2)<>'OK' then
continue;
if copy(string(retp),1,2)='OK' then
break;
end;
//while
if copy(string(retp),1,2)='OK' then
begin
pos_frm.statusm.Font.Color:=clblue;
pos_frm.statusm.Lines.Add('手持机应答成功[ok]');
with datamodulefrm1.ADOQ_temp2do
begin
close;sql.Clear;
sql.Text:='select 食品种类,食品编码 from food_lb';
open;
first;
while not eofdo
begin
do
wn_cs1:=fieldvalues['食品种类'];
csbm:=fieldvalues['食品编码'];
retp:='';
code:=link_read(pos_handle,retp,2,1000);
if copy(string(retp),1,2)='OK' then
begin
space:=' ';
send:=pchar(trim(csbm)+copy(down_cs1+space,1,5));
pos_frm.jyzm.Lines.Add('下载:'+send);
while pos_frm.flag_truedo
begin
application.ProcessMessages;
begin
if pos_frm.flag_true=false then
break;
end;
code:=link_write(pos_handle,send,5);
if code=5 then
begin
pos_frm.statusm.Font.Color:=clblue;
pos_frm.statusm.Lines.Add('食品种类下载成功');
break;
end;
end;
end
else
begin
pos_frm.statusm.Font.Color:=clred;
pos_frm.statusm.Lines.Add('手持机应答不成功[食品种类]');
link_close(pos_handle);
exit;
end;
next;
end;
//while
end;
end
end
else
begin
pos_frm.statusm.Font.Color:=clred;
pos_frm.statusm.Lines.Add('手持机应答不成功[食品分类定义]');
link_close(pos_handle);
exit;
end;
//-------------------下载卡底金------------------------------------
while pos_frm.flag_truedo
begin
application.ProcessMessages;
begin
if pos_frm.flag_true=false then
break;
end;
code:=link_read(pos_handle,retp,12,1000);
if code<=0 then
continue;
if code>0 then
break;
end;
if copy(retp,1,8)='卡片底金' then
begin
pos_frm.statusm.Font.Color:=clblue;
pos_frm.statusm.Lines.Add('读取标示成功[卡片底金]');
with datamodulefrm2.local_table do
begin
open;
if recordcount<=0 then
begin
pos_frm.statusm.Font.Color:=clred;
pos_frm.statusm.Lines.Add('卡底金未设定');
link_close(pos_handle);
exit;
end;
do
wn_cs1:=floattostr(fieldvalues['卡底金']);
end;
//with
send:='OK 1';
code:=link_write(pos_handle,send,6);
if code<0 then
begin
pos_frm.statusm.Font.Color:=clred;
pos_frm.statusm.Lines.Add('计算机应答不通[卡底金]');
link_close(pos_handle);
exit;
end;
if code>0 then
begin
pos_frm.statusm.Font.Color:=clblue;
pos_frm.statusm.Lines.Add('计算机应答通[卡底金]');
end;
while truedo
begin
application.ProcessMessages;
begin
if pos_frm.flag_true=false then
break;
end;
code:=link_read(pos_handle,retp,2,100);
if copy(string(retp),1,2)<>'OK' then
continue;
if copy(string(retp),1,2)='OK' then
break;
end;
//while
if copy(string(retp),1,2)='OK' then
begin
pos_frm.statusm.Font.Color:=clblue;
pos_frm.statusm.Lines.Add('手持机应答成功[ok]');
code:=link_read(pos_handle,retp,2,1000);
if copy(string(retp),1,2)='OK' then
begin
send:=pchar(down_cs1);
pos_frm.jyzm.Lines.Add('下载:'+send);
while pos_frm.flag_truedo
begin
application.ProcessMessages;
begin
if pos_frm.flag_true=false then
break;
end;
code:=link_write(pos_handle,send,4);
if code=4 then
begin
pos_frm.statusm.Font.Color:=clblue;
pos_frm.statusm.Lines.Add('下载卡底金成功');
break;
end;
end;
end
else
begin
pos_frm.statusm.Font.Color:=clred;
pos_frm.statusm.Lines.Add('手持机应答不成功[下载卡底金]');
link_close(pos_handle);
exit;
end;
end;
end
else
begin
pos_frm.statusm.Font.Color:=clred;
pos_frm.statusm.Lines.Add('手持机应答不成功[下载卡底金]');
link_close(pos_handle);
exit;
end;
//-----------------------系统标示-----------------------------------
while pos_frm.flag_truedo
begin
application.ProcessMessages;
begin
if pos_frm.flag_true=false then
break;
end;
code:=link_read(pos_handle,retp,12,1000);
if code<=0 then
continue;
if code>0 then
break;
end;
if copy(retp,1,8)='系统标识' then
begin
pos_frm.statusm.Font.Color:=clblue;
pos_frm.statusm.Lines.Add('读取标示成功[系统标示]');
with datamodulefrm2.local_table do
begin
open;
if recordcount<=0 then
begin
pos_frm.statusm.Font.Color:=clred;
pos_frm.statusm.Lines.Add('系统标示未设定');
link_close(pos_handle);
exit;
end;
do
wn_cs1:=fieldvalues['系统标示'];
end;
//with
send:='OK 1';
code:=link_write(pos_handle,send,6);
if code<0 then
begin
pos_frm.statusm.Font.Color:=clred;
pos_frm.statusm.Lines.Add('计算机应答不通[系统标示]');
link_close(pos_handle);
exit;
end;
if code>0 then
begin
pos_frm.statusm.Font.Color:=clblue;
pos_frm.statusm.Lines.Add('计算机应答通[系统标示]');
end;
while truedo
begin
application.ProcessMessages;
begin
if pos_frm.flag_true=false then
break;
end;
code:=link_read(pos_handle,retp,2,100);
if copy(string(retp),1,2)<>'OK' then
continue;
if copy(string(retp),1,2)='OK' then
break;
end;
//while
if copy(string(retp),1,2)='OK' then
begin
pos_frm.statusm.Font.Color:=clblue;
pos_frm.statusm.Lines.Add('手持机应答成功[ok]');
code:=link_read(pos_handle,retp,2,1000);
if copy(string(retp),1,2)='OK' then
begin
send:=pchar(down_cs1);
pos_frm.jyzm.Lines.Add('下载:'+send);
while pos_frm.flag_truedo
begin
application.ProcessMessages;
begin
if pos_frm.flag_true=false then
break;
end;
code:=link_write(pos_handle,send,8);
if code=8 then
begin
pos_frm.statusm.Font.Color:=clblue;
pos_frm.statusm.Lines.Add('下载系统标示成功');
break;
end;
end;
end
else
begin
pos_frm.statusm.Font.Color:=clred;
pos_frm.statusm.Lines.Add('手持机应答不成功[下载系统标示]');
link_close(pos_handle);
exit;
end;
end;
end
else
begin
pos_frm.statusm.Font.Color:=clred;
pos_frm.statusm.Lines.Add('手持机应答不成功[下载系统标示]');
link_close(pos_handle);
exit;
end;
//---------------------下载地点-------------------------------------
while pos_frm.flag_truedo
begin
application.ProcessMessages;
begin
if pos_frm.flag_true=false then
break;
end;
code:=link_read(pos_handle,retp,12,1000);
if code<=0 then
continue;
if code>0 then
break;
end;
if copy(retp,1,8)='地点定义' then
begin
pos_frm.statusm.Font.Color:=clblue;
pos_frm.statusm.Lines.Add('读取标示成功[地点定义]');
with datamodulefrm1.ADOQ_temp1do
begin
close;sql.Clear;
sql.Text:='select count(地点编码) 个数 from place';
open;
if recordcount<=0 then
begin
pos_frm.statusm.Font.Color:=clred;
pos_frm.statusm.Lines.Add('地点设定');
link_close(pos_handle);
exit;
end;
count_cs:=fieldvalues['个数'];
end;
//with
space:=' ';
getmem(send,20);
send:=pchar('OK'+copy(space+inttostr(count_cs),length(inttostr(count_cs))+1,4));
code:=link_write(pos_handle,send,6);
if code<0 then
begin
pos_frm.statusm.Font.Color:=clred;
pos_frm.statusm.Lines.Add('计算机应答不通[地点个数]');
link_close(pos_handle);
exit;
end;
pos_frm.statusm.Font.Color:=clblue;
pos_frm.statusm.Lines.Add('计算机应答成功[发送地点个数]');
pos_frm.statusm.Lines.Add('准备发送地点个数'+inttostr(count_cs)+'个');
while truedo
begin
application.ProcessMessages;
begin
if pos_frm.flag_true=false then
break;
end;
code:=link_read(pos_handle,retp,2,100);
//statusm.Lines.Add('code='+inttostr(code));
if copy(string(retp),1,2)<>'OK' then
continue;
if copy(string(retp),1,2)='OK' then
break;
end;
//while
if copy(string(retp),1,2)='OK' then
begin
pos_frm.statusm.Font.Color:=clblue;
pos_frm.statusm.Lines.Add('手持机应答成功[ok]');
with datamodulefrm1.ADOQ_temp2do
begin
close;sql.Clear;
sql.Text:='select 地点名称,地点编码 from place';
open;
first;
while not eofdo
begin
do
wn_cs1:=fieldvalues['地点名称'];
csbm:=fieldvalues['地点编码'];
retp:='';
code:=link_read(pos_handle,retp,2,1000);
if copy(string(retp),1,2)='OK' then
begin
space:=' ';
send:=pchar(trim(csbm)+copy(down_cs1+space,1,10));
pos_frm.jyzm.Lines.Add('下载:'+send);
while pos_frm.flag_truedo
begin
application.ProcessMessages;
begin
if pos_frm.flag_true=false then
break;
end;
code:=link_write(pos_handle,send,12);
if code=12 then
begin
pos_frm.statusm.Font.Color:=clblue;
pos_frm.statusm.Lines.Add('地点下载成功');
break;
end;
end;
end
else
begin
pos_frm.statusm.Font.Color:=clred;
pos_frm.statusm.Lines.Add('手持机应答不成功[地点下载]');
link_close(pos_handle);
exit;
end;
next;
end;
//while
end;
end
end
else
begin
pos_frm.statusm.Font.Color:=clred;
pos_frm.statusm.Lines.Add('手持机应答不成功[地点下载]');
link_close(pos_handle);
exit;
end;
//--------------------下载病区--------------------------------
while pos_frm.flag_truedo
begin
application.ProcessMessages;
begin
if pos_frm.flag_true=false then
break;
end;
code:=link_read(pos_handle,retp,12,1000);
if code<=0 then
continue;
if code>0 then
break;
end;
if copy(retp,1,8)='病区定义' then
begin
pos_frm.statusm.Font.Color:=clblue;
pos_frm.statusm.Lines.Add('读取标示成功[病区定义]');
with datamodulefrm1.ADOQ_temp1do
begin
close;sql.Clear;
sql.Text:='select count(病区编码) 个数 from ill_room';
open;
if recordcount<=0 then
begin
pos_frm.statusm.Font.Color:=clred;
pos_frm.statusm.Lines.Add('病区未设定');
link_close(pos_handle);
exit;
end;
count_cs:=fieldvalues['个数'];
end;
//with
space:=' ';
getmem(send,20);
send:=pchar('OK'+copy(space+inttostr(count_cs),length(inttostr(count_cs))+1,4));
code:=link_write(pos_handle,send,6);
if code<0 then
begin
pos_frm.statusm.Font.Color:=clred;
pos_frm.statusm.Lines.Add('计算机应答不通[病区个数]');
link_close(pos_handle);
exit;
end;
pos_frm.statusm.Font.Color:=clblue;
pos_frm.statusm.Lines.Add('计算机应答成功[发送病区个数]');
pos_frm.statusm.Lines.Add('准备发送病区个数'+inttostr(count_cs)+'个');
while truedo
begin
application.ProcessMessages;
begin
if pos_frm.flag_true=false then
break;
end;
code:=link_read(pos_handle,retp,2,100);
//statusm.Lines.Add('code='+inttostr(code));
if copy(string(retp),1,2)<>'OK' then
continue;
if copy(string(retp),1,2)='OK' then
break;
end;
//while
if copy(string(retp),1,2)='OK' then
begin
pos_frm.statusm.Font.Color:=clblue;
pos_frm.statusm.Lines.Add('手持机应答成功[ok]');
with datamodulefrm1.ADOQ_temp2do
begin
close;sql.Clear;
sql.Text:='select 病区,病区编码 from ill_room';
open;
first;
while not eofdo
begin
do
wn_cs1:=fieldvalues['病区'];
csbm:=fieldvalues['病区编码'];
retp:='';
code:=link_read(pos_handle,retp,2,1000);
if copy(string(retp),1,2)='OK' then
begin
space:=' ';
send:=pchar(trim(csbm)+copy(down_cs1+space,1,10));
pos_frm.jyzm.Lines.Add('下载:'+send);
while pos_frm.flag_truedo
begin
application.ProcessMessages;
begin
if pos_frm.flag_true=false then
break;
end;
code:=link_write(pos_handle,send,12);
if code=12 then
begin
pos_frm.statusm.Font.Color:=clblue;
pos_frm.statusm.Lines.Add('病区下载成功');
break;
end;
end;
end
else
begin
pos_frm.statusm.Font.Color:=clred;
pos_frm.statusm.Lines.Add('手持机应答不成功[病区下载]');
link_close(pos_handle);
exit;
end;
next;
end;
//while
end;
end
end
else
begin
pos_frm.statusm.Font.Color:=clred;
pos_frm.statusm.Lines.Add('手持机应答不成功[病区下载]');
link_close(pos_handle);
exit;
end;
pos_frm.statusm.Font.Color:=clblue;
pos_frm.statusm.Lines.Add('参数下载成功!');
end;
//if 参数
end;
//下载黑名单
//--------------------下载菜谱----------------------------------
if pos_frm.down_menu.Checked then
begin
pos_frm.statusm.Clear;
pos_frm.statusm.Font.Color:=clblue;
pos_frm.statusm.Lines.Add('开始下载菜谱');
count_i:=0;
while pos_frm.flag_truedo
begin
application.ProcessMessages;
begin
if pos_frm.flag_true=false then
break;
end;
code:=link_read(pos_handle,retp,4,1000);
if code<=0 then
begin
if count_i=10 then
break;
count_i:=count_i+1;
sleep(100);
pos_frm.statusM.Lines.Clear;
pos_frm.statusM.Lines.Add('下载菜谱等待...'+inttostr(count_i));
continue;
end;
if code>0 then
break;
end;
if copy(retp,1,4)='菜谱' then
begin
pos_frm.statusm.Font.Color:=clblue;
pos_frm.statusm.Lines.Add('读取标示成功[下载菜谱]');
with datamodulefrm1.ADOQ_temp1do
begin
close;sql.Clear;
sql.Text:='select count(食谱编码) 个数 from menu_sc';
open;
if recordcount<0 then
begin
pos_frm.statusm.Font.Color:=clred;
pos_frm.statusm.Lines.Add('无上传食谱');
link_close(pos_handle);
exit;
end;
count_cs:=fieldvalues['个数'];
{ if count_cs>60 then
begin
statusm.Font.Color:=clred;
statusm.Lines.Add('上传食谱个数不能超过60');
link_close(pos_handle);
exit;
end;
}
end;
//with
space:=' ';
getmem(send,20);
send:=pchar('OK'+copy(space+inttostr(count_cs),length(inttostr(count_cs))+1,4));
code:=link_write(pos_handle,send,6);
if code<0 then
begin
pos_frm.statusm.Font.Color:=clred;
pos_frm.statusm.Lines.Add('计算机应答不通[菜谱个数]');
link_close(pos_handle);
exit;
end;
pos_frm.statusm.Font.Color:=clblue;
pos_frm.statusm.Lines.Add('计算机应答成功[发送菜谱个数]');
pos_frm.statusm.Lines.Add('准备发送菜谱个数'+inttostr(count_cs)+'个');
while truedo
begin
application.ProcessMessages;
begin
if pos_frm.flag_true=false then
break;
end;
code:=link_read(pos_handle,retp,2,100);
//statusm.Lines.Add('code='+inttostr(code));
if copy(string(retp),1,2)<>'OK' then
continue;
if copy(string(retp),1,2)='OK' then
break;
end;
//while
if copy(string(retp),1,2)='OK' then
begin
pos_frm.statusm.Font.Color:=clblue;
pos_frm.statusm.Lines.Add('手持机应答成功[ok]');
with datamodulefrm1.ADOQ_temp2do
begin
close;sql.Clear;
sql.Text:=' select 食谱编码,名称,单价,日期,餐次编码 '+
' from menu_sc order by 日期,餐次编码';
open;
if recordcount<=0 then
begin
pos_frm.statusm.Font.Color:=clred;
pos_frm.statusm.Lines.Add('无菜谱记录不能下载');
link_close(pos_handle);
exit;
end;
first;
while not eofdo
begin
code:=link_read(pos_handle,retp,4,1000);
if (code<0)or(trim(copy(retp,2,3))<>inttostr(recno))or(copy(retp,1,1)<>'S') then
begin
pos_frm.statusm.Font.Color:=clred;
pos_frm.statusm.Lines.Add('第'+inttostr(recno)+'号菜谱没有接到手持机发送的标志,请重发');
link_close(pos_handle);
exit;
end;
space:=' ';
str:= string(format('%3.2f',[fieldbyname('单价').asfloat]));
space:='S'+copy('000'+inttostr(recno),length(inttostr(recno))+1,3)+
fieldvalues['食谱编码']+copy(fieldvalues['名称']+space,1,16)+
copy(' '+str,length(str)+1,5)+
copy(fieldvalues['日期'],6,2)+copy(fieldvalues['日期'],9,2)+
trim(fieldvalues['餐次编码']);
pos_frm.jyzm.Lines.Add('下载菜谱:'+space);
send:=pchar(space);
while pos_frm.flag_truedo
begin
application.ProcessMessages;
begin
if pos_frm.flag_true=false then
break;
end;
code:=link_write(pos_handle,send,34);
if code=34 then
begin
pos_frm.statusm.Font.Color:=clblue;
pos_frm.statusm.Lines.Add('菜谱下载成功');
if pos_frm.autochk.Checked then
pos_frm.down_hmd.Checked:=true;
break;
end;
end;
next;
end;
//while
end;
end
end
else
begin
pos_frm.statusm.Font.Color:=clred;
pos_frm.statusm.Lines.Add('手持机应答不成功[菜谱下载]');
link_close(pos_handle);
exit;
end;
//-----------
//-------------下载黑名单-----------------------------------------------
if pos_frm.down_hmd.Checked then
begin
pos_frm.statusm.Clear;
pos_frm.statusm.Font.Color:=clblue;
pos_frm.statusm.Lines.Add('准备下载黑名单');
count_i:=0;
while pos_frm.flag_truedo
begin
application.ProcessMessages;
begin
if pos_frm.flag_true=false then
break;
end;
code:=link_read(pos_handle,retp,8,1000);
if code<=0 then
begin
if count_i=10 then
break;
count_i:=count_i+1;
sleep(100);
pos_frm.statusM.Lines.Clear;
pos_frm.statusM.Lines.Add('下载挂失等待...'+inttostr(count_i));
continue;
end;
if code>0 then
break;
end;
if copy(retp,1,4)='黑单' then
begin
pos_frm.statusm.Font.Color:=clblue;
pos_frm.statusm.Lines.Add('读取标示成功[下载黑名单]');
with datamodulefrm1.ADOQ_temp1do
begin
close;sql.Clear;
sql.Text:='select count(病例号) 个数 from loss_card';
open;
if recordcount<=0 then
begin
pos_frm.statusm.Font.Color:=clred;
pos_frm.statusm.Lines.Add('无挂失名单');
link_close(pos_handle);
exit;
end;
count_cs:=fieldvalues['个数'];
end;
//with
space:=' ';
getmem(send,20);
send:=pchar('OK'+copy(space+inttostr(count_cs),length(inttostr(count_cs))+1,4));
code:=link_write(pos_handle,send,6);
if code<0 then
begin
pos_frm.statusm.Font.Color:=clred;
pos_frm.statusm.Lines.Add('计算机应答不通[黑名单个数]');
link_close(pos_handle);
exit;
end;
pos_frm.statusm.Font.Color:=clblue;
pos_frm.statusm.Lines.Add('计算机应答成功[发送黑名单个数]');
pos_frm.statusm.Lines.Add('准备发送黑名单个数'+inttostr(count_cs)+'个');
while truedo
begin
application.ProcessMessages;
begin
if pos_frm.flag_true=false then
break;
end;
code:=link_read(pos_handle,retp,2,100);
//statusm.Lines.Add('code='+inttostr(code));
if copy(string(retp),1,2)<>'OK' then
continue;
if copy(string(retp),1,2)='OK' then
break;
end;
//while
if copy(string(retp),1,2)='OK' then
begin
pos_frm.statusm.Font.Color:=clblue;
pos_frm.statusm.Lines.Add('手持机应答成功[ok]');
with datamodulefrm1.ADOQ_temp2do
begin
close;sql.Clear;
sql.Text:='select 卡号 from loss_card';
open;
first;
while not eofdo
begin
code:=link_read(pos_handle,retp,4,1000);
if (code<0)or(trim(copy(retp,2,3))<>inttostr(recno))or(copy(retp,1,1)<>'S') then
begin
pos_frm.statusm.Font.Color:=clred;
pos_frm.statusm.Lines.Add('第'+inttostr(recno)+'号黑名单没有接到手持机发送的标志,请重发');
link_close(pos_handle);
exit;
end;
do
wn_cs1:=fieldvalues['卡号'];
next;
send:=pchar(down_cs1);
pos_frm.jyzm.Lines.Add('下载黑名单:'+send);
while pos_frm.flag_truedo
begin
application.ProcessMessages;
begin
if pos_frm.flag_true=false then
break;
end;
code:=link_write(pos_handle,send,6);
if code=6 then
begin
pos_frm.statusm.Font.Color:=clblue;
pos_frm.statusm.Lines.Add('黑名单下载成功');
break;
end;
end;
end;
//while
end;
end
end
else
begin
pos_frm.statusm.Font.Color:=clred;
pos_frm.statusm.Lines.Add('手持机应答不成功[黑名单下载]');
link_close(pos_handle);
exit;
end;
end;
//下载黑名单
//------------------------------------------------------------------
end;
finally
freeonterminate:=true;
link_close(pos_handle);
end;

//-------------------------------------------------------
end;
procedure pos_cj.Execute;
var
i : integer;
begin
freeonterminate:=true;
//entercriticalsection(cs);
for i:=1 to 2do
begin
Com:='COM'+inttostr(i);
synchronize(sjcj);
end;
//leavecriticalsection(cs);
end;
end.
 
哈欠,G622:CoInitialize,UnInitialize,要使用它该引用哪个单元,帮助中找不倒相关主题.
 
to g622:uses 了activex ,依然无法识别CoInitialize
* 我是在主单元中声明了一个线程类,这样添加Activex是否只需要在住单元的uses
中加一个Activex就好了?
 
哦,我知道了是因为CoUnInitialize blue Boy写错了.
 
多人接受答案了。
 
顶部