nodejs: http listen interferes with serialport reads

Internet of Things IoT Frameworks 2 years ago

0 1 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating
_x000D_ _x000D_ I'm trying to read in data from an arduino using serialport, and serve it to a web browser. Without the webserver (ie. if I just leave out that 'listen' call at the end), the serial data gets constantly streamed in with the expected 5 updates per second shown in the console. But when I add the 'listen' call, nothing is shown on the console until I make a request to the server with my web browser, at which time the console gets at most only one log entry added (but sometimes still nothing). The data shown in the web browser is the 'old' data from whenever the last request was made, not the current latest data from the arduino. In other words, the serial data is processed a little after each http request is served - not very useful. const http = require('http'); const serialport = require('serialport'); var serial = new serialport('/dev/ttyUSB0', { baudRate: 115200 }); var jsonStr = ''; var jsonObj = {}; function handleData(data) { jsonStr += data; if ( data.indexOf('}') > -1 ) { try { jsonObj = JSON.parse(jsonStr); console.log(jsonObj); } catch(e) {} jsonStr = ''; } }; serial.on('data', function (data) { handleData(data); }); const app = http.createServer((request, response) => { response.writeHead(200, {"Content-Type": "text/html"}); response.write(JSON.stringify(jsonObj)); response.end(); }); app.listen(3000); (The data coming from the arduino is already a JSON string which is why I'm looking for a '}' to start parsing it.) I also tried using the 'readable' event for getting the serial data but it makes no difference: serial.on('readable', function () { handleData(serial.read()); }); If I understand it correctly, the listen call itself is not blocking, it merely registers an event listener/callback to be triggered later. As an accepted answer in a related question says: "Think of server.listen(port) as being kinda similar to someElement.addEventListener('click', handler) in the browser." If node.js is single threaded then why does server.listen() return? So why is that 'listen' preventing the serial connection from receiving anything, except for briefly each time a request is served? Is there no way I can use these two features without them interfering with each other?

Posted on 16 Aug 2022, this text provides information on IoT Frameworks related to Internet of Things. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (1)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago
_x000D_ I discovered that the code worked as expected on a different computer, even though the other computer was using the exact same operating system (Fedora 20) and the exact same version of node.js (v10.15.0) which had been installed in the exact same way (built from source). I also found that it worked ok on the original computer with a more recent version of Fedora (29). This likely points to some slight difference in usb/serial drivers which I don't have the time, knowledge or need to delve into. I'll just use the configurations I know will work.

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

Important Internet of Things Links

tuteehub community

Join Our Community Today

Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.

tuteehub community