Bhargavpopat
Bhargavpopat

Reputation: 1

Serial data transfer using Pyserial on FPGA Uart

I know this has been discussed a lot but couldn't find the right solution to my problem. I am working on sending data into my Uart on an FPGA. The FPGA part is clear, I am using a 8 bit UART receiver\tx on the FPGA ide. The data looks like 000101010101 101010110110 101011001011 . . . . .. 101010110110 ( i.e. 12 columns data and 4096 rows ASCII values)

Can someone help me with some way to send this data from pyserial into the Uart in binary form serially?? Thank you in advance..

Upvotes: 0

Views: 1775

Answers (1)

10SecTom
10SecTom

Reputation: 2664

Old code I wrote when playing around with pyserial, might be of use:

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)

Upvotes: 1

Related Questions