Problems when using bb_serial_read python

0

These are developing a small panel of monitoring in flask and I want to acquire some data from the serial port, the function that I am using to acquire the serial is:

import wiringpi
import wiringpi2 #as wiringpi
import random
import time
import sys
import read_PWM
import pigpio
import difflib
from thing import PiThing
from flask import *
from mlx90614 import MLX9
@app.route("/com")

def com():
    try:
            pi = pigpio.pi()
            pi.set_mode(RX, pigpio.INPUT)
            pi.bb_serial_read_open(RX, 115200, 8)

    except:
            pi.bb_serial_read_close(RX)
            pi.stop()

    def com_read():
            #dato =  data
            port = {'dato':pi.bb_serial_read(RX)}
            yield 'data: {0}\n\n'.format(json.dumps(port))
            time.sleep(1)
    return Response(com_read(), mimetype='text/event-stream')

When I run this code the debugger signals the following error

My code that front that handles that function is the following:

//serial
     var comSource = new EventSource("{{url_for ('com')}}");
        comSource.onmessage = function(c) {
          updateCom($.parseJSON(c.data));


       // Funcion para actulizar el serial

       function updateCom(comState){
            console.log('Serial: '+ comState.port);
            $("#com_serial").text(comState.port);
             }

And the data does not pass to the front

For now I just try to see it in the javascrip console.

also try to pass it in the following way:

    @app.route("/com")


    def com():
    try:
            pi = pigpio.pi()
            pi.set_mode(RX, pigpio.INPUT)
            pi.bb_serial_read_open(RX, 115200, 8)
            (data, count) = pi.bb_serial_read(RX)
            if count:
                    print data
                    print count
                    time.sleep(1)
    except:
            pi.bb_serial_read_close(RX)
            pi.stop()
    def com_read():
            dato =  data
            port = {'dato':dato}
            yield 'data: {0}\n\n'.format(json.dumps(port))
            time.sleep(1)
    return Response(com_read(), mimetype='text/event-stream')

The error you send me is:

The function in the front is the same and appears the same as undefined

I think the problem is when I turn it into a tuple or when I want to convert it to json format, but the truth is, I do not know how to pass it.

    
asked by Marco Báez 29.08.2018 в 00:08
source

0 answers