Nuno
Junior Member
Posts: 64
|
Post by Nuno on Mar 24, 2023 13:22:55 GMT -5
Hello all,
I need to implement a way to generate 8 bit words, that will be sent through a COM port. Problem is, can't find a way to generate real 8 bit words. If I use the function DECHEX$(x), the x decimal number will be transformed into it's hexadecimal string equivalent.
As in the example below:
x=255 dechex$(x)
will generate "FF". Seems like I have a 8 bit word "11111111". but is not what happens. I actually get two ASCII "F"s, which is "0100011001000110". Not what I'm looking for.
The other way I tried is with hexdec("XX"). where "XX" is the hexadecimal equivalent of my binary number
I'm assuming this would generate the number I want, and not it's ASCII representation, However, also doesn't seem to work, as once sent through the COM port, the receiver does nothing. Meaning it didn't recognize the number.
Anyone knows a way of generating such 8 bit words reliably on demand?
Thank you
|
|
|
Post by Rod on Mar 24, 2023 13:35:51 GMT -5
An eight bit word is a bit off the mark since eight bit “things” are really characters. A character is a byte, 8 bits, a word is two bytes, 16 bits. Hex often confuses because two symbols represent a single byte.
Anyway forget all that and just use chr$() to represent the eight bit byte you want to send.
Post code if you want exact help.
If you are sending to the com port remember to use the ; character to stop the automatic addition of line feed.
Again post code so we can understand what’s going wrong.
|
|
Nuno
Junior Member
Posts: 64
|
Post by Nuno on Mar 24, 2023 13:58:21 GMT -5
An eight bit word is a bit off the mark since eight bit “things” are really characters. A character is a byte, 8 bits, a word is two bytes, 16 bits. Hex often confuses because two symbols represent a single byte. Anyway forget all that and just use chr$() to represent the eight bit byte you want to send. Post code if you want exact help. If you are sending to the com port remember to use the ; character to stop the automatic addition of line feed. Again post code so we can understand what’s going wrong. The techicalities on the amount of bits are: 8bits --> 1 Byte, 16bits --> 1 word. But, as you said, forget it. I was talking about a group of 8 bits, that are written as 2 hex symbols.
Didn't knew about that ";" at the end to avoid the line feed. Good tip. Thanks! Will try the chr$() and see how it goes. will post the result and code. *************************************************************************** edited **********
Just tested this code to see what happened:
open "com";portn;":9600,n,8,2,ds0,cs0,rs" for random as #comm print #comm, chr$(3);chr$(0);chr$(1);chr$(0);chr$(1);chr$(240);chr$(0); 'MODBUS RTU 30101F000
didn't work... I need to send, in hex format: "0300010001F000"
|
|
|
Post by Rod on Mar 24, 2023 14:41:29 GMT -5
|
|
Nuno
Junior Member
Posts: 64
|
Post by Nuno on Mar 24, 2023 14:46:54 GMT -5
Silly me... My MODBUS RTU packet was incomplete... Should have been: "010300010001D5CA". The device addr was missing...
Working now! Thanks for the tips on the chr$() and ";"
|
|