简体   繁体   English

python脚本与嵌入式板交互

[英]python script to interact with embedded board

I want to write a Python script to interact with an Embedded board through COM4. 我想编写一个Python脚本,以通过COM4与嵌入式板进行交互。

I used Teraterm and could able to execute what I wanted. 我使用了Teraterm,并且能够执行我想要的。 Basically, I just want to get some information from the board. 基本上,我只想从董事会中获取一些信息。
Ex: If I send Ver, the board replies back with Version number If I send Serv, the board replies back with the list of its services 例如:如果我发送Ver,则董事会会以版本号回复。如果我发送Serv,则董事会将以其服务清单回复。

I don't know how to write a Python script to achieve the same. 我不知道如何编写Python脚本来实现相同目的。

Following is my code. 以下是我的代码。 The problem is that ser.readline() is reading what I am sending . 问题是ser.readline()正在读取我发送的内容 The output is: b'ver' and b'serv'. 输出为:b'ver'和b'serv'。

Please suggest me what changes should I do in my script to make it work. 请建议我应该对脚本进行哪些更改以使其起作用。

Thanks. 谢谢。

    import serial
    import time
    import sys

    ser = serial.Serial(baudrate=115200, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, timeout=5.0)
    ser.port = "COM4"
    ser.open()
    if ser.isOpen():
      print("Open: ",ser.portstr)
    print('--------')       


    ser.write(bytes('ver',encoding='ascii'))
    time.sleep(1)
    print(ser.readline())

    time.sleep(1)

    ser.write(bytes('serv',encoding='ascii'))
    time.sleep(1)
    print(ser.readline())

    ser.close()

To make it simple, I just used the following code. 为简单起见,我只使用了以下代码。 In this case, I am getting a blank output as b' ' continuously. 在这种情况下,我连续获得b''的空白输出。

import serial
import time
import sys

ser = serial.Serial(baudrate=115200, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, timeout=5.0)
ser.port = "COM4"
ser.open()
if ser.isOpen():
  print("Open: ",ser.portstr)
print('--------')       

ser.write(bytes('ver',encoding='ascii'))
while 1:
        print(ser.readline())

ser.close()

Your code looks fine. 您的代码看起来不错。 When you send something over pySerial, what you get back is what you sent, along with anything the board responds with. 当您通过pySerial发送邮件时,您获得的回报就是您发送的邮件以及董事会回应的所有内容。 I think your problem relates the the following lines: 我认为您的问题与以下几行有关:

print(ser.readline())

If you look through the readline() documentation you will notice that the readline() only reads up to the the eol character. 如果仔细阅读readline()文档,您会发现readline()仅读取eol字符。

What your board returns might look something like this: 董事会的回报可能看起来像这样:

serv           #your input
\n             #newline character
output_text    #board output
\n             #newline character

but ser.readline() stops reading at the newline character because it only reads one line. 但是ser.readline()停止读取换行符,因为它只读取一行。

You should be able to fix this by calling ser.readline() several times (at least twice) until all of the output is read or use one of the other reading methods 您应该能够通过多次(至少两次)调用ser.readline()来解决此问题,直到读取所有输出或使用其他读取方法之一为止

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM