Matthieu Benard
Matthieu Benard

Reputation: 21

Send terminal command through Pyserial ascii format

I would like to create a Python programm like a terminal to send some request with Pyserial.

But when I send a request like "dataid 60000 get value" it show me an error message like : TypeError: unicode strings are not supported, please encode to bytes: 'dataid 60000 get value'

I tried to use .encode but no result..

See below my code :

#Modules
from base64 import encode
import serial

port = "COM5"
baud = 115200

#Serial port configuration
com = serial.Serial(port, baud, timeout=1)

if com.isOpen():
    print(com.name + ' is open...')

#Print output
while True:
    cmd = input("Enter command or 'exit':")
    if cmd == 'exit':
        com.close()
        exit()
    else:
        com.write(cmd)
        out = com.read()
        print('Receiving...'+out)

Thanks in advance ! :)

Upvotes: 2

Views: 1000

Answers (1)

bas
bas

Reputation: 9

To send command over serial/console port, use:
com.write(cmd.encode("utf-8"))
or
com.write(b"string")

This encodes your input to bytes.

Upvotes: 1

Related Questions