문제

I've got a problem with my Python script. It's working but something is wrong. I'm newbie in Python so I can't find any solution :(

My script is connecting via pySerial to Arduino board, and read temperature data. It's connecting fine but data shown in terminal or save in TXT file (with Cron) is wrong:

2013-03-16 13:40:01 166.8
2013-03-16 13:41:02 1617.

where it should be:

2013-03-16 13:40:01 16.68
2013-03-16 13:41:02 16.17

My Python script:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# pytemp.py

import serial
import time

ser = serial.Serial('/dev/ttyACM0',9600, timeout=10)
read = ser.read(5)
comp = read.split()
ser.close();
print time.strftime("%Y-%m-%d %H:%M:%S"), comp[0]

I'm using Python3.3, with pySerial 2.6. In oryginal version there was:

read = ser.readline(eol=/r)

but as far as I know in 2.5+ eol command is not working anymore. I have no idea how to edit my script to print data correct all the time.

도움이 되었습니까?

해결책

According to the documentation, the eol parameter is indeed no longer supported when using Python version 2.6 of higher.

Please disregard my previous advice to use FileLike. That isn't using in python 2.6+ either!

How you have to handle the data depends on how the data looks. Since you haven't given us an example of the raw data, I'll be using the format from this page as an example.

The format of the data in the abovementioned example is:

t: 2012.11.18 19:39:03 50A93957 +024.50 0189
t: 2012.11.18 19:39:13 50A93961 +024.50 0189
t: 2012.11.18 19:39:23 50A9396B +024.50 0188

Each line has the following columns:

  • date and time
  • raw date and time in hex
  • temperature value in degree celsuis
  • raw temperature value in hex

You will notice that each measurement starts with a 't:', and has six items separated by spaces. So in this case I would run a loop that works like this:

import serial
import time

buffer = bytes()
ser = serial.Serial('/dev/ttyACM0',9600, timeout=10)
while buffer.count('t:') < 2:
    buffer += ser.read(30)
ser.close();
# Now we have at least one complete datum. Isolate it.
start = buffer.index('t:')
end = buffer.index('t:', start+1)
items = buffer[start:end].strip().split()
print items[1], items[2], items[4]

An example. Realize that you might begin reading in the middle of a line of data. You cannot assume you start reading at the beginning of a line.

In [23]: buffer = '39:03 50A9\r\nt: 2012.11.18 19:39:13 50A93961 +024.50 0189\r\nt: 2012.11.18 19:39:23 50A9396B +024.50 0188'

Let's check how many 't:''s we can find. (You could also search for '\r\n' instead. All that matters is that you have something to delimiter lines with)

In [24]: buffer.count('t:')
Out[24]: 2

Since we have two delimiters, we have at least one data point. Let's isolate the complete data point.

In [25]: buffer.index('t:')
Out[25]: 12

In [26]: buffer.index('t:', 12+1)
Out[26]: 58

This is what we want to see. A complete data point:

In [27]: buffer[12:58+1].strip().split()
Out[27]: ['t:', '2012.11.18', '19:39:13', '50A93961', '+024.50', '0189', 't']

In [28]: items = buffer[12:59].strip().split()

In [29]: print items[1], items[2], items[4]
2012.11.18 19:39:13 +024.50
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top