在FPGA Uart上使用Pyserial进行串行数据传输

时间:2018-05-09 13:46:20

标签: python pyserial serial-communication

我知道这已经讨论了很多,但无法找到解决问题的正确方法。 我正在努力在FPGA上将数据发送到我的Uart。 FPGA部分很清楚,我在FPGA ide上使用8位UART接收器\ tx。 数据看起来像 000101010101 101010110110 101011001011 。 。 。 。 .. 101010110110(即12列数据和4096行ASCII值)

有人可以通过某种方式帮助我将这些数据从pyserial以二进制形式连续发送到Uart吗? 提前谢谢..

1 个答案:

答案 0 :(得分:0)

我在玩pyserial时写的旧代码可能有用:

class myser():
    timer = ''
    buffer = b''
    ser = ''
    def __init__(self):
        pass

    def Cancel_Timer(self):
        try:
            self.timer.cancel()
        except Exception as ex:
            template = "ct: An exception of type {0} occured. Arguments:\n{1!r}"
            message = template.format(type(ex).__name__, ex.args)
            print(message)

    def OpenSerial(self, COM, Baud, Parity, sBits, dBits, Timeout):
        try:
            self.ser = Serial(port=COM, baudrate=Baud, parity=Parity,
                         stopbits=sBits, bytesize=dBits, timeout=Timeout)
        except Exception as ex:
            template = "op: An exception of type {0} occured. Arguments:\n{1!r}"
            message = template.format(type(ex).__name__, ex.args)
            print(message)

    def SendSerial(self, data):   
        try:
            if self.ser.is_open:
                s = str(data)
                chars = []
                for c in s:
                    chars.append(ord(c))
                    chars = list(map(int, chars))
                self.ser.write(chars)
                self.ser.flush() 
        except Exception as ex:
            template = "ss: An exception of type {0} occured. Arguments:\n{1!r}"
            message = template.format(type(ex).__name__, ex.args)
            print(message)

    def ReadSerial(self, tmr, period):
        try:
            if self.ser.inWaiting() > 0:
                __stb = self.ser.read(self.ser.inWaiting())
                self.buffer += __stb
        except Exception as ex:
            template = "rs: An exception of type {0} occured. Arguments:\n{1!r}"
            message = template.format(type(ex).__name__, ex.args)
            print(message)
        if not tmr:
            return
        self.timer = threading.Timer(period, self.ReadSerial, [ True, period])
        self.timer.start()
        return


mySer = myser()
mySer.OpenSerial('COM7', 19200, 'N', 1, 8, 0)
data = '010111010001'
mySer.SendSerial(data)