2

I have googled, read, experimented etc and still have had no luck sending a packet of 5 hex values over serial. Here is what I have tried:

import serial
import time

ser = serial.Serial(
    port='dev/serial0''
    baudrate=9600'
    parity=serial.PARITY_NONE'
    stopbits=serial.STOPBITS_ONE'
    bytesize=serial.EIGHTBITS,
    timeout=1
    )
#tried this
cw = b'0x55,0x18,0x03,0x06,0x01'
ser.write(serial.to_bytes(cw))

#tried this
cw = b'\x55\x18\x03\x06\x01'
ser.write(serial.to_bytes(cw)

the name of the file is serialtest.py
I've tried:
python serialtest.py

i've tried python 3:
python3 serialtest.py

it either prints out on minicom: jibberish or actually

0x55,0x18,0x03,0x06,0x01 

but I am not actually convinced this is actually hex data or just a string conversion of what is being sent. I can send the same hex packet from my windows box and the machine it is connected to does accordingly. Any help, ideas, examples, anything would be appreciated. I've wasted at least a week and a half on this.

Chad G
  • 1,043
  • 1
  • 7
  • 14
mScientist
  • 107
  • 1
  • 1
  • 6

2 Answers2

2

Try it with:

cw = [0x55,0x18,0x03,0x06,0x01]

ser.write(serial.to_bytes(cw))
bummi
  • 109
  • 3
Steve Amor
  • 36
  • 1
  • still looks like it may be sending characters instead of hex. It is sending (or reading rather on the loopback in minicom) a U, which is 0x55 ascii. – mScientist Apr 08 '19 at 20:22
  • @mScientist remember that "hex" is just a representation of a value, just as "U" (in ASCII encoding) is. – Ghanima Apr 08 '19 at 20:29
  • how would I pass in one of the hex values as a variable? – mScientist Apr 08 '19 at 20:56
  • You are asking a different question to the original post. cw in my example above is a Python list. If we assume that 0x03 is the hex value you want to change, you can change it with the following cw[2] = variable – Steve Amor Apr 09 '19 at 07:46
2

sending a packet of 5 hex values, ...

I agree with the above comments that the character "U" is encoded in ASCII as "0x55".
In other words, sending "U" is the same thing as sending "0x55".

To make things as simple as possible, I have tried sending only 1 hex value, instead of 5 hex values:

  1. Repeatedly sending b'\0x55'
  2. Repeatedly sending b'\0x5a'

Below are the scope screen captures. If you think I am doing what you have been trying, I can do more examples such as sending the following:

  1. b'\0x55\0x5a\0x5a\0x5a\0x55'
  2. b'UUUUU'

You can find my python send char/hex program in the answer of the following post. Serial to Arduino totally non-responsive

send 0x55 1/2 send 0x55 2/2 send 0xa5 1/2 send 0xa5 2/2

tlfong01
  • 4,665
  • 3
  • 10
  • 24