[英]Serial Communication issue in hardware interface with python platform
I have one hardware which uses Asynchronous serial interface protocol, 9600 baud rate, #Start bits 1,#Data bits 8, #Stop bits 1, Parity None. 我有一个硬件使用异步串行接口协议,9600波特率,#Start位1,#数据位8,#停止位1,奇偶校验无。 When I am communicating my hardware with a python script using a cp2102 device that time hardware doesn't respond but when I try to communicate the same hardware with Arduino nano then it works properly.
当我使用cp2102设备与python脚本通信我的硬件时,硬件没有响应,但当我尝试与Arduino nano通信相同的硬件时,它正常工作。
Working Arduino code: 工作Arduino代码:
void volt_currentTest()
{
Serial.write(0xDD);
Serial.write(0xA5);
Serial.write(0x03);
Serial.write(0x00);
Serial.write(0xFF);
Serial.write(0xFD);
Serial.write(0x77);
while(c != 0x77)
{
if(Serial.available())
{
if((c = Serial.read()) != -1)
{
Serial.println(c , HEX);
d[i] = c;
i++;
}
}
}
}
Python code: Python代码:
import serial
ser = serial.Serial(
'COM9', 9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE
)
'''data = b'\xdd\xa5\x03\x00\xff\xfd\x77'
ser.write(data)'''
ser.write(b'\xDD')
ser.write(b'\xA5')
ser.write(b'\x03')
ser.write(b'\x00')
ser.write(b'\xFF')
ser.write(b'\xFD')
ser.write(b'\x77')
'''
ser.write('\xDD\r\n'.encode())
ser.write('\xA5\r\n'.encode())
ser.write('\x03\r\n'.encode())
ser.write('\x00\r\n'.encode())
ser.write('\xFF\r\n'.encode())
ser.write('\xFD\r\n'.encode())
ser.write('\x77\r\n'.encode())
'''
while True:
response = ser.read()
'''response = ser.readline()
'''
print(response)
So Anyone having an idea why it's not working ..... 所以任何人都知道为什么它不工作.....
You probably need to call ser.flush()
after all the write()s, but before the 'while' loop. 您可能需要在所有write()s之后但在'while'循环之前调用
ser.flush()
。
Python's serial.Serial uses buffered I/O by default – all your writes wait until you call flush(), after which they're sent in one batch. Python的serial.Serial默认使用缓冲的I / O - 所有的写操作都会等到你调用flush(),之后它们会在一个批处理中发送。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.