MSComm Electronic Scale Reference

Source: Internet
Author: User

Unit unit1;interfaceuses  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 &NBSP;*.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;                          //the first few comm Mouth//      mscomm. settings:= ' 9600,n,8,1 ';               // Baud rate, parity bit, data bit, stop bit    default  9600      mscomm. Settings:= trim (Edtcomm. Text);       mscomm. outbuffersize:=0;                        //set the size of the send buffer, default to 512 bytes        MSComm. inbuffersize:=1024;                      //sets the size of the accept buffer, which defaults to 1024 bytes of       mscomm. Inputlen:=160;                           //sets the number of characters per read, and 0 reads the contents of the entire buffer        mscomm. inputmode:=0;                            //0 for text transfer, 1 for binary data        //mscomm1. handshaking:=0;                        //set the handshake protocol, and 0 represents the service protocol       mscomm. rthreshold:=160;                         //Activate receive transfer event when character transfer data is greater than 160        mscomm. Sthreshold:=0;                           //activates the transfer event when the character transfer data is greater than 1 o'clock        mscomm. parityreplace:= '? ';                       //characters that occur when a parity error occurs       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 d o          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&NBSP;MOD&NBSP;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&NBSP;&NBSP;STRTEMP,ST,STRWEIGH1&NBSP;:STRING;&NBSP;&NBSP;INTTEMP1,&NBSP;INTTEMP2:  Integer;begin  if commType=0 then  begin    if  mscomm.commevent=2 then   //occurs if the receiving area has data that reaches the RThreshold value, 1 indicates that the sending area is less than Sthreshold value occurs      begin      strtemp:=mscomm. input;     //the receive buffer to remove a string of characters       memo1. Lines.add (' strtemp for buffered data '   ' +strtemp ')      //debug weighing data        memo1. Lines.add ('----------------------------------------');   &Nbsp;   inttemp1:=pos (' K ', Trim (strtemp));       memo1. Lines.add (' "Inttemp1 for the first occurrence of K"   ' +inttostr (INTTEMP1));       memo1. Lines.add ('----------------------------------------');      if inttemp1< 14&NBSP;THEN&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;BEGIN&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;INTTEMP2: =pos (' K ', Trim (strtemp));         memo1. Lines.add (' "Jump in If condition 1th level  inttemp2 for K first occurrence position (again catch)" ' +inttostr (INTTEMP2);         memo1. Lines.add ('----------------------------------------');        if  inttemp2<14 then        begin           memo1. Lines.add (' "Jump in If condition 2nd level   This fetch data error, exit ... "');           memo1. Lines.add('----------------------------------------');           inttemp1:=0;          exit;         end;        inttemp1:=inttemp2;         memo1. Lines.add (' "inttemp1:=inttemp2  the position of the 2nd fetch K" ' +inttostr (INTTEMP1));         memo1. Lines.add ('----------------------------------------');      end;       strweigh1:=trim (copy (strtemp,inttemp1-8,8));       memo1. Lines.add (' "STRWEIGH1 is K-Top 8 to K data" ' +strweigh1);      //debug weighing data        memo1. Lines.add ('----------------------------------------');       st:=trim (Copy ( strtemp,inttemp1-13,5));       memo1. Lines.Add (' "St is K-top 13 to 17-bit" ' +st ");      //debug weighs data      // memo1. Lines.add ('----------------------------------------');   //------------------------------------------ -------------------------------------------------------------------------       if st= ' St,gs '  then                             //Data Stabilization        begin  //      Labweigh.Font.Color:=clGreen;   //      labweigh.caption:=trim (STRWEIGH1);  //       labweigh. Refresh;  //      labinfor2.caption:= ' Please scan the barcode ';         memo1. Lines.add (' When data is stable, please scan the barcode ... ');   //    &nBsp; labinfor2. font.color:=clblue;  //      labinfor2.refresh;       end      else      begin   //      Labweigh.Font.Color:=clred;  //       //labweigh.caption:=trim (STRWEIGH1);   //      //labweigh. refresh;  //      labinfor2.caption:= ' data instability ';         memo1. Lines.add (' data instability ... ');   //      labinfor2. font.color:=clred;  //      labinfor2.refresh;       end;      //if not checkweigh then exit;   //Check weight   //------------------------------------------------------------------------------------ -------------------------------    end;    memo1. Lines.add (' *********** ' End "********** ');   end;end;procedure tform1.timer1timer (Sender:  TObject) var  receivedata :string;   //receives the data   decimal_digits that the electronic is sent to the computer: string; //number of decimal digits   flag:char;  //flag bit &NBSP;&NBSP;ASCII_DATA:INTEGER;&NBSP;//ASCII data    decdata:string; //binary Data   hexdata:string; //hex data   w:string; // Data without symbols and decimals   receivedatanew:string; //data without symbols and decimals   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 ) The value of =1&NBSP;THEN&NBSP;//CHR (2) is # # (that is, start position)      begin        flag:=receivedata[2];  //(1) obtains a sign comma, is always the second digit         w:=copy (receivedata,5,6);  //(2) data without symbols and decimals          ascii_data:=ord (flag);  //(3) Convert characters to acsii yards (10 in)          hexdata:=inttohex (ascii_data,2);  //(4) Convert acsii yards (10) to 16 binary          decdata:=hextobin (Hexdata);  //(5) Convert 16 binary to binary          decimal_digits :=copy (decdata,4,3);  //(6) Number of decimal digits          gross_weight:= ' 0 ';  //initial gross weight         try             //in eight cases, the value of which is calculated by the combination of the number of decimal places to get the actual value          &nBsp;   if  decimal_digits= '        then    gross_weight:= floattostr (Strtofloat (w)  * 1)              else if  decimal_digits= '   then    gross_weight:= floattostr (Strtofloat (w)  * 1)              else if  decimal_digits= ' 010 '   then    Gross_weight:= floattostr (Strtofloat (w)  * 1)              else if  decimal_digits= '   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)   *&NBSP;0.001)             else if   Decimal_digits= ' 011 '   then   gross_weight:= floattostr (StrToFloat (w)  * &NBSP;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.

This article is from the "Imagine the Sky" blog, please be sure to keep this source http://kinwar.blog.51cto.com/3723399/1629949

MSComm Electronic Scale Reference

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.