HEX with serial in and out

Jixz

New Member
I really need some help! I'm trying to communicate with an external device via serial with my picaxe 28x2, the only problem is the device only talks in hex! So i need some help figuring out how to do serial I/O and communicate with this device. I think i've got the serout part down and goes something like this:

Code:
serout 6,N19200_16,($00)
is this right? I can't really know for sure until i get the serial input down.

and as far as the input, I have tried the following with no avail:

Code:
serin [15000,timeout], 5,T19200_16,b0
' Output to computer ( used to monitor picaxe )
sertxd($b0)
and
Code:
serin [15000,timeout], 5,T19200_16,$b0
' Output to computer ( used to monitor picaxe )
sertxd(b0)
 

westaust55

Moderator
I think you may need to describe the "hex" format that you require but here is a first take:


The PICAXE program SEROUT command really always outputs a value in binary
All computers really work in binary. Decimal, hexadecimal and ASCII are just alternative ways to represent a byte of data that we humans can better understand/read.

Have a look at the number conversion chart I created some time ago at post 20 here:
http://www.picaxeforum.co.uk/showthr...t=10893&page=2

So “A” (ASCII), $44 (hex), 65 (decimal) and %01000001 (binary) all represent the same value.


If you just send $65 that is sent in binary as %01100101

If you want the value to be seen in hex dump readable format then you neet to send this as "$65" in which case this is sent as 3 bytes being "$", "6" and "5" (which is digitally sent as %00100100, %00110110, %00110101 )



SO, if you need to send the ASCII equivalent of a hexadecimal number
For example $65 to be sent as “$65”


If you only have a few cases, then you could just put those directly into SEROUT command lines:
serout 6,N19200_16,(“$65”)

If you want to do many such values, or values defined by calcs, etc, then the code will be along these lines:
Code:
b0 = $65        ; the value to "convert"
b1 = b0/16    ; extract the high nybble  ie "6" in this example

IF b1 < 10 THEN
        b1 b1  + $30   ; for 0 to 9
ELSE
        b1 = b1 + $37  ; for A (10) to F (15)
ENDIF

b0 = b0 AND $0F    ; extract the low nybble  ie the "5" in this example
IF b0 < 10 THEN
        b0 = b0  + $30   ; for 0 to 9
ELSE
        b0 = b0 + $37  ; for A to F
ENDIF

serout 6,N19200_16,(&#8220;$&#8221;, b1, b0)
Obviously the above routine can be tightened up but left simple here for clarity
 
Top