Reputation: 41
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 ?
Upvotes: 4
Views: 6965
Reputation: 26
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.
Upvotes: 1
Reputation: 1
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)
Upvotes: 0