Does the serial port not work?

I created a program that sends data to my arduin, which determines what was sent, and then turns on the correct output depending on which key is pressed.

When using arduino software on a Windows computer, the arduino sketch works fine, I can turn each output on and off by sending either WAS or D.

When sending via node, the RX light on the arduino flashes, but nothing else happens.

Can anyone help?

Node.js program:

var httpServer = require('http').createServer(function(req, response){ /* Serve your static files */ }) httpServer.listen(8080); var nowjs = require("now"); var everyone = nowjs.initialize(httpServer); everyone.now.logStuff = function(msg){ console.log(msg); } var SerialPort = require('serialport2').SerialPort; var assert = require('assert'); var portName; if (process.platform == 'win32') { portName = 'COM4'; } else if (process.platform == 'darwin') { portName = '/dev/cu.usbserial-A800eFN5'; } else { portName = '/dev/ttyUSB0'; } var readData = ''; var sp = new SerialPort(); sp.on('close', function (err) { console.log('port closed'); }); sp.on('error', function (err) { console.error("error", err); }); sp.on('open', function () { console.log('port opened... Press reset on the Arduino.'); }); sp.open(portName, { baudRate: 9600, dataBits: 8, parity: 'none', stopBits: 1, flowControl: false }); everyone.now.forward = function() { sp.write("w"); } everyone.now.back = function() { sp.write("s"); } everyone.now.left = function() { sp.write("a"); } everyone.now.right = function() { sp.write("d"); } sp.on('data', function(data) { console.log(data.toString()); }); 

Arduino program:

 void setup(){ Serial.begin(9600); Serial.write("READY"); //Set all the pins we need to output pins pinMode(8, OUTPUT); pinMode(9, OUTPUT); pinMode(10, OUTPUT); pinMode(11, OUTPUT); } void loop (){ if (Serial.available() > 0) { //read serial as a character char ser = Serial.read(); Serial.write(ser); //NOTE because the serial is read as "char" and not "int", the read value must be compared to character numbers //hence the quotes around the numbers in the case statement switch (ser) { case 'w': move(8); break; case 's': move(9); break; case 'a': move(10); break; case 'q': move(10); move(8); break; case 'd': move(11); break; case 'e': move(11); move(8); break; } } } void move(int pin){ Serial.print(pin); digitalWrite(pin, HIGH); delay(1); digitalWrite(pin, LOW); } 
+8
javascript arduino node-serialport
source share
3 answers

I recently got into this. Arduino is automatically reset when it receives serial communication from most other things besides Arduino IDE. This is why you can send messages from the IDE, but not node.js.

I have a Uno and put a capacitor between the Reset and Ground.Here page with some good information on this.
Good luck. http://arduino.cc/playground/Main/DisablingAutoResetOnSerialConnection

+9
source share

There is a problem on the capacitor and reset ... There is a small capacitor between one of the serial control lines and reset on the Arduino in later models. This capacitor causes the Arduino to reset when the port is open, but otherwise it will not interfere with normal serial operation.

This reset trick allows you to load Arduino reset code as part of the boot process. When the Arduino starts the code loader, it starts first for a short time before the loaded code runs.

Download process: reset Arduino, which launches the bootloader, starts the boot process in the Arduino development environment, establishes a connection, downloads, and then runs the downloaded code. When the Arduino starts, it waits for loading for a short period of time, if none is received, it proceeds to run the code.

I find this very useful as it allows you to effectively reset the Arduino just by closing and opening the port. In old Arduino's, without this capacitor, you had to press the reset button at the right time to load the code. And the time was such that Arduino spent much more time waiting before he started with the loaded code.

In the problem described here, I don't think he had problems due to the reset trick used. He should have had only the Arduino reset effect when he opened the serial port, and from the look of his information, this is the desired side effect.

+1
source share

I use node daily to send actions to Arduino via usb or via bt, and it works great in both cases. I think your problem comes from sending emails. You should send a buffer instead of the ascii value for the letter:

 myPort.write(Buffer([myValueToBeSent])); 

for this, I think, it will be better for you with some "logical" interface, with data headers, number of actions, such things. It is not required for you, but it will make your code more reliable and easier in the future in the future.

Here is an example of how I do it. Node first:

 var dataHeader = 0x0f, //beginning of the data stream, very useful if you intend to send a batch of actions myFirstAction = 0x01, mySecondAction = 0x02, myThirdAction = 0x03; 

Then you call them like you:

 everyone.now.MyBatchOfActions = function() { sp.write(Buffer([dataHeader])); sp.write(Buffer([0x03])); // this is the number of actions for the Arduino code sp.write(Buffer([myFirstAction])); sp.write(Buffer([mySecondAction])); sp.write(Buffer([myThirdAction])); } 

Thus, it is easy to get Serial.read () data on Arduino: (Note that you need to define the header and data below)

 void readCommands(){ while(Serial.available() > 0){ // Read first byte of stream. uint8_t numberOfActions; uint8_t recievedByte = Serial.read(); // If first byte is equal to dataHeader, lets do if(recievedByte == DATA_HEADER){ delay(10); // Get the number of actions to execute numberOfActions = Serial.read(); delay(10); // Execute each actions for (uint8_t i = 0 ; i < numberOfActions ; i++){ // Get action type actionType = Serial.read(); if(actionType == 0x01){ // do you first action } else if(actionType == 0x02{ // do your second action } else if(actionType == 0x03){ // do your third action } } } } } 

Hope I understand and hope this helps! Hooray!

+1
source share

All Articles