[英]Not able to write and read from serial port using pyserial python script
I want to write to serial port to embedded micro-controller,我想写入串口到嵌入式微控制器,
I am using serial to USB converter.我正在使用串行到 USB 转换器。
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.但是每次我用一些命令运行代码时,我都没有得到预期的输出。 eg If I send command '4', it is expected that it should read all error data from controller.
例如,如果我发送命令'4',它应该从控制器读取所有错误数据。 But instead it gives following empty message;
但相反,它给出了以下空信息;
True
cmd >> 4
b''
Could anyone please help?有人可以帮忙吗? Where am I doing something wrong?
我哪里做错了?
Before accessing any serial port make sure that serial port is not encrypted.在访问任何串行端口之前,请确保该串行端口未加密。
My code mentioned above is solved when serial port decrypted.我上面提到的代码在串口解密时就解决了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.