Python Serial Read 返回奇怪的值

标签 python string serial-port

我有一个来自另一个网站的小型 python 示例。我试图了解如何使用它从串行读取。

我通过串行从 FRDM K64f 板发送一条消息,Python 程序读取此消息但返回一个奇怪的值,下面是其中之一的示例:

YVkJ�ZC

我的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
>> 

最佳答案

设法解决这个问题。

我的序列号声明似乎无法正常工作。

回到 pyserial 文档并像下面这样声明我的序列号并使用 readline() 解决了问题。

ser = serial.Serial('/dev/ttyACM0')

关于Python Serial Read 返回奇怪的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35725705/

相关文章:

python - 如何获得信号的高低包络

python - 将不同数量的字符附加到字符串列表中的字符串

java - Java中的RS232通信问题

python - 以下代码适用于 python 解释器,但不能作为脚本

python - 艰难地学习Python ex24

java - 在 Java 中读取法语字符

c++ - 采访 : Adding two binary numbers given as strings

Python - 从字符串中删除前 5 个字符 - 如何获取新的较短字符串?

c# - 无法连接到mysql数据库C#

python - 使用 Cron 从其目录运行程序