0

I've a python file reads serial data from arduino which is generated in loop.

#!/usr/bin/python
import serial
ser = serial.Serial('/dev/ttyACM0', 9600)
while True:
  print(ser.readline())
ser.close()

How to import these data by Javascript?

I used xmlhttprequest to open the python file in a loop but it's re-opening the python file rather than streaming the serial data.. Can I loop the xmlhttprequest response to retrieve the data?

Is there anyway to retrieve the python's data in javascript?

4
  • 1
    So you are getting data in python, correct? And the data you got you simply want to read in JS, right? Commented May 23, 2021 at 7:44
  • @shaswat.dharaiya, Exactly.. Yes Commented May 23, 2021 at 7:45
  • Do you want to read it after the data has been received or do you want to read while data is being streamed? Commented May 23, 2021 at 7:46
  • Any, but just to be sure javascript is not missing any data update (to be showing sensor's update on time, or just after being received). Commented May 23, 2021 at 7:49

1 Answer 1

1

Try this then.

This will write a file in python

f = open("sensor.txt", "a")
while True:
  f.write(ser.readline())
ser.close()
f.close()

I am guessing you are using some HTML with JS, so try this to read it in JS.

<body>
    <input type="file" name="inputfile"
            id="inputfile">
    
    <script>
        document.getElementById('inputfile')
            .addEventListener('change', function() {
              
            var fr=new FileReader();
            fr.onload=function(){
                document.getElementById('output')
                        .textContent=fr.result;
            }
              
            fr.readAsText(this.files[0]);
        })
    </script>
</body>
Sign up to request clarification or add additional context in comments.

1 Comment

Simpler: elm.textContent = await this.files[0].text()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.