[fpc-pascal] Reading Serial Hex Data

James Richters james.richters at productionautomation.net
Mon Dec 28 00:20:32 CET 2020


What I meant by 'Hex Data'  is that I am reading data in the form of bytes but they do not represent ASCII characters.  
So if I read a $0D,  it's just a byte $0D, it doesn't represent a carriage return.  There is no specific code for end of line, 
the last two bytes are a checksum, but I won't know they are the last ones until I'm done reading.  
I'm reading all the data available, then analyzing it.

This is a console application.

I have managed to get a bit more accurate timing by setting a time_variable := NOW and then repeating until (Now-TimeVariable) >= Minimum_Time_I_Want.
I'm not sure what the resolution of NOW is, but it's working quite a bit better than Sleep()

I think I do need to put this in a thread.. I notice one strange behavior... if I just run my program, then it works exactly as I expect, 
and I'm not wasting a lot of time waiting for the end of Data.
However if I do a writeln to the console before the function that reads the serial port, then I am missing the end of the data sometimes,
I have to put an additional delay equal to about 10 Bytes of data to make it work... but without the writeln's I need only to wait for
the amount of time of 2 Bytes, which is what I expected.

I'm not using any threads yet, so this is strange behavior, doing a writeln before I even start the procedure that does the serial communication
should not effect it at all, because the writeln should be completely done before it starts the serial communication, yet this is what I observe... 
If I don't do the writeln's everything is fine, if I do them, my data timing is off... it's still there, but I have to wait longer to make sure I get all the data that there is.
What seems to be happening is that I'm 

I'm using SerRead,  it does seem that I never miss anything, it's just that I'm not detecting the end of data accurately if I have writeln's right before I read the serial port.

James
-----Original Message-----
From: fpc-pascal <fpc-pascal-bounces at lists.freepascal.org> On Behalf Of Bo Berglund via fpc-pascal
Sent: Sunday, December 27, 2020 5:08 PM
To: fpc-pascal at lists.freepascal.org
Cc: Bo Berglund <bo.berglund at gmail.com>
Subject: Re: [fpc-pascal] Reading Serial Hex Data

On Sun, 27 Dec 2020 10:47:59 -0500, James Richters via fpc-pascal <fpc-pascal at lists.freepascal.org> wrote:

>I'm trying to figure out how to read a packet of HEX Bytes of an 
>unknown length that have no specific termination character over a 
>serial port as efficiently as practical.

What exactly do you mean by HEX Bytes?
Hex transfers using two characters for each byte? like 1F A4 etc?

>The packet ends when there is just no more data to read.
>
>I have something that is working that I wrote using SerRead from the 
>Serial unit in FPC, and it works at 9600bps,

If you use the serial unit and just read the data coming in using blocking calls you will not miss anything, what you do not read will stay in the input buffer until you read again.
If you need to do something inbetween then you can put the reading in a thread so that is handled there and then transfer the data to the main thread via a synchronize call to a receive function there.

Is your program a console or GUI program?


--
Bo Berglund
Developer in Sweden

_______________________________________________
fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal



More information about the fpc-pascal mailing list