<div dir="ltr">ok will give it a go.... </div><div class="gmail_extra"><br><div class="gmail_quote">2015-10-06 23:01 GMT+02:00 Bruno Girin <span dir="ltr"><<a href="mailto:brunogirin@gmail.com" target="_blank">brunogirin@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">Bo,<div><br></div><div>The data is JSON. However, because of the way your code reads the data, it starts reading the beginning of the second line. See the additional { in the message (first character second line):</div><div><br></div><div><div style="font-size:12.8px">{"@":"151f","+":4,"T|C16":336,"O":1,"vac|h":94,"v|%":0}</div><div style="font-size:12.8px"><b>{</b>Traceback (most recent call last):</div></div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">So you need to add detection of the newline in your code to ensure that the string you pass to json.loads is a single line and if you have more characters after that newline, push them to the next. A good way to do this would be using a generator function, so something like:</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">def read_lines_from_serial(ser):</div><div style="font-size:12.8px">    <span style="font-size:12.8px">while 1:</span></div><div style="font-size:12.8px">       # Read from serial port, blocking</div><div style="font-size:12.8px">       data = ser.read(1)</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">       # If there is more than 1 byte, read the rest</div><div style="font-size:12.8px">       n = ser.inWaiting()</div><div style="font-size:12.8px">       if n:</div><div style="font-size:12.8px">           data = data + ser.read(n)</div><div style="font-size:12.8px">           lines = data.splitlines()</div><div style="font-size:12.8px">           # iterate over all lines except the last one</div><div style="font-size:12.8px">           # (will do nothing if no newlines in data)</div><div style="font-size:12.8px">           for l in lines[:-1]:</div><div style="font-size:12.8px">               yield l</div><div style="font-size:12.8px">           # assign the last line to data so that we can keep appending to it</div><div style="font-size:12.8px">           data = lines[-1]</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">Then you should be able to call your function like this:</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">for d in read_lines_from_serial(ser):</div><div style="font-size:12.8px">    j = json.loads(d)</div><div style="font-size:12.8px">    # do whatever is sensible with j</div><div style="font-size:12.8px"><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On 6 October 2015 at 21:40, Bo Herrmannsen <span dir="ltr"><<a href="mailto:bo.herrmannsen@gmail.com" target="_blank">bo.herrmannsen@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">could it be that the data is not json but just looks like it?</div><div><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>
</div></div><br>_______________________________________________<br>
OpenTRV-dev mailing list<br>
<a href="mailto:OpenTRV-dev@lists.opentrv.org.uk" target="_blank">OpenTRV-dev@lists.opentrv.org.uk</a><br>
<a href="http://lists.opentrv.org.uk/listinfo/opentrv-dev" rel="noreferrer" target="_blank">http://lists.opentrv.org.uk/listinfo/opentrv-dev</a><br>
<br></blockquote></div><br></div>
<br>_______________________________________________<br>
OpenTRV-dev mailing list<br>
<a href="mailto:OpenTRV-dev@lists.opentrv.org.uk">OpenTRV-dev@lists.opentrv.org.uk</a><br>
<a href="http://lists.opentrv.org.uk/listinfo/opentrv-dev" rel="noreferrer" target="_blank">http://lists.opentrv.org.uk/listinfo/opentrv-dev</a><br>
<br></blockquote></div><br></div>