Sep-16-2020, 12:11 PM
Dear all,
I am trying to read data from usb using serial library. The other side of the computer is FPGA. The communication startting with sending data (0xFF) to FPGA. If the data is matched with decided one, FPGA will send 5 differnet data which are 16 bits. However, sometimes, commmunication misses the data and it catches the next 8 bits like its own data. Thus, when I combine two 8 bits, the obtained data is wrong. For instance, computer is sending 0xFF in order to start communication, FPGA sends 104 (0000 0000 0110 1000) (LSB 8 bit first (0110 1000), so next 8 bit MSB is zero (0000 0000)). Nevertheless, computer can catch second 8 bits which is equal to 0 as LSB(0000 0000). the real LSB 8 bit is equal to 104 (0110 1000) and unfortunately cannot catch it. When another data is sent which is equal to 108 (0000 0000 0110 1100), python catches LSB 8 bit (0110 1100). When I combine two 8 bit data, it is becoming 104000. this wrong data readig goes for a while. It happens again and this time, it reads true data because, data is becoming its own place in second error. It is going on like this. When I try to draw data, it is jumping everywhere. I have made a research and find a library named asyncio. It is a little bit difficult to understand how to use it.
Can someone help me about it pls?
I am posting my code.
I am trying to read data from usb using serial library. The other side of the computer is FPGA. The communication startting with sending data (0xFF) to FPGA. If the data is matched with decided one, FPGA will send 5 differnet data which are 16 bits. However, sometimes, commmunication misses the data and it catches the next 8 bits like its own data. Thus, when I combine two 8 bits, the obtained data is wrong. For instance, computer is sending 0xFF in order to start communication, FPGA sends 104 (0000 0000 0110 1000) (LSB 8 bit first (0110 1000), so next 8 bit MSB is zero (0000 0000)). Nevertheless, computer can catch second 8 bits which is equal to 0 as LSB(0000 0000). the real LSB 8 bit is equal to 104 (0110 1000) and unfortunately cannot catch it. When another data is sent which is equal to 108 (0000 0000 0110 1100), python catches LSB 8 bit (0110 1100). When I combine two 8 bit data, it is becoming 104000. this wrong data readig goes for a while. It happens again and this time, it reads true data because, data is becoming its own place in second error. It is going on like this. When I try to draw data, it is jumping everywhere. I have made a research and find a library named asyncio. It is a little bit difficult to understand how to use it.
Can someone help me about it pls?
I am posting my code.
import random
from itertools import count
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
from matplotlib.animation import FuncAnimation
#from tkinter import *
import serial
import time
plt.style.use('fivethirtyeight')
fig = plt.figure()
ser=serial.Serial()
ser.port = 'COM4'
ser.baudrate = 57600
ser.bytesize = serial.EIGHTBITS
ser.parity = serial.PARITY_NONE
ser.stopbits = serial.STOPBITS_ONE
#ser.timeout = 1
ser.open()
#root=Tk()
a_vals = []
b_vals = []
c_vals = []
q_vals = []
d_vals = []
x_vals = []
index = count()
def animate(i):
package = bytearray()
package.append(0x7F)
package.append(0x7F)
ser.write(package)
Ia_low1 = ser.read(1)
Ia_high1 = ser.read(1)
Ib_low1 = ser.read(1)
Ib_high1 = ser.read(1)
Ic_low1 = ser.read(1)
Ic_high1 = ser.read(1)
Iq_low1 = ser.read(1)
Iq_high1 = ser.read(1)
Id_low1 = ser.read(1)
Id_high1 = ser.read(1)
package = bytearray()
package.append(0x0F)
package.append(0x0F)
ser.write(package)
Ia_low = ord(Ia_low1)
Ia_high = ord(Ia_high1)
Ib_low = ord(Ib_low1)
Ib_high = ord(Ib_high1)
Ic_low = ord(Ic_low1)
Ic_high = ord(Ic_high1)
Iq_low = ord(Iq_low1)
Iq_high = ord(Iq_high1)
Id_low = ord(Id_low1)
Id_high = ord(Id_high1)
Ia = Ia_high*1000 + Ia_low
Ib = Ib_high*1000 + Ib_low
Ic = Ic_high*1000 + Ic_low
Iq = Iq_high*1000 + Iq_low
Id = Id_high*1000 + Id_low
time.sleep (0.1)
a_vals.append(Ia)
b_vals.append(Ib)
c_vals.append(Ic)
q_vals.append(Iq)
d_vals.append(Id)
x_vals.append(random.randint(0, 100))
ax1 = plt.subplot(121)
ax1.cla()
ax1.plot(x_vals, a_vals, label='Ia')
ax1.plot(x_vals, b_vals, label='Ib')
ax1.plot(x_vals, c_vals, label='Ic')
ax1.set_title('Ia, Ib, Ic illustration window')
ax1.set_xlabel('sample')
ax1.set_ylabel('current')
ax1.legend()
ax2 = plt.subplot(122)
ax2.cla()
ax2.plot(x_vals, q_vals, label='Iq')
ax2.plot(x_vals, d_vals, label='Id')
ax2.set_title('Iq, Id illustration window')
ax2.set_xlabel('sample')
ax2.set_ylabel('current')
ax2.legend()
ani = FuncAnimation(fig, animate, interval = 200)
plt.tight_layout()
plt.show()
