Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9048147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T12:04:59+00:00 2026-06-16T12:04:59+00:00

I’m building a webserver that would need to read (and keep reading) the serial

  • 0

I’m building a webserver that would need to read (and keep reading) the serial port of the machine it’s running on.
The purpose is to be able to read a barcode scanner, and using Server-Sent Events to update a browser with the read barcode.

I’m using flask to do this. I’ve browsed around and some implementations require only flask, some say I would need an async library like Gevent, and some others even say I’d need Gevent and some sort of queue like Redis or RabbitMQ.

I’ve tried to base my code on a very simple example I found on stackoverflow here. I have it mostly working, but I am stuck with some questions;

  • In Chrome there is a cross-origin error, by adding an
    Access-Control-Allow-Origin header I can get it to work in FireFox,
    but Chrome still doesn’t work. Is it correct that only FF supports
    SSE cross-origin? I need it to support CORS because the browser will
    need to load the barcode data from a separate machine.
  • After each message, the browser shows the barcode in the console, but
    it then closes the connection and only opens it again after about 3
    seconds. It seems that this originates in Flask, it gives me the data
    and then just stops.
  • Also, I’m wondering how this will perform under load. I mean, flask
    keeps a connection open for the text/event-stream mimetype. If
    multiple clients connect, won’t it block flask after a while, because
    all of the connections will be saturated?

My code is as follow (shortened for clarity)

Server-side:

from flask import Flask
import flask
import serial

app = Flask(__name__)
app.debug = True

def event_barcode():
    ser = serial.Serial()
    ser.port = 0
    ser.baudrate = 9600
    ser.bytesize = 8
    ser.parity = serial.PARITY_NONE
    ser.stopbits = serial.STOPBITS_ONE
    ser.open()
    s = ser.read(7)
    yield 'data: %s\n\n' % s

@app.route('/barcode')
def barcode():
    newresponse = flask.Response(event_barcode(), mimetype="text/event-stream")
    newresponse.headers.add('Access-Control-Allow-Origin', '*')
    return newresponse

if __name__ == '__main__':
    app.run(port=8080, threaded=True)

Client-side:

    <!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv=Content-Type content="text/html; charset=utf-8">
    <title>TEST</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript" charset="utf-8"></script>
    <script>

        $(document).ready(function(){
            if (!!window.EventSource) {
                console.log('SSE supported.');
                var source = new EventSource('http://localhost:8080/barcode');

                source.addEventListener('message', function(e) {
                  console.log(e.data);
                }, false);

                source.addEventListener('open', function(e) {
                  console.log('Connection was opened.');
                }, false);

                source.addEventListener('error', function(e) {
                  if (e.readyState == EventSource.CLOSED) {
                    console.log('Connection was closed.');
                  }
                }, false);

            } else {
                console.log('SSE notsupported.');
            }
        });

    </script>
</head>

<body>

</body>
</html>

There is some more information I was looking at here:
http://www.socketubs.net/2012/10/28/Websocket_with_flask_and_gevent/
http://sdiehl.github.com/gevent-tutorial/#chat-server

I hope someone can clear up my questions, and maybe point me towards some solutions, for the cross-origin and the 3 second delay problem.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T12:05:00+00:00Added an answer on June 16, 2026 at 12:05 pm

    Answering my own questions

    1. It seems that indeed only Firefox supports CORS for SSE for now ->
      article
    2. With help from Janus Troelsen I figured out how to keep the
      connection open and send several barcodes across the line (see code
      below)
    3. Performance-wise it seems that I can only make one connection, but
      that might be because I have only one serial port, the subsequent
      connections can’t open the serial port anymore. I think it will work
      from flask, but something with socketio and gevents will perfom
      better because it’s more suited for the job. There’s an interesting
      article here.

    For the code:

    import flask
    import serial
    from time import sleep
    
    app = flask.Flask(__name__)
    app.debug = True
    
    def event_barcode():
        messageid = 0
        ser = serial.Serial()
        ser.port = 0
        ser.baudrate = 9600
        ser.bytesize = 8
        ser.parity = serial.PARITY_NONE
        ser.stopbits = serial.STOPBITS_ONE
        ser.timeout = 0
        try:
            ser.open()
        except serial.SerialException, e:
             yield 'event:error\n' + 'data:' + 'Serial port error({0}): {1}\n\n'.format(e.errno, e.strerror)
             messageid = messageid + 1
        str_list = []
        while True:
            sleep(0.01)
            nextchar = ser.read()
            if nextchar:
                str_list.append(nextchar)
            else:
                if len(str_list) > 0:
                    yield 'id:' + str(messageid) + '\n' + 'data:' + ''.join(str_list) + '\n\n'
                    messageid = messageid + 1
                    str_list = []
    
    @app.route('/barcode')
    def barcode():
        newresponse = flask.Response(event_barcode(), mimetype="text/event-stream")
        newresponse.headers.add('Access-Control-Allow-Origin', '*')
        newresponse.headers.add('Cache-Control', 'no-cache')
        return newresponse
    
    if __name__ == '__main__':
        app.run(port=8080, threaded=True)
    

    Because I want to support multiple browsers, SSE is not the way to go for me right now. I will look into websockets and try and work from that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
In my XML file chapters tag has more chapter tag.i need to display chapters
I am doing a simple coin flipping experiment for class that involves flipping a
I would like to run a str_replace or preg_replace which looks for certain words

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.