Forum > Packages and Libraries

TLazSerial

(1/3) > >>

johnsolomon:
OK, I give up. I need help. Forgive me, I am new to this.

I am communicating via an RS-232 through a usb-232 adapter to a mettler toledo balance. When I send a command, I get a response. example:

procedure TForm1.Button2Click(Sender: TObject);
begin
        //Get Serial Number
        LazSerial1.WriteData('I4'+#13#10);
        Memo1.lines.Add(LazSerial1.ReadData);
        Button3.Enabled:=True;

then the scale will respond back:
I4 A "B346986532"

the program is pretty simple:

unit MT1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs,
  StdCtrls, ExtCtrls, LazSerial;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    Label1: TLabel;
    LazSerial1: TLazSerial;
    Memo1: TMemo;

    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);

    //Function TLazSerialReadData: String;

  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;
  CurPos : integer;


implementation

{$R *.lfm}

{ TForm1 }


procedure TForm1.FormCreate(Sender: TObject);
begin
        Memo1.DoubleBuffered := True;
        Button2.Enabled:=False;
        Button3.Enabled:=False;
        Button4.Enabled:=False;
        Button5.Enabled:=False;
        Button6.Enabled:=False;

end;

procedure TForm1.Button1Click(Sender: TObject);
begin
        //Setup Comm Port
        LazSerial1.ShowSetupDialog;
        Button2.Enabled:=True;
        LazSerial1.Active:= True;
        LazSerial1.Open;
        LazSerial1.WriteData('@'+#13#10);
        LazSerial1.WriteData('ZI'+#13#10);
        sleep(2);
        LazSerial1.SynSer.flush;

end;

procedure TForm1.Button2Click(Sender: TObject);
begin
        //Get Serial Number
        LazSerial1.WriteData('I4'+#13#10);
        Memo1.lines.Add(LazSerial1.ReadData);
        Button3.Enabled:=True;

end;

procedure TForm1.Button3Click(Sender: TObject);
begin
        //Get Calibration Weight
        LazSerial1.WriteData('M19'+#13#10);
        Memo1.lines.Add(LazSerial1.ReadData);
        Button4.Enabled:=True;

end;

procedure TForm1.Button4Click(Sender: TObject);
begin
        //Unlock for calibration with external weights
        LazSerial1.WriteData('C0 1 1'+#13#10);
        Memo1.lines.Add(LazSerial1.ReadData);
        Button5.Enabled:=True;

end;

procedure TForm1.Button5Click(Sender: TObject);
begin
        //Initiate calibration sequence
        LazSerial1.WriteData('C2'+#13#10);
        Memo1.lines.Add(LazSerial1.ReadData);
        Button6.Enabled:=True;

end;

procedure TForm1.Button6Click(Sender: TObject);
begin
        //lock out calibration with external weights
        LazSerial1.WriteData('C0 0 0'+#13#10);
        Memo1.lines.Add(LazSerial1.ReadData);
        LazSerial1.Active:= False;
        Button2.Enabled:=False;
        Button3.Enabled:=False;
        Button4.Enabled:=False;
        Button5.Enabled:=False;
        Button6.Enabled:=False;
        LazSerial1.Close;
end;

end.

The problem I am seeing is that it looks like a buffering issue. I click the first button (not including comsetting), I get the first  response (its correct). I click the second button, I get the response as if I clicked button 1 (not correct).. I click the third button, I get the response as if I clicked button 2... and so on. I get the correct action from the unit, but the response I get seems to get out of sync. the responses I get are correctly displayed, but for the previous command.

Do I need to flush the buffer on readdata?
how do I do that?
am I missing something else?

any help is appreciated.

any additional info you need, I will be happy to get for you.
 

Noodly:
Is the code for TForm1.Button2Click missing a line?

Memo1.lines.Add(LazSerial1.ReadData);

Paul Breneman:
Without more info on the correct sequence of data it is hard for me to determine what is going wrong.

If you could use Control Terminal (http://ctrlterm.com/) and manually do the sequence of commands, then save the log file and share that then it would probably be easier to figure things out.

johnsolomon:
Noodly, although you are correct, I have been hacking and slashing away at this code for two days trying this, that and the other thing. I must have removed that bit when I trimed out my last attempt. its there now and still no difference.

Paul,

   being new, I am not sure what you mean as far as sequence of commands. I tried to make this so simple. button 1 allows user to enter com port data. button2, to read the serial number. button3 is to get the preprogramed weight for calibration, button 4 is to unlock the calibration function, button5 is to activate the calibration sequence. and finally button6 is to lock it back so line users cant run the cal sequence after its been ran. I enable the buttons as the cal techs step though the process keeping them in order.

This is the response I get from the balance:
I4 A "B346986532"    (when button2 is pressed) correct
I4 A "B346986532"    (when button3 is pressed) this is button2 response
M19 A 500.000 g       (when button4 is pressed) this is button3 response
C0 A                         (when button5 is pressed) this is button4 response
C2 B                         (When button6 is pressed) this is button5 respose.

so when I press button3...I should get "M19 A 500.000 g". I hope you understand what I am sttemting to discribe...


johnsolomon:
this is output from another program called Advanced Serial Port Monitor. I hope it helps.

<20171005130029.747 SYS>
COM is open
<20171005130029.747 SYS>
In/out queue size 4096/0
<20171005130029.747 SYS>
Set timeouts: ReadInterval=65535, ReadTotalTimeoutMultiplier=0, ReadTotalTimeoutConstant=0, WriteTotalTimeoutMultiplier=0, WriteTotalTimeoutConstant=0
<20171005130029.747 SYS>
RTS on
<20171005130029.747 SYS>
DTR on
<20171005130029.747 SYS>
Purge the serial port: RXABORT, RXCLEAR, TXABORT, TXCLEAR
<20171005130029.753 SYS>
Baud rate 9600
<20171005130029.753 SYS>
RTS on
<20171005130029.753 SYS>
DTR on
<20171005130029.753 SYS>
Data bits=8, Stop bits=1, Parity=None
<20171005130029.753 SYS>
Set chars: Eof=0x00, Error=0x00, Break=0x00, Event=0x00, Xon=0x11, Xoff=0x13
<20171005130029.753 SYS>
Handflow: ControlHandShake=(DTR_CONTROL), FlowReplace=(AUTO_TRANSMIT, AUTO_RECEIVE, TRANSMIT_TOGGLE, RTS_CONTROL), XonLimit=1024, XoffLimit=1024
<20171005130029.754 TX>
@ [len=1]
<20171005130029.754 TX>
ZI [len=2]
<20171005130037.281 TX>
@ [len=1]
<20171005130037.281 TX>
ZI [len=2]
<20171005130038.766 TX>
I4 [len=2]
<20171005130038.766 RX>
I4 A "B346986532" [len=17]
<20171005130038.766 RX>
I4 A "B346986532" [len=17]
<20171005130041.219 TX>
M19 [len=3]
<20171005130044.642 TX>
C0 1 1 [len=6]
<20171005130044.642 RX>
I4 A "B346986532" [len=17]
<20171005130044.642 RX>
M19 A 500.000 g [len=15]
<20171005130049.158 TX>
C2 [len=2]
<20171005130053.123 TX>
C0 0 0 [len=6]
<20171005130053.123 RX>
C0 A [len=4]
<20171005130053.123 RX>
C2 B [len=4]
<20171005130053.123 RX>
C2 "     0.000 g" [len=17]
<20171005130053.123 SYS>
Purge the serial port: RXABORT, RXCLEAR, TXABORT, TXCLEAR
<20171005130053.123 SYS>
Purge the serial port: RXABORT, RXCLEAR, TXABORT, TXCLEAR
<20171005130053.123 SYS>
RTS off
<20171005130053.123 SYS>
DTR off
<20171005130053.186 SYS>
COM is closed

Navigation

[0] Message Index

[#] Next page

Go to full version