PySerial dataSend不接收数据

2024-06-01 03:08:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在python脚本和atxmega128a3u avr微控制器之间建立一个连接。在

Python脚本:

#!/usr/bin/python3

from termcolor import colored
import sys
import serial
import struct

class usbComm:
    'USB Communication Class'
    def __init__(self, Port):
        'Try to open the port and initialize the object'
        try:
            self.PortObj = serial.Serial(Port, 9600, timeout=1)
        except OSError as e:
            print(colored("ERROR: Failed to open {0}\nERROR: {1}".format(Port, str(e)), "red"))
            exit(1)
        print(colored("Connected to " + self.PortObj.name, "green"))

    def SendData(self, Data):
        'Send raw data to 3D Printer'
        'Returns total number of bytes written'
        return self.PortObj.write(Data)

    def ReadData(self, count):
        return self.PortObj.read(count)

    def __del__(self):
        try:
            self.PortObj.close()
            print(colored("Closed", "green"))
        except AttributeError as e:
            print(colored("ERROR: " + str(e), "red"))

def main():
    print(colored("+-----------------------------------+", "blue"))
    print(colored("| 3D Printer USB Communication Tool |", "blue"))
    print(colored("+-----------------------------------+", "blue"))

    if len(sys.argv) != 3:
        print(colored("ERROR: Invalid number of arguments.", "red"))
        print("Usage: main.py <PORT> <DATA>")
        exit(1)

    usbCommObj = usbComm(sys.argv[1])
    print(usbCommObj.SendData(b"\x7e"))
    print(usbCommObj.ReadData(1))

if __name__ == "__main__":
    main()

所以我应该收到“~”但是输出是:

连接到/dev/ttyACM10 1 b'\xff' 关闭

在我的微控制器上我有代码:

^{pr2}$

前一段时间我可以得到一个工作连接,我错了什么?在


Tags: toimportself脚本mainportdefsys