-->
Page 2 of 3

Re: Sending bytes to serial port

PostPosted: Mon Apr 04, 2016 11:50 am
by cicciocb
Hi,
maybe this can help you :

Code: Select allin = "4D4D4953434F4F4C"
gosub [sub.from.hex]
print out
end


'input variable in
'output variable out
[sub.from.hex]
out = ""
for i = 1 to len(in)
a = asc(mid(in,i,1)) - 48
if (a > 9) then a = a - 7
i = i + 1
b = asc(mid(in,i,1)) - 48
if (b > 9) then b = b - 7
c = a << 4 or b
out = out & chr(c)
next i
return


This is a simple function that converts a string containing hex chars in text format to a string that contains the equivalent chars in binary format.
fill the string in the variable 'in' (ex. in = "C30D0A") ; the chars must be upper case and in couple (multiple of 2)
then call the sub (gosub [sub.from.hex])
the result will be in the variable out (ex. serialprint out)

Hoping this can be useful also to other people

CiccioCB

Re: Sending bytes to serial port

PostPosted: Mon Apr 04, 2016 12:29 pm
by Electroguard
Each command starts with byte 0x7E, followed by a length byte indicating the number of bytes that follow, followed by a byte indicating the command to execute (there are about 28 different possible commands - Play, VolUP, Next etc), followed by an optional first argument byte, followed by an optional second argument byte, followed by the 'end' byte 0xEF as the terminator.

For example, the command ”PLAY” (0x0D) is constructed with the following 4 bytes

0x7E – Start Byte
0x02 – 2 Bytes Follow
0x0D – Command Byte
0xEF – Termination Byte

Once I looked up all the hex to dec conversions on an ascii chart I was able to get it to work with your chr() suggestion, eg:
let vstart = 126
let vlen = 2
let vplay = 13
let vend = 239
serialprintln chr(vstart) & chr(vlen) & chr(vplay) & chr(vend)

So thanks for that.

When done, this will allow people to add super-simple 2-wire voice capability to ESP Basic using a £5 JQ6500 mp3 module or similar. I'll get things working as a browser controlled music player first, but the real intention is to provide udp network controlled voice messaging for triggered alarm announcements etc.

Re: Sending bytes to serial port

PostPosted: Mon Apr 04, 2016 12:36 pm
by Electroguard
Wow, thanks for that... your fingers work a lot quicker than my brain!

Perhaps worth posting the function in the Examples so others can find it.

Re: Sending bytes to serial port

PostPosted: Tue Apr 05, 2016 10:55 pm
by stern0m1
convert the hex string to text (http://www.unit-conversion.info/texttools/hexadecimal/) then just print to the serial port the text and it should work.

I had the same thing with a meter and it worked.

I used the input command and set the serial timeout for 1 second.