[fpc-pascal] Reading Serial Hex Data
James Richters
james.richters at productionautomation.net
Mon Dec 28 02:02:55 CET 2020
I think I figured out why my writeln's are causing an issue.. they are introducing a delay between the SerWrite and SerRead...
and the device I am reading is timing out and sending it's response a second time.
This seems to be the case because it's re-sending bytes that have already been received.
I was under the impression that there was a hardware buffer on the serial ports, my packets are very small,
some only a few bytes, but even those are not going into any kind of a hardware buffer.
I'm on Windows, and I remember seeing something about buffers in device manager.. and so I went into the settings
On the serial port I am using... and it turns out, it doesn't have a buffer... because it's a USB to serial converter.
I checked the settings on real serial port, and those indeed have a hardware buffer I can turn on, but the USB serial port
Does not.
I think the best solution is to make the separate thread that reads everything available all the time and create a software
Buffer.
James
-----Original Message-----
From: fpc-pascal <fpc-pascal-bounces at lists.freepascal.org> On Behalf Of James Richters via fpc-pascal
Sent: Sunday, December 27, 2020 6:21 PM
To: bo.berglund at gmail.com; 'FPC-Pascal users discussions' <fpc-pascal at lists.freepascal.org>
Cc: James Richters <james.richters at productionautomation.net>
Subject: Re: [fpc-pascal] Reading Serial Hex Data
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
_______________________________________________
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