unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, OleCtrls, MSCommLib_TLB, StdCtrls, ExtCtrls; type TForm1 = class(TForm) Panel1: TPanel; Panel2: TPanel; CheckBox1: TCheckBox; MSComm: TMSComm; Memo1: TMemo; edtcomm: TEdit; Label1: TLabel; RadioGroup1: TRadioGroup; Timer1: TTimer; Edit1: TEdit; Label2: TLabel; procedure CheckBox1Click(Sender: TObject); procedure MSCommComm(Sender: TObject); procedure Timer1Timer(Sender: TObject); private function HexToBin(HexNr : string): string; function HexCharToBin(HexToken : char): string; function HexCharToInt(HexToken : char):Integer; { Private declarations } public { Public declarations } end; var Form1: TForm1; commType:integer; implementation {$R *.dfm} procedure TForm1.CheckBox1Click(Sender: TObject);begin if CheckBox1.Checked then begin if mscomm.portopen then begin Timer1.Enabled := false; mscomm.portopen:=false; mscomm.Refresh; end else begin mscomm.CommPort:=5; //第幾個comm口// mscomm.Settings:='9600,N,8,1'; //波特率,奇偶校驗位,數據位,中止位 默認 9600 mscomm.Settings:= trim(edtcomm.Text); mscomm.OutBufferSize:=0; //設置發送緩衝區的大小,缺省爲512字節 mscomm.InBufferSize:=1024; //設置接受緩衝區的大小,缺省爲1024字節 mscomm.InputLen:=160; //設置每次讀取的字符數,0表示讀取整個緩衝區的內容 mscomm.InputMode:=0; //0爲文本傳輸,1爲二進制數據 //mscomm1.Handshaking:=0; //設置握手協議,0表示務協議 mscomm.RThreshold:=160; //當發生字符傳輸數據大於160時,激活接收傳輸事件 mscomm.SThreshold:=0; //當發生字符傳輸數據大於1時,激活傳輸事件 mscomm.ParityReplace:='?'; //當發生奇偶校驗錯誤時,出現的字符 if radioGroup1.itemIndex=0 then commType := 0 else commType := 1; if commType=1 then Timer1.Enabled := true; try MSComm.Enabled:=True; MSComm.PortOpen:=True; except on e:Exception do ShowMessage(e.Message); end; end; end; end; function TForm1.HexCharToBin(HexToken: char): string;var DivLeft : integer;begin DivLeft:=HexCharToInt(HexToken); { first HEX->BIN } Result:=''; // Use reverse dividing repeat //Trick; divide by 2 //if (odd(DivLeft)) then // result = odd ? then bit = 1 if (DivLeft mod 2) = 1 then Result:='1'+Result // result = even ? then bit = 0 else Result:='0'+Result; DivLeft:=DivLeft div 2; // keep dividing till 0 left and length = 4 until (DivLeft=0) and (length(Result)=4); // 1 token = nibble = 4 bits end; function TForm1.HexCharToInt(HexToken: char): Integer;begin {if HexToken>#97 then HexToken:=Chr(Ord(HexToken)-32); { use lowercase aswell } Result:=0; if (HexToken>#47) and (HexToken<#58) then { chars 0....9 } Result:=Ord(HexToken)-48 else if (HexToken>#64) and (HexToken<#71) then { chars A....F } Result:=Ord(HexToken)-65 + 10; end; function TForm1.HexToBin(HexNr: string): string;var Counter : integer;begin Result:=''; for Counter:=1 to length(HexNr) do Result:=Result+HexCharToBin(HexNr[Counter]);end; procedure TForm1.MSCommComm(Sender: TObject);var strtemp,st,strweigh1 :string; inttemp1, inttemp2: Integer;begin if commType=0 then begin if mscomm.CommEvent=2 then //若是接收區有數據達到了RThreshold的值則發生;1表示發送區域小於SThreshold的值時發生 begin strtemp:=mscomm.Input; //接收緩衝區移走一串字符 memo1.Lines.Add('【strtemp爲緩衝取的數據】 '+strtemp); //debug稱重數據 memo1.Lines.Add('----------------------------------------'); inttemp1:=pos('k',trim(strtemp)); memo1.Lines.Add('【inttemp1爲k第一次出現的位置】 '+inttostr(inttemp1)); memo1.Lines.Add('----------------------------------------'); if inttemp1<14 then begin inttemp2:=pos('k',trim(strtemp)); memo1.Lines.Add('【跳進if條件第1層 inttemp2爲k第一次出現的位置(再一次撲捉)】'+inttostr(inttemp2)); memo1.Lines.Add('----------------------------------------'); if inttemp2<14 then begin memo1.Lines.Add('【跳進if條件第2層 本次抓取的數據錯誤,退出。。。】'); memo1.Lines.Add('----------------------------------------'); inttemp1:=0; exit; end; inttemp1:=inttemp2; memo1.Lines.Add('【inttemp1:=inttemp2 以第2次抓取k的位置爲準】'+inttostr(inttemp1)); memo1.Lines.Add('----------------------------------------'); end; strweigh1:=trim(copy(strtemp,inttemp1-8,8)); memo1.Lines.Add('【strweigh1爲k前8位到K的數據】'+strweigh1); //debug稱重數據 memo1.Lines.Add('----------------------------------------'); st:=trim(copy(strtemp,inttemp1-13,5)); memo1.Lines.Add('【st爲K前13位到17位】'+st); //debug稱重數據 // memo1.Lines.Add('----------------------------------------'); //------------------------------------------------------------------------------------------------------------------- if st='ST,GS' then //數據穩定 begin // Labweigh.Font.Color:=clGreen; // Labweigh.Caption:=Trim(StrWeigh1); // labweigh.Refresh; // Labinfor2.Caption:='請掃入條碼'; memo1.Lines.Add('數據穩定時,請掃入條碼……'); // labinfor2.Font.Color:=clblue; // Labinfor2.Refresh; end else begin // Labweigh.Font.Color:=clred; // //Labweigh.Caption:=Trim(StrWeigh1); // //labweigh.Refresh; // Labinfor2.Caption:='數據不穩定'; memo1.Lines.Add('數據不穩定……'); // labinfor2.Font.Color:=clred; // Labinfor2.Refresh; end; //if not checkweigh then exit; //檢查重量 //------------------------------------------------------------------------------------------------------------------- end; memo1.Lines.Add('***********【結束】**********'); end;end; procedure TForm1.Timer1Timer(Sender: TObject);var ReceiveData :String; //接收電子稱發到電腦的數據 decimal_digits:String; //小數位數 flag:char; //標誌位 ascII_Data:integer; //ascII數據 DecData:string; //二進制數據 HexData:string; //十六進制數據 w:String; //不帶符號和小數點的數據 ReceiveDataNew:String; //不帶符號和小數點的數據 Start, Stop : integer; gross_weight:string; filenrc :char; buffer :variant; s1,ss:string; c :char;begin if commType=1 then begin ReceiveData:=MSComm.Input; If pos(chr(2),ReceiveData )=1 then //chr(2)的值是#2(也就是開始位置) begin flag:=ReceiveData[2]; //(1)取得標誌位逗號,始終是第二位 W:=copy(ReceiveData,5,6); //(2)不帶符號和小數點的數據 ascII_Data:=ORD(flag); //(3)把字符轉換成ACSII碼(10進制) HexData:=inttohex(ascII_Data,2); //(4)把ACSII碼(10進制)轉換爲16進制 DecData:=HexToBin(HexData); //(5)把16進制轉換爲二進制 decimal_digits :=copy(DecData,4,3); //(6)小數位數 gross_weight:='0'; //初始化毛重量 try //分八種狀況處理,稱的數值乘於小數點位數結合後得出實際值 if decimal_digits='000' then gross_weight:= FloatToStr(StrToFloat(w) * 1) else if decimal_digits='100' then gross_weight:= FloatToStr(StrToFloat(w) * 1) else if decimal_digits='010' then gross_weight:= FloatToStr(StrToFloat(w) * 1) else if decimal_digits='110' then gross_weight:= FloatToStr(StrToFloat(w) * 0.1) else if decimal_digits='001' then gross_weight:= FloatToStr(StrToFloat(w) * 0.01) else if decimal_digits='101' then gross_weight:= FloatToStr(StrToFloat(w) * 0.001) else if decimal_digits='011' then gross_weight:= FloatToStr(StrToFloat(w) * 0.0001) else if decimal_digits='111' then gross_weight:= FloatToStr(StrToFloat(w) * 0.00001); edit1.Text:=FloatToStr(StrToFloat(gross_weight)*10); finally end; end; end;end; end.