4

I am trying to communicate with my smartmeter, which works, partly. Basicly, I get the serial data from my meter through a FT232 USB-Serial cable. The smartmeter has a data logger port which sends out a telegram message every 10 seconds (9600 baud, 7 databit, even parity, 1 stopbit). The configuration file I use with minicom is

pu port             /dev/ttyUSB0
pu baudrate         9600
pu bits             7
pu parity           E
pu stopbits         1
pu minit            ~^M~AT S7=45 S0=0 L1 V1 X4 &c1 E1 Q0^M

This works, and I am able to succesfully recieve my data. But my goal was to recieve the data in python so I tried the following script

import serial

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"

ser.close()
ser.open()
print ("Waiting for P1 output on "  + ser.portstr)

counter=0
#read 20 lines    
while counter < 20:
    print ser.readline()
    counter=counter+1

try:
    ser.close()
    print ("Closed serial port.")
except:
    sys.exit ("Couldn't close serial port.")

This does not seem to work, it just times out after 20 seconds. I have also tried 'cu' with:

cu -l /dev/ttyUSB0 -s 9600 -oe

I found out that when running python script, and then run the minicom script, the python script would get the required data. I've also tried to change the Initialization string to ' ' which works, but if I don't set it at all, minicom doesn't get the data either

Does anyone have any idea ?

8
  • 1
    Could you post the minicom script - I am guessing that it does something to open the port that your python code doesn't. Alternatively - does it run as root? Commented Dec 15, 2013 at 17:23
  • @adrianmcmenamin I am not using any script for minicom, I simply 'run' the configuration file by running > minicom smartmconfig. Commented Dec 15, 2013 at 18:23
  • They do not run as root, they are run by a user which is in the dialout group. I've tried to run them as root though, with the same results. Commented Dec 15, 2013 at 18:35
  • what happens if you remove that first ser.close()? Commented Dec 15, 2013 at 19:53
  • I tried that, I does not make any difference. I'm realy pulling my hair out here. Commented Dec 15, 2013 at 19:57

2 Answers 2

1

I was having the same problem, and it turns out my transmitting device wasn't transmitting a line at a time, it was transmitting individual characters.

using:

bytesToRead = ser.inWaiting()
ser.read(bytesToRead)

allowed the data to start pouring in.

0

Similar problems, I can read the smartmeter but for 50% of the time it doesn't work. From the 20 lines it only read the first 15. 15 to 20 are ignored.

This is my solution use cu and capture the output

#!/usr/bin/python

# test voor cu -l /dev/ttyUSB0 -s 9600 --parity=none

import time
import os
import signal
import sys
import subprocess
from subprocess import Popen, PIPE

line = ''
teller = 0
stack = []
#Use a process group so as to enable sending a signal to all the process in the groups.
process = subprocess.Popen('cu -l /dev/ttyUSB0 -s 9600 --parity=none', shell=True, stdout=PIPE, bufsize=1, preexec_fn=os.setsid)

while teller < 20:
    line = process.stdout.readline()
    stack.append(line)
    print str(teller) + ' ' + line
    teller = teller + 1

#time.sleep(15)

os.killpg(process.pid, signal.SIGTERM) 

Not the answer you're looking for? Browse other questions tagged or ask your own question.