Canedje
Posts: 265
Joined: Thu Mar 26, 2015 7:18 am

USB packets loss or corrupted

Mon Apr 13, 2015 3:53 pm

I'm using a Raspberry reading information with USB0 from a smart energy meter by the P1 port from the smart meter.
I did write code at Python to read the port and store the data at a logfile and MYSQL Database.
The P1 is sending every 10 seconds 15 lines of information.
With a Crontab job I do read the P1 port every 5 minutes by USB0 by running the Python code.

At the basic this is working fine.
Only roughly after about 12 hours the USB0 is missing packets or the packages are corrupted. This wrong reading is going on for about 20 a 30 minutes then after that for about12 hours everything is working fine again.
I did some debugging and my conclsion at that time was that the P1 port did send corrupted information and I couldn't do anything against it.
Now I'm not sure anymore after reading this link:
viewtopic.php?f=29&t=20749

This topic shows that Raspberry is sometimes having trouble reading USB.
Is somebody here knowing this trouble with USB and Raspberry is still the case?
If yes is there a fixe or work around known to solve this problem?

Edit:

I followed this link for a test.
https://bitcointalk.org/index.php?topic=843788.0
Wondering if it will work

EDIT2:
This is not the solution
Maybe still the P1 is causing the problem 8-)

Canedje
Posts: 265
Joined: Thu Mar 26, 2015 7:18 am

Re: USB packets loss or corrupted

Wed Apr 15, 2015 12:35 pm

It looks like this problem is hard to solve and to be recognized by someone.

Maybe someone can help me by the correct code?
I used the next code in Python to read the USB. Is somebody see errors? or maybe tips to optimize the code?
For example I did use the FlushInput() command in the past.
or is there an alternative way to write code to approach the USB0 port?
I do use the variable stack. Is this maybe a protected variable not usable the way I use??
I do use the import serial. I also see on the internet the import PyUSB? What is the difference?
What import to use?

Any advise will be appreciated.
This drives me crazy :oops:

Code: Select all

import serial
#Set COM port config
ser = serial.Serial()
ser.baudrate = 9600
ser.bytesize=serial.SEVENBITS
ser.parity=serial.PARITY_EVEN
ser.stopbits=serial.STOPBITS_ONE
ser.xonxoff=0
ser.rtscts=0
ser.timeout=20
ser.port="/dev/ttyUSB0"

  #Initialize
  # stack is mijn list met de 15 regeltjes.
  p1_teller = 0
  stack=[]
  while p1_teller < 15:
      p1_line=''
      #Read 1 line
      try:
          p1_raw = ser.readline()
          if debug: debug("ingelezen regel: ", p1_raw, False)   #zet de ingelezen regel in de debug file
      except:
          sys.exit ("Seriele poort %s kan niet gelezen worden. Programma afgebroken." % ser.name )      
     
      p1_str=str(p1_raw)
      stack.append(p1_str)
      p1_teller = p1_teller + 1

  #Close port and show status
  try:
      ser.close()
  except:
      sys.exit ("Oops %s. Programma afgebroken." % ser.name )
 
 
Please advise to optimize or give alternative right code tot test

Canedje
Posts: 265
Joined: Thu Mar 26, 2015 7:18 am

Re: USB packets loss or corrupted

Fri Apr 17, 2015 10:07 am

Update:

I changed the Python code:
I recognized that I was opening and closing the serial port in a "15 times" while loop.
(To much for the serial port??)
I replaced the opening and reading of the serial port out of the while loop.
Beside that I added serial.flushInput() at opening and before closing the serial port.

Until now the RPI is running without errors for 24 hrs

Return to “Troubleshooting”