Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
wrong data reading on uart
#1
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.

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()
Reply
#2
Could anyone help or give any clue pls?
Reply
#3
I would suggest first trying to communicate with your FPGA
using a terminal emulator like Putty.

The only purpose of doing this is to assure that the device is working correctly prior to attempting communications with python.

asyncio is not the way to go with a simple serial connection, it's designed for concurrent processing with many devices.

if all is well with putty (or other terminal emulator), find an example code like that found in http://www.varesano.net/blog/fabio/seria...s%20python

FYI:
In the future, when you have a lot of text, space it out a bit more. Many don't like reading everything compressed into one clump, and it might be cause for slow response.
Reply
#4
Dear @Larz60+
Thanks for your suggestion. I have checked my FPGA design with a uart communication program and found any mistake. FPGA is communicating with computer. In addition, I have modified my code using thread.joın(). Worg data reading has reduced but, I want it omit completely. Could you give me idea to getover it?
Regadrs I

python generally used on rasberry, beaglebone etc. Is that possible there is bug on windows?
Reply
#5
have you set baud rate and handshaking in python so it matches that of the device?
Are you using any handshaking? (DSR, RTS, etc)?
Reply
#6
Hi Larz60+
Yes I have set baudrate 57600 for both python and FPGA. I am not using handshaking. I am sending 32639 data value from python and FPGA is checking if it is equal to 32639. If yes, FPGA starts to send data. When python reads, ıt sends a different data value such as 0X00. Then, FPGA closes the communication because the value is not equal to 32639. When python reads data, python sends again 32639 and it goes on.

Should I add a handshaking in my design?

My communication interface is usb to RS232 converter

Thanks
Reply
#7
Normally not needed. It's been so long since I did any work with serial communications, The communications is defiantly more reliable if handshaking is used, but will create software complications.

Maybe look through these packages, see if one looks promising, and either use it direct, or look at the code and see how they do it.
https://pypi.org/search/?q=usb+%2B+to+%2B+FPGA
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  UART & I2C slow down a loop trix 4 603 Dec-28-2023, 05:14 PM
Last Post: trix
  Reading All The RAW Data Inside a PDF NBAComputerMan 4 1,336 Nov-30-2022, 10:54 PM
Last Post: Larz60+
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,531 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  Reading Data from JSON tpolim008 2 1,074 Sep-27-2022, 06:34 PM
Last Post: Larz60+
  How to clean UART string Joni_Engr 4 2,471 Dec-03-2021, 05:58 PM
Last Post: deanhystad
  Help reading data from serial RS485 korenron 8 13,908 Nov-14-2021, 06:49 AM
Last Post: korenron
  Help with WebSocket reading data from anoter function korenron 0 1,328 Sep-19-2021, 11:08 AM
Last Post: korenron
  Fastest Way of Writing/Reading Data JamesA 1 2,184 Jul-27-2021, 03:52 PM
Last Post: Larz60+
  UART Serial Read & Write to MP3 Player Doesn't Work bill_z 15 5,786 Jul-17-2021, 04:19 PM
Last Post: bill_z
  Reading data to python: turn into list or dataframe hhchenfx 2 5,358 Jun-01-2021, 10:28 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020