0

I trying to read by serial differents values, but i dont know hoy to split that, because the two values are numbers but from different source

First i have a PICAXE sending converted data by ADC of light sensor by serial to python. Second i have a PICAXE sending data of temperature sensor by serial to python.

Light code PICAXE

symbol puerto = B.5
main: readadc10 puerto,w1    ; read value into w1
sertxd(#w1,cr,lf)
goto main       ; loop back to start

Temp code PICAXE

symbol temp = B.4

readtemp temp, w0    ; read value into w1
debug
sertxd(#w0,cr,lf)
goto main

Python code

   import pygame
   import sys, serial
   from pygame.locals import *




   ser = serial.Serial()
   ser.port = 3
   ser.baudrate = 4800

   while True:

        datos = ser.readline()            
        grados = float(datos)
        print grados

The problem is that picaxe send simultaneus data from light and temp, but when python receive data, i dont know how to recognized each data.

Anyone can help me??

Thank!

dsolimano
  • 8,870
  • 3
  • 48
  • 63

1 Answers1

0

If you have a temperature reading and a light level reading to send at the same time, you could put them on one line separated by a space.

PICAXE:

sertxd(#w0," ",#w1,cr,lf)

Python:

readings = ser.readline()
[reading1, reading2] = readings.split()
temperature = float(reading1)
lightlevel = float(reading2)

If the two types of reading are produced irregularly, you could transmit a character before each one to identify what type it is.

PICAXE:

sertxd("T ",#w0,cr,lf)
...
sertxd("L ",#w1,cr,lf)

Python:

reading = ser.readline()
[readingtype, readingvalue] = reading.split()
if readingtype == "T":
    temperature = float(readingvalue)
elif readingtype == "L":
    lightlevel = float(readingvalue)
nekomatic
  • 5,988
  • 1
  • 20
  • 27