1
votes

I want to write to serial port to embedded micro-controller,

I am using serial to USB converter.

My code is as below (Sample code):

import serial
import time

ec = serial.Serial('COM2', baudrate=57600)

print(ec.isOpen())

cmd=str(input('cmd >> '))

ncmd = cmd+'\r\n'

ec.write(ncmd.encode())

time.sleep(2)

data = ec.read_all()

time.sleep(1)

print(data)

ec.close()

if ec.close():
    print('port closed')**

But every time I run code with some command I do not get expected output. e.g. If I send command '4', it is expected that it should read all error data from controller. But instead it gives following empty message;

True
cmd >> 4
b''

Could anyone please help? Where am I doing something wrong?

1
what is uploaded to the target microcontroller? - Z4-tier
I am not aware about source code of controller. But when I user tera term and send cmd 4 and get the expected output. - Ravi Shrikhande
try it without the carriage return \r - Nullman
I would also assume a mistake in line termination/protocol. Please print the repr of your ncmd and show this to us. - deets
Hi..working it now. Actually port was encrypted. Now it working when using another decrypted port. thanks @Z4-tier and Nullman - Ravi Shrikhande

1 Answers

0
votes

Before accessing any serial port make sure that serial port is not encrypted.

My code mentioned above is solved when serial port decrypted.