2

I have a Raspberry Pi running Raspbian (a Debian flavor: Linux version 3.12-1-rpi ([email protected]) (gcc version 4.7.2 (Debian 4.7.2-5+rpi1) ) #1 Debian 3.12.9-1+rpi1 (2014-05-19) ).

I have a serial output device (electricity meter) connected via USB. The serial port is read with a Python script every minute.

EDIT: The data that I'm expecting conforms to this standard: DSMR v3.0 A data-telegram is offered every ten seconds. The Python script is started by cron every minute to read one data-telegram.

My dmesg, var/log/messages and /var/log/syslog are being flooded by these:

Nov  2 10:32:07 electron kernel: [151762.159243] ttyUSB0: 2 input overrun(s)

I've adjusted /etc/rsyslog.conf. By removing *.=kern; I managed to suppress these messages going into /var/log/messages. Unfortunately, dmesg and /var/log/syslog continue to show these messages.

/var/log/syslog is mentioned nowhere in /etc/rsyslog.conf. Is it fed by a different mechanism? How can I suppress these messages from ttyUSB0 going into dmesg and /var/log/syslog?

For those concerned that the cause might be found in the Python script (any improvements are appreciated). Here is the script I use (post-processing of data omitted):

#! /usr/bin/python

import sys, serial, re

port = serial.Serial()
port.baudrate = 9600
port.bytesize = serial.SEVENBITS
port.parity = serial.PARITY_EVEN
port.stopbits = serial.STOPBITS_ONE
port.xonxoff = 1
port.rtscts = 0
port.dsrdtr = 0
port.timeout = 0
port.port = "/dev/ttyUSB0"

def gettelegram():
  # flag used to exit the while-loop
  abort = 0
  # countdown counter used to prevent infinite loops
  loops2go = 40
  # storage space for the telegram
  telegram = []
  # end of line delimiter
  delim = "\x0a"

  try:
    port.open()
    serial.XON
  except:
    abort == 4
    # open error terminates immediately
    return telegram, abort

  while abort == 0:
    try:
      # this doesn't seem to work
      #line = str(port.readline()).strip()
      line = "".join(iter(lambda:port.read(1),delim)).strip()
    except:
      # read error, terminate prematurely
      abort = 2
    if line == "!":
      abort = 1
    if line != "":
      telegram.append(line)
    loops2go = loops2go - 1
    if loops2go < 0:
      abort = 3

  # test for correct start of telegram
  if telegram[0][0] != "/":
    abort = 2

  try:
    serial.XOFF
    port.close()
  except:
    abort == 5

  # Return codes:
  # abort == 1 indicates a successful read
  # abort == 2 means that no valid data was read from the serial port
  # abort == 3 indicates a data overrun. More lines were received than expected.
  # abort == 4 indicates a serial port open error.
  # abort == 5 indicates a serial port close error.
  return (telegram, abort)

if __name__ == "__main__":
  telegram, status = gettelegram()

EDIT: I've done some extra testing. It just so happens that the input overrun error happens AFTER the above program has finished. I tried adding a port.XOFF() just before the port.close(), but that doesn't help.

EDIT2: I've tried inserting serial.XON and serial.XOFF around the port.read(1) command. But, that didn't help.

7
  • @Mohsen: Thanks for the link. If this is an RTFM-hint then please be aware that I know of and have read the Serial HOWTO. So, apparently I'm missing something here, as I can't find the cause. Care to be more specific? :-)
    – Mausy5043
    Nov 2, 2014 at 10:00
  • I believe it is because you are not reading all of the input from the serial device. It is then being queued up, until the buffers are full, then data is being losed. Nov 2, 2014 at 11:20
  • Does the program work? Nov 2, 2014 at 11:30
  • Why disconnect (close)? Every time you disconnect, you will have to resynchronise. Your current synchronisation code seems to be “abort if not synchronised”, Nov 2, 2014 at 11:31

2 Answers 2

0

Since I nolonger have this problem I'd like to share my solution:

I've daemonized the Python script. This enabled me to open the serial connection on start-up of the script and then read the serial-port data every ten seconds.

No more overruns since.

Code of the script is here on GitHub

-2

http://hintshop.ludvig.co.nz/show/persistent-names-usb-serial-devices/

IT helped me and I got solution at instant.

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .