码迷,mamicode.com
首页 > 其他好文 > 详细

MSCOMM电子秤参考

时间:2015-04-08 16:52:22      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:mscomm   电子秤   

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.

本文出自 “畅想天空” 博客,请务必保留此出处http://kinwar.blog.51cto.com/3723399/1629949

MSCOMM电子秤参考

标签:mscomm   电子秤   

原文地址:http://kinwar.blog.51cto.com/3723399/1629949

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!