线程的使用问题 (问题自己解决,散分,多来几个人啊)(100)

  • 主题发起人 主题发起人 lht123
  • 开始时间 开始时间
L

lht123

Unregistered / Unconfirmed
GUEST, unregistred user!
我用 hidcomponent控件用来读取hid设备,hid设备我本公司采集铁路轨距的轨距尺,轨距尺里的数据 按段 ,包,组 分类,一段数据包含若干包,一个包可能有若干组数据,一组为64个字节,现在是用 hidcomponent控件的一个接收函数来接受数据,协议是自己定义的: 第一 部 :我需要知道有多少段,第二 我需要知道每个段有多少包,第三:我把每个段的每包每组数据都读出来 第二 ,三 部我就需要用循环用接收hid设备的数据了,问题是当在第二部用循环来读取有多少个包时候,没执行完,就到第三部了,导致第三部读的都是空 现在用了三个 radionclick 事件来控制 ,可以正确读出来。 我现在做的就是把三个radioonclick事件都放在一起,就出现数据读不出来的问题了,如果那保证 三个radioonclick 事件都完全执行了,我的数据也能读出来,问题是 线程里的函数到最后才串行执行,导致中间的代码都漏了,谁能帮帮我 ,谢谢
 
怎么没人回答啊。
 
1.保证支持线程读写;2.保证通讯协议的正常;因为有些硬件响应时间比较滞后;3.再不行就看你用的hidcomponent源码有没有问题。
 
to autumn 我吧源码贴出来 大家帮我看一下 procedure TF_dqq.RadioButton5Click(Sender: TObject);var i,j,k:integer; Str,str_b: string; Buf: array [0..64] of Byte; Written: Cardinal; ToWrite: Cardinal; Err: DWORD; ds:integer;begin self.RadioButton4.OnClick(RadioButton4); sleep(200); currentDevice.OnData:=nil; if str_state='ds' then self.RadioButton6.OnClick(RadioButton6); sleep(200); // currentDevice.OnData:=nil; // if str_state='syb' then begin yx_cs:=0; zbs:=0; //showmessage('下载开始'); currentDevice.OnData:=ShowRead; for i:=low(packs_ds) to high(packs_ds) do zbs:=zbs+packs_ds; for k:=low(packs_ds) to high(packs_ds) do // for k:=0 to 2 do begin // if k=0 then // continue; if Devlistbox.Items.Count=0 then begin showmessage('设备通讯中断'); application.Terminate; end; buf[0]:=StrToIntDef('$00', 0); buf[1]:=StrToIntDef('$AF', 0); buf[2]:=StrToIntDef('$AF', 0); buf[3]:=StrToIntDef('$FC', 0); buf[4]:=StrToIntDef('$FC', 0); buf[5]:=StrToIntDef('$'+inttohex(k+1,2), 0); buf[6]:=StrToIntDef('$00', 0); for sy_b:=1 to packs_ds[k] do // for sy_b:=1 to 2 do begin str_state:='syb'; bz:=false; buf[7]:=StrToIntDef('$'+inttohex(sy_b,2), 0); Application.ProcessMessages; for i:=8 to 64 do begin Buf:=StrToIntDef('$00', 0); end; ToWrite := CurrentDevice.Caps.OutputReportByteLength; if not CurrentDevice.WriteFile(Buf,ToWrite, Written) then begin Err := GetLastError; Add(Format('写错误: %s (%x)', [SysErrorMessage(Err), Err])); end else begin for I := 1 to Written-1 do begin Str := Str + Format('%.2x ', [Buf]); //add(str); end; currentDevice.ThreadSleepTime:=1000; // currentDevice.OnData:=nil; end; end; end; end;end;procedure TF_dqq.RadioButton4Click(Sender: TObject);var i,j,k:integer; Buf: array [0..64] of Byte; Written: Cardinal; ToWrite: Cardinal; str:string; Err: DWORD; str_b: string;begin listbox1.Items.Clear; CurrentDevice := TJvHidDevice(DevListBox.Items.Objects[DevListBox.ItemIndex]); currentDevice.OnData:=ShowRead; //L_usb.Caption:=DeviceName(CurrentDevice); { if l_usb.Caption='' then begin showmessage('没有HID设备'); exit; end; } //currentDevice.OnData:=ShowRead; str_state:='csh'; buf[0]:=StrToIntDef('$00', 0); buf[1]:=StrToIntDef('$AF', 0); buf[2]:=StrToIntDef('$AF', 0); buf[3]:=StrToIntDef('$FA', 0); buf[4]:=StrToIntDef('$FA', 0); for i:=5 to 64 do begin Buf:=StrToIntDef('$00', 0); end; ToWrite := CurrentDevice.Caps.OutputReportByteLength; if not CurrentDevice.WriteFile(Buf, ToWrite, Written) then begin Err := GetLastError; Add(Format('WRITE ERROR: %s (%x)', [SysErrorMessage(Err), Err])); end else begin for I := 1 to Written-1 do Str := Str + Format('%.2x ', [Buf]); //AddToHistory(Str); //currentDevice.OnData:=ShowRead; end;end;procedure TF_dqq.RadioButton6Click(Sender: TObject);var i,j,k:integer; Str,str_b: string; Buf: array [0..64] of Byte; Written: Cardinal; ToWrite: Cardinal; Err: DWORD;begin qj_bz:=true; //for k:=1 to listbox1.Items.Count do for k:=1 to ds_z do begin //currentDevice.OnData:=nil; str_state:='ds'; buf[0]:=StrToIntDef('$00', 0); buf[1]:=StrToIntDef('$AF', 0); buf[2]:=StrToIntDef('$AF', 0); buf[3]:=StrToIntDef('$FB', 0); buf[4]:=StrToIntDef('$FB', 0); buf[5]:=StrToIntDef('$'+inttohex(k,2), 0); for i:=6 to 64 do begin Buf:=StrToIntDef('$00', 0); end; if qj_bz=true then begin ToWrite := CurrentDevice.Caps.OutputReportByteLength; if not CurrentDevice.WriteFile(Buf, ToWrite, Written) then begin Err := GetLastError; Add(Format('写错误: %s (%x)', [SysErrorMessage(Err), Err])); end else begin for I := 1 to Written-1 do Str := Str + Format('%.2x ', [Buf]); currentDevice.OnData:=ShowRead; qj_bz:=false; // currentDevice.ThreadSleepTime:=1000; // currentDevice.OnData:=nil; end; end; end; //currentDevice.OnData:=nil; //str_state:='syb';end;procedure TF_dqq.ShowRead(HidDev: TJvHidDevice; ReportID: Byte; const Data: Pointer; Size: Word);var Str,str_b: string; i,j,k:integer; Buf: array [0..64] of Byte; Written: Cardinal; ToWrite: Cardinal; Err: DWORD;begin for I := 0 to Size - 1 do begin packs:=Format('%.2x ', [Cardinal(PChar(Data))]); Str:= Str + Format('%.2x ', [Cardinal(PChar(Data))]); end; if str_state='csh' then begin if packs[4]<>'' then begin if HexToInt(trim(packs[4]))>0 then begin ds_z:=HexToInt(trim(packs[4])); for i:=1 to HexToInt(trim(packs[4])) do listbox1.Items.Add('第'+inttostr(i)+' 段'); end; end; // currentdevice.CloseFile; str_state:='ds'; exit; end; if str_state='syb' then begin zs:=HexToInt(trim(packs[5])); AddToHistory(Str); yx_cs:=yx_cs+1; if yx_cs=1 then showmessage('下载开始'); if yx_cs=zbs then begin sleep(2000); showmessage('下载结束'); end; end; if str_state='ds' then begin qj_bz:=true; ds:=ds+1; setlength(packs_ds,ds); bs:=0; if packs[4]<>'' then begin if HexToInt(trim(packs[4])+trim(packs[5]))>0 then begin bs:=HexToInt(trim(packs[4])+trim(packs[5])); packs_ds[ds-1]:=bs; end; end; if ds=ds_z then begin str_state:='syb'; exit; end; end;end;
 
恭喜,接分。
 
咋解决呢,吱声啊~
 
在 ShowRead 里控制的
 
后退
顶部