Black Lives Matter - Action and Equality. ... Adafruit is open and shipping.
0

Sending UART messages
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Sending UART messages

by rdevries on Tue Jun 23, 2020 8:25 pm

Hello,
I am trying to send a message over a UART.

What I need to do is send a command, value, then a terminator.
If I enter the value as a constant it will work.

Code:
end_cmd = bytearray([0xFF,0xFF,0xFF])
DispUart.write(b'r1et.val='+ b'18' + end_cmd)

If I want to send a variable it doesn't.

Code:
DispUart.write(b'r1et.val='+ bytes(SumV) + end_cmd)

If I use
Code:
SumV = 18
print("Write ETime - ",SumV," ")
print(bytes(SumV))

I get
Code:
Write ETime - 18
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

If I try to convert the int to a string
Code:
SumV = '123'
DispUart.write(b'r1et.val='+ SumV + end_cmd)

Works, but if I try
Code:
SumV = str(Vr1tc_val)
DispUart.write(b'r1et.val='+ SumV + end_cmd)

Doesn't work.
What would the issue be?
Where can I find more examples of code snipits for the commands I am trying to use? (str, byte(), etc.)

Thank you

rdevries
 
Posts: 20
Joined: Wed Mar 02, 2011 12:04 am
Location: On. Canada

Re: Sending UART messages

by tannewt on Wed Jun 24, 2020 7:43 pm

I think you want s.encode("utf-8") to convert strings to bytes.

tannewt
 
Posts: 1685
Joined: Thu Oct 06, 2016 8:48 pm

Re: Sending UART messages

by rdevries on Sat Jun 27, 2020 3:59 pm

Thank you tannewt, I was able to get things to work.

It took a bit, but once the int was converted to a string then encode perform the encode.
Tmr1Dis = str.encode(str(TmrCh1Cnt),'utf-8')

Thank you for your help.

rdevries
 
Posts: 20
Joined: Wed Mar 02, 2011 12:04 am
Location: On. Canada

Please be positive and constructive with your questions and comments.