I am using the Comport Library to read and write data from a my comport which is connected to a scale
I started with the reading of the data which initially gave me funny ASCII characters whereby after a bit of investigation I was able to convert this to Hex and get the values I expected
I am now trying to communicate with the scale by writing HEX to the ComPort. I am not sure if I am doing this right. My question is do with what format do I send the data in as the WriteStr method obviously wants you to send a string
So how do I send my Hex value to the comport with the comport library?
This is really not a ComPort question it is a basic Pascal question, how to hex characters are represented in a string.
Hex characters are represented in pascal by using the '$' sign before the hex value like $AA; you can concatenate several chars by using $AA$55$09; you may be tempted to do $AA5509 which would compile but would represent a different value.
var tempstr : string;
tempstr := $AA$55$09$00$20$FF$FF$03$26;
OR
tempstr := $AA+$55+$09+$00+$20+$FF+$FF+$03+$26;
OR
tempstr := chr($AA)+chr($55)+chr($09)+chr($00)+chr($20)+chr($FF)+chr($FF)+chr($03)+chr($26);
ALL are valid.
Note: Because of the $00 in the fourth position do NOT use a PChar because $00 terminates (ends) the string.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
While the HEx values are hex literals inside your pascal source code, I would have asked the question this way:
"How can I write direct binary data out to the com port, for which I know the hex values".
That avoids the terrible confusion between your way of expressing the binary values in your source code (as hexadecimal constants which you use to create bytes (represented in Unicode delphi as Characters).
Note that the UnicodeString based port (4.11b) handles this in one way, and that another earlier unicode port by me, uses AnsiString. Be very careful when you use either one that you understand the byte values (8 bits of data) that actually come out your com port.
Warren
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hello
I am using the Comport Library to read and write data from a my comport which is connected to a scale
I started with the reading of the data which initially gave me funny ASCII characters whereby after a bit of investigation I was able to convert this to Hex and get the values I expected
I am now trying to communicate with the scale by writing HEX to the ComPort. I am not sure if I am doing this right. My question is do with what format do I send the data in as the WriteStr method obviously wants you to send a string
So how do I send my Hex value to the comport with the comport library?
Can I do the following
TempStr := 'AA55090020FFFF0326';
Comport1.WriteStr(TempStr);
I have tested this and although its sends the data I don't get a reply from the scale which I should if what I sent was correct
Do I need to send the data byte for byte or is one string fine?
Do I need to convert the data back to ASCII?
Thanks
Mark
This is really not a ComPort question it is a basic Pascal question, how to hex characters are represented in a string.
Hex characters are represented in pascal by using the '$' sign before the hex value like $AA; you can concatenate several chars by using $AA$55$09; you may be tempted to do $AA5509 which would compile but would represent a different value.
var tempstr : string;
tempstr := $AA$55$09$00$20$FF$FF$03$26;
OR
tempstr := $AA+$55+$09+$00+$20+$FF+$FF+$03+$26;
OR
tempstr := chr($AA)+chr($55)+chr($09)+chr($00)+chr($20)+chr($FF)+chr($FF)+chr($03)+chr($26);
ALL are valid.
Note: Because of the $00 in the fourth position do NOT use a PChar because $00 terminates (ends) the string.
Hi
Apologies, yes it is a Delphi/Pascal Question although it is relevant to the use of the ComPort and I haven't had much success on other forums
A big thank you to your reply though, it is exactly what I was looking for (missing) and it works 100%
While the HEx values are hex literals inside your pascal source code, I would have asked the question this way:
"How can I write direct binary data out to the com port, for which I know the hex values".
That avoids the terrible confusion between your way of expressing the binary values in your source code (as hexadecimal constants which you use to create bytes (represented in Unicode delphi as Characters).
Note that the UnicodeString based port (4.11b) handles this in one way, and that another earlier unicode port by me, uses AnsiString. Be very careful when you use either one that you understand the byte values (8 bits of data) that actually come out your com port.
Warren