Forum > General

Receiving serial string with 5DPO

(1/3) > >>

PeterH:
Hello,

Using 5DPO I want to receive a string which has about 120 characters but can have a variabele length. I use the 'onreceivedata' event to do this. I write the output to a memobox.
Most of the time I see the string broken up in three pieces. See below. Start of the string is always F0. End is always F7.

F07F01075072657365742031372020202020202020202
0203700370037003700370037003700370037003700370037003700370
0370037000000000025F7

What is the easiest way to make the complete string again and check if the string is completly received? I mean : how to check for the start and stop character?

Thanks in advance.




JohnvdWaeter:
Hi,

Have a global reeivestring rx, initiate as RX:='';


OnReceive:

if length(received)<2 then exit; // loose characters

if copy(received,1,2)='F0' then RX:=Received
else if copy(received,length(received)-1,2)='F7'
       then begin
                RX:=RX+Received ;
               // RX is complete, do something with it:
                Process(RX);
                RX:='';
              end;
        else RX:=RX+Received.

Needs some further refining, but this is the idea.

hth!
John

alejol0:
Hi, PeterH.

I have worked with RS232 communications (not using Lazarus), and I think
that for your application is better that each character received generates an event.
I think that waiting for a string is a way to miss characters.

Maybe the component
you use to deal with received strings have a strange definition of what is a "string".
Or maybe the string is broken in three or four strings, the first begin with F0
and the last string containing F7.

I think that working byte-by-byte is more secure.

Work in binary form, use bytes instead of characters. Then, if a valid
array of bytes have been collected, cast the array to a string.

If you have a event triggered by each byte received, things can made more easy:
you discard all bytes until a F0 appears, then collect byte by byte until
F7 appears (with some time window or check because F7 could not to come never).

Hope this help.

-- alejandro.lavarello (at) gmail (dot) com

PeterH:
Thanks for the replies.

I'm also concerned about loosing characters when handling the incoming string as a number of strings.

Are there components for Lazarus which have a 'received 1 byte / character' event?

The handling of the incoming data is way easier.
The principle is the same as buffered I/O routines for microcontrollers.

I know I a few for Delphi but these are not free and have to be ported.

Is Synaser (for instance) capable of that? I couldn't find it.



alejol0:
I downloaded the 5DPO packet and have a superficial look at this.
Have encountered in synaser.pas this:

function RecvByte(timeout: integer): byte; virtual;


Maybe this can help you. Maybe setting timeout less than
2 character times can work, needs tweaking. Use low baud rates
if you can,check for communication errors.

Or maybe you can use another packet for managing serial communications. I remember that the Elektor electronics magazine have posted
in Internet a packet for managing serial port using Visual Basic,
this is a .DLL very simple (and close to hardware).

When a UART receives a byte, this set to 1 a internal bit,
if the computer is configured, a interrupt is fired.

With today operating systems is more secure to pool the bit frequently and read the byte (because interrupts are hard to manage and today PCs are very fast).

Good luck!

Navigation

[0] Message Index

[#] Next page

Go to full version