Recent

Author Topic: TComPort  (Read 53787 times)

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: TComPort
« Reply #30 on: May 20, 2016, 12:52:46 am »
you can change the source code of the lazserialsetup.pas file of the tlazserial component : 

Code: Pascal  [Select][+][-]
  1. procedure EditComPort(ComPort: TLazSerial);
  2. begin
  3.   with TComSetupFrm.Create(nil) do
  4.   begin
  5.     ComComboBox1.Text := ComPort.Device;
  6.     ComComboBox2.Text :=  BaudRateToStr(ComPort.BaudRate);
  7.     ComComboBox3.Text :=  DataBitsToStr(ComPort.DataBits);
  8.     ComComBoBox4.Text :=  StopBitsToStr(ComPort.StopBits);
  9.     ComComBoBox5.Text :=  ParityToStr(ComPort.Parity);
  10.     ComComBoBox6.Text :=  FlowControlToStr(ComPort.FlowControl);
  11.  
  12.  if ShowModal = mrOK then
  13.     begin
  14.       ComPort.Close;
  15.       ComPort.Device := ComComboBox1.Text;
  16.       ComPort.BaudRate := StrToBaudRate(ComComboBox2.Text);
  17.       ComPort.DataBits := StrToDataBits(ComComboBox3.Text);
  18.       ComPort.StopBits := StrToStopBits(ComComboBox4.Text);
  19.       ComPort.Parity := StrToParity(ComComboBox5.Text);
  20.       ComPort.FlowCOntrol := StrToFlowControl(ComComboBox6.Text);
  21.       // ComPort.Open;
  22.     end;
  23.     Free;
  24.   end;
  25. end;

when the OK button of the dialog window is clicked you pass in the if ShowModal = mrOK then part of the code.
For the CANCEL button you must add code.

Friendly, J.P

Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

dawid75_75

  • New Member
  • *
  • Posts: 14
Re: TComPort
« Reply #31 on: May 20, 2016, 01:20:23 am »
Thank you for help :)
I have one more question - is there any possibility to see the Setup Dialog in project window like I can see, e.g.: TForm1? I'm pretty sure that I saw it when I had opened LazSerial sertest project first time but I closed it immediately. I've got no idea why :)

Regards :)

PS: Sorry for my English, it's not my native language :P
« Last Edit: May 20, 2016, 01:40:25 am by dawid75_75 »

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: TComPort
« Reply #32 on: May 20, 2016, 10:11:29 am »
hello,
you can add the file Lazserialsetup.pas from the lazserial package folder to your project (don't copy it just add) .
don't forget if you change anything in the setup dialog form or in the pascal file , it is the Tlazserial component that is modified.
See Attachment

As you can see in the attachment, English is not my natural language  :P

Friendly, J.P
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

dawid75_75

  • New Member
  • *
  • Posts: 14
Re: TComPort
« Reply #33 on: May 20, 2016, 10:35:56 pm »
Hello,
I did what you wrote but when I am choosing Setup form in 'View Project Forms' window Lazarus opens it in source editor and I still can't see it in project window. How can I open it in project window?

Regards :) 

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: TComPort
« Reply #34 on: May 20, 2016, 11:26:34 pm »
hello,
what is your O.S and your lazarus version ?
can you show me your project inspector window ?

Friendly, J.P
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

dawid75_75

  • New Member
  • *
  • Posts: 14
Re: TComPort
« Reply #35 on: May 21, 2016, 10:39:58 am »
Hello,
I work on Win10 x64 and Lazarus 1.6 32bit.
Screen is in the attachment.

Regards :)

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: TComPort
« Reply #36 on: May 21, 2016, 11:33:07 am »
hello,
you use Tcomport not Tlazserial ?    :'(
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

dawid75_75

  • New Member
  • *
  • Posts: 14
Re: TComPort
« Reply #37 on: May 21, 2016, 12:42:17 pm »
Hello,
My professor asked me to do it by TComport but finally I'll use LazSerial. I noticed that LazSerial works more stable :) Now I'm just practising establishing connection between PC and atmega and I have to show working software to my professor. Next I'll rewrite my soft on LazSerial :)

So again - how can I open and edit Setup Dialog?
« Last Edit: May 21, 2016, 01:04:32 pm by dawid75_75 »

dawid75_75

  • New Member
  • *
  • Posts: 14
Re: TComPort
« Reply #38 on: May 21, 2016, 04:19:09 pm »
Ah, to the hell with TComport, I rewrote my soft with LazSerial :) Screen is in attachment.

I have one more question - when I was reading LazSerial.pas file I found that line:

//    function ReadBuffer(var buf; size: integer): integer;

Why this line is inactive? In the future I will in need of receiving numerical data from Atmega and that line - receiving numbers, not strings - will be very, very helpful. For what reason that instruction is inactive?

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: TComPort
« Reply #39 on: May 22, 2016, 09:18:23 am »
hello,
don't worry for this inactive line (tlazserial is based on 5dpoSerial and in this package the line is inactive).
Tlazserial works with binary data :
Example :
project to send binary data on a port and received data on another port :
Procedure to send binary data :
Code: Pascal  [Select][+][-]
  1. procedure TFMain.BSendDataClick(Sender: TObject);
  2. var
  3.   TempList: TstringList;
  4.   mBuffer:array[0..100] of Byte;
  5.   x: Integer;
  6.  
  7. begin
  8.   TempList := TStringList.Create;  { construire l'objet liste }
  9.   TempList.CommaText :=  OutputData.Text; //OutData TEdit comma formated hexa data
  10.   For  x:= 0 to TempList.Count - 1 do
  11.   begin
  12.      mBuffer[x] := StrToInt('$' + TempList[x]);
  13.   End ;
  14.   Serial1.WriteBuffer(mBuffer,TempList.Count);
  15. //      Timer1.Enabled := True;
  16. end;        

Procédure to receive data from the other serial port (the Two Serial ports aree linked) :
Code: Pascal  [Select][+][-]
  1. procedure TFMain.SerialRxData(Sender: TObject);
  2. var
  3.   S, HexaS: string;
  4.   i: integer;
  5. begin
  6.   S := Serial.ReadData;
  7.   for I := 1 to Length(S) do
  8.   begin
  9.     HexaS := HexaS + InttoHex(Ord(S[I]), 2) + ' ';
  10.   end;
  11.   if Length(S) > 0 then  Memo.Lines.Add(HexaS);
  12. end;
the Receive procedure display the received data in Hexa in the memo control.   

Friendly, J.P
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

dawid75_75

  • New Member
  • *
  • Posts: 14
Re: TComPort
« Reply #40 on: November 03, 2016, 12:02:26 am »
Hello everyone again  :)
I've been working in Lazarus for a while and I've got problem. I receive data by TComPort and some data are corrupted. How do I know it? I just see the same data in Realterm without any problem.
I'll tell you what is going on - I receive by COM packet of 13 bytes of data. Every packet starts from 0x01 end ends by 0x04. In Realterm I can see all packets in good condition, nothing is lost. But in Lazarus in TComPort about 20% of packets are corrupted, e.g. some bytes are lost or some bytes have different values than real ones.

I receive packet by this code:
Code: Pascal  [Select][+][-]
  1. Serial.Read(RxBuffer, Count);
in OnRxChar event.

What can cause that behaviour? If you Guys have any idea, please, answer :)
BR,
Dawid.
« Last Edit: November 03, 2016, 01:03:49 pm by dawid75_75 »

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: TComPort
« Reply #41 on: November 03, 2016, 12:54:47 am »
hello,
see here for Reading data in packets.

Friendly, J.P
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

dawid75_75

  • New Member
  • *
  • Posts: 14
Re: TComPort
« Reply #42 on: November 03, 2016, 11:11:15 am »
Hello,
thanks for your reply :) I did it and situation looks better.
But something still happens.
My data packet looks like this:
StartString:=#1
StopString:=#4

Example packet (dec values):
1 107 4 139 57 42 44 85 255 58 212 63 4

This is all procedure I receive data and show it on ListBox:
Code: Pascal  [Select][+][-]
  1. procedure TMainWindow.DataPacketPacket(Sender: TObject; const Str: string);
  2. const FrameStartPos = 0;
  3. const FrameEndPos   = 12;
  4. var
  5.   RxBuffer:          array[1..FrameEndPos+1]          of Byte   = (0,0,0,0,0,0,0,0,0,0,0,0,0);
  6.   c: Integer = 0;
  7.   i: integer = 0;
  8.   licznik: Integer = 0;
  9.   NStr: String;
  10. begin
  11.   for i := 1 to 13 do begin
  12.     c := ord(Str[i]);
  13.     RxBuffer[i] := c;
  14.   end;
  15.   i:=0;
  16.   NStr:=' ';
  17.   for i:=1 to 13 do begin
  18.     NStr:=NStr + IntToStr(RxBuffer[i]) + ' ';
  19.     //ComMemo.Text:=ComMemo.T
  20.   end;
  21.   if((RxBuffer[1]<>1) Or (RxBuffer[13]<>4)) then
  22.   begin
  23.        BadDataCnt:=BadDataCnt+1;
  24.        BadData.Caption:=IntToStr(BadDataCnt);
  25.   end;
  26.  
  27.   Listbox1.Items.Add(NStr);
  28.   Serial.ClearBuffer(true, true);
  29. end;                          

Most of data is correct, but some data has changed order. I mean that:
139 57 42 44 85 255 58 212 63 4 (1 107 4) -> Should be on the beginning of the packet.
As you can see, first three elements are on the end of the packet, and I don't know why.
I noticed that I got all packet when I set Size in TDataPacket on 14, but I've got 13 bytes in packet.
Any ideas about all of that? :)
Regards,
D.
« Last Edit: November 03, 2016, 11:26:21 am by dawid75_75 »

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: TComPort
« Reply #43 on: November 03, 2016, 12:47:45 pm »
hello dawid75_75,
Something is strange in your packet :
Example packet (dec values):
1 107 4 139 57 42 44 85 255 58 212 63 4

and also you say :
Quote
Every packet starts from 0x00
EDIT : rectify in his primary message by dawid   :P

you have a stopstring inside your packet, so it isn't a stop string. is it possible to have also a #1  inside your packet ?


Can you show us some lines of what you receive exactly (without format or filter ) ?
 
Friendly, J.P
« Last Edit: November 03, 2016, 01:10:09 pm by Jurassic Pork »
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

dawid75_75

  • New Member
  • *
  • Posts: 14
Re: TComPort
« Reply #44 on: November 03, 2016, 01:19:55 pm »
Ah, I made a mistake. Every packet starts from 0x01 (I corrected that in a post above). And I set StartString:=#1.
And actually it is possible to have 0x01 and 0x04 inside a packet which are not a start/stop strings. In fact, I thought that isn't a problem when I set Size in DataPacket. Am I wrong? Will program crash when I receive start or stop string inside a packet?

Raw data in software I wrote in Lazarus are ASCII characters corresponding to decimal values I send by COM to the program. Next action is getting  their decimal values to the RxBuffer and finally IntToStr function to show them in ListBox. I am not in the house now, I send some lines later, but now I can tell that most of packets look correct, like this:
StartString:=#1 ->1 107 4 139 57 42 44 85 255 58 212 63 4<-StopString:=#4, all packet = 13 bytes.

but some packets have changed order like this:
missed first three bytes -> 139 57 42 44 85 255 58 212 63 4 <- Stop character equals 0x04; start character equals 0x01 -> 1 107 4 and these three bytes are moved from the beginning to the end of string.
« Last Edit: November 03, 2016, 01:23:30 pm by dawid75_75 »

 

TinyPortal © 2005-2018