<div dir="ltr">could it be that the data is not json but just looks like it?</div><div class="gmail_extra"><br><div class="gmail_quote">2015-10-06 22:00 GMT+02:00 Bo Herrmannsen <span dir="ltr"><<a href="mailto:bo.herrmannsen@gmail.com" target="_blank">bo.herrmannsen@gmail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">now the script is:<div><br></div><div><div>import sys</div><div>import serial</div><div>import json</div><div><br></div><div>port = "/dev/ttyUSB0"</div><div>baud = 4800</div><div><br></div><div>ser = serial.Serial()</div><div>ser.port = port</div><div>ser.baudrate = baud</div><div><br></div><div>try:</div><div> ser.open()</div><div>except:</div><div> sys.stderr.write("Error opening serial port %s\n" % (ser.portstr) )</div><div> sys.exit(1)</div><div><br></div><div>ser.setRtsCts(0)</div><div><br></div><div>while 1:</div><div> # Read from serial port, blocking</div><div> data = ser.read(1)</div><div><br></div><div> # If there is more than 1 byte, read the rest</div><div> n = ser.inWaiting()</div><div> if n:</div><div> data = data + ser.read(n)</div><div><br></div><div><br></div><div><br></div><div> sys.stdout.write(data)</div><div><br></div><div> if data.startswith('{'):</div><div><br></div><div> j = json.loads(data)</div><div> print j['@']</div></div><div><br></div><div><br></div><div><br></div><div>my logic is to print what comes in from serial and that part works fine</div><div><br></div><div>i then want to build on it and if we have a json string then write just the units id...</div><div><br></div><div>the last part is failing me.. i get:</div><div><br></div><div><div><br></div><div>{"@":"151f","+":4,"T|C16":336,"O":1,"vac|h":94,"v|%":0}</div><div>{Traceback (most recent call last):</div><div> File "opentrvimport.py", line 35, in <module></div><div> j = json.loads(data)</div><div> File "/usr/lib/python2.7/json/__init__.py", line 326, in loads</div><div> return _default_decoder.decode(s)</div><div> File "/usr/lib/python2.7/json/decoder.py", line 365, in decode</div><div> obj, end = self.raw_decode(s, idx=_w(s, 0).end())</div><div> File "/usr/lib/python2.7/json/decoder.py", line 381, in raw_decode</div><div> obj, end = self.scan_once(s, idx)</div><div>ValueError: Expecting object: line 1 column 0 (char 0)</div></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">2015-10-06 17:05 GMT+02:00 Bo Herrmannsen <span dir="ltr"><<a href="mailto:bo.herrmannsen@gmail.com" target="_blank">bo.herrmannsen@gmail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hi<div><br></div><div>I started on a script that can read serial data from a rev2 unit and send the data on to emoncms.</div><div><br></div><div>i get this on the serial line</div><div><br></div><div><div>{"@":"2800","+":4,"T|C16":544,"vC|%":2340,"O":1}</div></div><div><br></div><div><br></div><div>and so far i have this python script</div><div><br></div><div><br></div><div><div>import serial</div><div>import io</div><div>import time</div><div>ser = serial.Serial('/dev/ttyUSB0',4800)</div><div><br></div><div><br></div><div>sio = io.TextIOWrapper(io.BufferedRWPair(ser, ser, 1), encoding='ascii',newline='\r\n')</div><div><br></div><div>while ser.isOpen():</div><div><br></div><div> datastring = sio.readline()</div><div><br></div><div>print datastring</div></div><div><br></div><div><br></div><div><br></div><div><br></div><div>the serial data end with CRLF</div><div><br></div><div>but it does not print anything.... where am i wrong?</div><div><br></div><div>before i used this script:</div><div><br></div><div><div style="font-size:12.8px">import serial</div><div style="font-size:12.8px">import time</div><div style="font-size:12.8px">s = serial.Serial('/dev/ttyUSB0',4800)</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">while 1:</div><div style="font-size:12.8px"> if s.inWaiting():</div><div style="font-size:12.8px"> val = s.read(s.inWaiting())</div><div style="font-size:12.8px"> print val</div></div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">but then i got:</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px"><div style="font-size:12.8px">{"@":"0950","</div><div style="font-size:12.8px">+":</div><div style="font-size:12.8px">5,"</div><div style="font-size:12.8px">t</div><div style="font-size:12.8px">T|C</div><div style="font-size:12.8px">":</div><div style="font-size:12.8px">1</div><div style="font-size:12.8px">9,"</div><div style="font-size:12.8px">vC|%</div><div style="font-size:12.8px">":</div><div style="font-size:12.8px">20</div><div style="font-size:12.8px">0,"</div><div style="font-size:12.8px">T|</div><div style="font-size:12.8px">C</div><div style="font-size:12.8px">1</div><div style="font-size:12.8px">6":</div><div style="font-size:12.8px">33</div><div style="font-size:12.8px">1}</div><div><br></div></div><div><br></div><div><br></div></div>
</blockquote></div><br></div>
</blockquote></div><br></div>