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?
-
1So you are getting data in python, correct? And the data you got you simply want to read in JS, right?shaswat.dharaiya– shaswat.dharaiya2021年05月23日 07:44:02 +00:00Commented May 23, 2021 at 7:44
-
@shaswat.dharaiya, Exactly.. Yesm sayed– m sayed2021年05月23日 07:45:30 +00:00Commented 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?shaswat.dharaiya– shaswat.dharaiya2021年05月23日 07:46:37 +00:00Commented 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).m sayed– m sayed2021年05月23日 07:49:46 +00:00Commented May 23, 2021 at 7:49
1 Answer 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>
Endless
38.8k14 gold badges118 silver badges138 bronze badges
answered May 23, 2021 at 7:53
shaswat.dharaiya
4515 silver badges14 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Endless
Simpler:
elm.textContent = await this.files[0].text()default