我有一个小python示例,我从另一个网站。我试图了解如何从串口中读取它。
我通过串口从FRDM K64f板发送消息,python程序读取此但返回一个奇怪的值,下面是其中一个示例:
YVkJZC
我的python代码:
import time
import serial
# configure the serial connections (the parameters differs on the device you are connecting to)
ser = serial.Serial(
port='/dev/ttyACM0',
baudrate=9600,
parity=serial.PARITY_ODD,
stopbits=serial.STOPBITS_TWO,
bytesize=serial.SEVENBITS
)
ser.isOpen()
print 'Enter your commands below.\r\nInsert "exit" to leave the application.'
input=1
while 1 :
# get keyboard input
input = raw_input(">> ")
# Python 3 users
# input = input(">> ")
if input == 'exit':
ser.close()
exit()
else:
# send the character to the device
# (note that I happend a \r\n carriage return and line feed to the characters - this is requested by my device)
ser.write(input + '\r\n')
out = ''
# let's wait one second before reading output (let's give device time to answer)
time.sleep(1)
while ser.inWaiting() > 0:
out += ser.read(1)
if out != '':
print ">>" + out
这是我的董事会代码:
int main(){
Serial pc(USBTX, USBRX);
pc.baud(9600);
while(1){
char c = pc.getc();
if((c == 'w')) {
pc.printf("Hello");
}
}
}
我得到的确切回报是:
Enter your commands below.
Insert "exit" to leave the application.
>> w
>>YVkJ�ZC
>>
答案 0 :(得分:1)
管理解决此问题。
我的连载声明似乎没有正常工作。
回到pyserial文档并在下面声明我的序列,并使用readline()解决了这个问题。
ser = serial.Serial('/dev/ttyACM0')