i using node serialport communicate wtih arduino uno board. following code have used.
serialport.on("open", function () { console.log('open'); serialport.on('data', function(data) { console.log('data received: ' + data); }); serialport.write('s1\n', function(err, results) { if(err) console.log('err ' + err); else console.log('results ' + results); }); });
when give input 's1' terminal (like teraterm or cool term) 0 or 1 result. basically, s1 command read status of particular sensor. if sensor activated 1 returned , if not 0 returned.
however, in above code snippet, results 3 (which number of characters in string used input. so, instead of 's1\n' if put 's11\n' result returned 4. doing wrong here?
the .on('data') event never triggered. but, .on('open') event triggers, indicates connection board ok.
the write call non-blocking , result byteswritten
, values seeing make sense. while double-checking docs found super useful note:
note: devices arduino reset when open connection them. in these cases if write device wont ready receive data. worked around having arduino send "ready" byte node program waits before writing. can away waiting around 400ms.
i can confirm that's true. wait "--start--" message firmware before sending requests.
bonus: watch out leftover junk in buffers when arduino resets. i've seen messages "temperatureupdat--start--". 2 ideas dealing this:
- scan "--start--" using endswith command, not exact match. downside being can't have "--start--" in other messages. smart message send , becomes non-issue).
- send blank line, start message (e.g. "\nstart\n"). downside being need expect 2 messages (i.e. empty message or junk or partial message followed valid start command)
Comments
Post a Comment