#!/usr/bin/env python

#Python app to run a K-30 Sensor
import serial
import time
from optparse import OptionParser
import sys

ser = serial.Serial("/dev/serial0")
#print("Serial Connected!", file=sys.stderr)
ser.flushInput()
time.sleep(1)

parser = OptionParser()
parser.add_option("-t", "--average-time", dest="avgtime",
                  help="Report value averaged across this period of time", metavar="SECONDS")

(options, args) = parser.parse_args()

sum = 0
num = int(options.avgtime)
num_init = num

while True:
    #ser.write("\xFE\x44\x00\x08\x02\x9F\x25".encode())
    ser.write("\xFE\x44\x00\x08\x02\x9F\x25")
    time.sleep(.01)
    resp = ser.read(7)
    high = ord(resp[3])
    low = ord(resp[4])
    co2 = (high*256) + low
    sum += co2
    num -= 1
    #print(time.strftime("%c") + ": CO2 = " + str(co2) + " ppm", file=sys.stderr)
    if (num > 0):
        time.sleep(1)
    if (num == 0):
        break

#print(int(sum/num_init))
print int(sum/num_init)

@@ -14,16 +14,17 @@
 v python
+3
 %0A%0A#Pytho
@@ -126,121 +126,8 @@
 ys%0A%0A
-ser = serial.Serial(%22/dev/serial0%22)%0A#print(%22Serial Connected!%22, file=sys.stderr)%0Aser.flushInput()%0Atime.sleep(1)%0A%0A
 pars
@@ -302,45 +302,345 @@
 S%22)%0A
-%0A(options, args) = parser.parse_args(
+parser.add_option(%22-d%22, %22--device%22, dest=%22serial%22, default=%22serial0%22,%0A                  help=%22Device name in /dev%22)%0A%0A(options, args) = parser.parse_args()%0A%0Adevice = %22/dev/%22 + options.serial%0Aser = serial.Serial(device, baudrate = 9600, timeout = .5)%0Aprint(%22Serial %22 + device + %22 Connected!%22, file=sys.stderr)%0Aser.flushInput()%0Atime.sleep(1
 )%0A%0As
@@ -705,17 +705,16 @@
 ue:%0A    
-#
 ser.writ
@@ -719,53 +719,75 @@
 ite(
-%22%5CxFE%5Cx44%5Cx00%5Cx08%5Cx02%5Cx9F%5Cx25%22.encode(
+serial.to_bytes(%5B0xFE, 0x44, 0x00, 0x08, 0x02, 0x9F, 0x25%5D
 ))%0A
+%0A
     
+#
 ser.
@@ -867,16 +867,69 @@
 read(7)%0A
+    # print(%22Resp: %25d%22 %25 len(resp), file=sys.stderr)%0A
     high
@@ -935,20 +935,15 @@
 h = 
-ord(
 resp%5B3%5D
-)
 %0A   
@@ -953,20 +953,15 @@
 w = 
-ord(
 resp%5B4%5D
-)
 %0A   
@@ -1168,17 +1168,16 @@
  break%0A%0A
-#
 print(in
@@ -1193,16 +1193,17 @@
 _init))%0A
+#
 print in

Fix k30 write bytes sequence