我正在使用python的串行库进行uart通信。通信是在fpga和python 3之间进行的。我正在从fpga到在Windows 10上运行的python发送常量值,即104 108 112 115 120作为16位(LSB字节为0)。但是,有时python正在读取数据如104000、108000、112000、115000和120000。看起来python读取了属于下一个提交数据的一个字节并将其组合在一起。因此,该值是错误的。 有人可以帮我找到问题吗?我已经从fpga端检查了每个数据,并且工作正常。 代码如下:
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)
send_data = 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)
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))
print ("Ia =", Ia)
print ("Ib =", Ib)
print ("Ic =", Ic)
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()