Skip to main content
Fixed syntax highlighting.
Source Link
VE7JRO
  • 2.5k
  • 19
  • 28
  • 32
http = require('http');
fs = require('fs');
serialport = require('serialport');
 
port = 3000;
host = '127.0.0.1';
SerialPort = serialport.SerialPort;
arduinoPort = '/dev/ttyUSB2';

var printLog = true;
var playerRoundKills = 0;
var playerTotalKills;
var roundLive = false;

//open port
var myPort = new SerialPort(arduinoPort);
parser: serialport.parsers.raw          //rohdatenparser definieren
serialport.on('open', function(){
  console.log('Serial Port Opend');
  serialport.on('data', function(data){
      console.log(data[0]);
  });
});

//implementation of abstract port functions
myPort.on('open', showPortOpen);
myPort.on('data', sendSerialData);
myPort.on('close', showPortClose);
myPort.on('error', showError);

function showPortOpen() {
   console.log('port open. Data rate: ' + myPort.options.baudRate);
}
 
//output incoming data
function sendSerialData(data) {
   console.log(data[0]);
if(typeof data[1] !== 'undefined') console.log(data[1]);
}
 
function showPortClose() {
   console.log('port closed.');
}
 
function showError(error) {
   console.log('Serial port error: ' + error);
}

myPort.on("open", function () {
  console.log('open');
//send data (3) to arduino
myPort.write(3, function(err, results) {
    if(err != null)console.log('Fehler beim ersten senden: ' + err);
    if(results != null)console.log("Killcount erstmals gesendet: "+results);
  });
});
http = require('http');
fs = require('fs');
serialport = require('serialport');
 
port = 3000;
host = '127.0.0.1';
SerialPort = serialport.SerialPort;
arduinoPort = '/dev/ttyUSB2';

var printLog = true;
var playerRoundKills = 0;
var playerTotalKills;
var roundLive = false;

//open port
var myPort = new SerialPort(arduinoPort);
parser: serialport.parsers.raw          //rohdatenparser definieren
serialport.on('open', function(){
  console.log('Serial Port Opend');
  serialport.on('data', function(data){
      console.log(data[0]);
  });
});

//implementation of abstract port functions
myPort.on('open', showPortOpen);
myPort.on('data', sendSerialData);
myPort.on('close', showPortClose);
myPort.on('error', showError);

function showPortOpen() {
   console.log('port open. Data rate: ' + myPort.options.baudRate);
}
 
//output incoming data
function sendSerialData(data) {
   console.log(data[0]);
if(typeof data[1] !== 'undefined') console.log(data[1]);
}
 
function showPortClose() {
   console.log('port closed.');
}
 
function showError(error) {
   console.log('Serial port error: ' + error);
}

myPort.on("open", function () {
  console.log('open');
//send data (3) to arduino
myPort.write(3, function(err, results) {
    if(err != null)console.log('Fehler beim ersten senden: ' + err);
    if(results != null)console.log("Killcount erstmals gesendet: "+results);
  });
});
#include <LiquidCrystal.h>

//LDCD Display
LiquidCrystal lcd(9, 8, 6, 5, 4, 3);  //Digitale Pins an dem das Display angeschlossen ist
#define LCD_WIDTH 16                    //Anzahl Spalten des Display (16)
#define LCD_HEIGHT 2                    //Anzahl Zeilen des Display (2)
 

 
#define DEBUG_MODE true        
 
void setup(void)
{
  Serial.begin(9600);  
  byte i;
 
  lcd.begin(LCD_WIDTH, LCD_HEIGHT,1);
 
  lcd.setCursor(0,0);
 
}

void loop() {
  // when characters arrive over the serial port...
  if (Serial.available()) {
    
    //clear display for new killcount
    //lcd.clear();
    // wait a bit for the entire message to arrive
    delay(100);
    // read all the available characters
    while (Serial.available() > 0) {
      // write each character to display as ascii
    int temp = Serial.read();
    lcd.write(temp);
    Serial.println(temp);
    
    } 
    //Serial.flush();
  }
}
#include <LiquidCrystal.h>

//LDCD Display
LiquidCrystal lcd(9, 8, 6, 5, 4, 3);  //Digitale Pins an dem das Display angeschlossen ist
#define LCD_WIDTH 16                    //Anzahl Spalten des Display (16)
#define LCD_HEIGHT 2                    //Anzahl Zeilen des Display (2)
 

 
#define DEBUG_MODE true        
 
void setup(void)
{
  Serial.begin(9600);  
  byte i;
 
  lcd.begin(LCD_WIDTH, LCD_HEIGHT,1);
 
  lcd.setCursor(0,0);
 
}

void loop() {
  // when characters arrive over the serial port...
  if (Serial.available()) {
    
    //clear display for new killcount
    //lcd.clear();
    // wait a bit for the entire message to arrive
    delay(100);
    // read all the available characters
    while (Serial.available() > 0) {
      // write each character to display as ascii
    int temp = Serial.read();
    lcd.write(temp);
    Serial.println(temp);
    
    } 
    //Serial.flush();
  }
}
http = require('http');
fs = require('fs');
serialport = require('serialport');
 
port = 3000;
host = '127.0.0.1';
SerialPort = serialport.SerialPort;
arduinoPort = '/dev/ttyUSB2';

var printLog = true;
var playerRoundKills = 0;
var playerTotalKills;
var roundLive = false;

//open port
var myPort = new SerialPort(arduinoPort);
parser: serialport.parsers.raw          //rohdatenparser definieren
serialport.on('open', function(){
  console.log('Serial Port Opend');
  serialport.on('data', function(data){
      console.log(data[0]);
  });
});

//implementation of abstract port functions
myPort.on('open', showPortOpen);
myPort.on('data', sendSerialData);
myPort.on('close', showPortClose);
myPort.on('error', showError);

function showPortOpen() {
   console.log('port open. Data rate: ' + myPort.options.baudRate);
}
 
//output incoming data
function sendSerialData(data) {
   console.log(data[0]);
if(typeof data[1] !== 'undefined') console.log(data[1]);
}
 
function showPortClose() {
   console.log('port closed.');
}
 
function showError(error) {
   console.log('Serial port error: ' + error);
}

myPort.on("open", function () {
  console.log('open');
//send data (3) to arduino
myPort.write(3, function(err, results) {
    if(err != null)console.log('Fehler beim ersten senden: ' + err);
    if(results != null)console.log("Killcount erstmals gesendet: "+results);
  });
});
#include <LiquidCrystal.h>

//LDCD Display
LiquidCrystal lcd(9, 8, 6, 5, 4, 3);  //Digitale Pins an dem das Display angeschlossen ist
#define LCD_WIDTH 16                    //Anzahl Spalten des Display (16)
#define LCD_HEIGHT 2                    //Anzahl Zeilen des Display (2)
 

 
#define DEBUG_MODE true        
 
void setup(void)
{
  Serial.begin(9600);  
  byte i;
 
  lcd.begin(LCD_WIDTH, LCD_HEIGHT,1);
 
  lcd.setCursor(0,0);
 
}

void loop() {
  // when characters arrive over the serial port...
  if (Serial.available()) {
    
    //clear display for new killcount
    //lcd.clear();
    // wait a bit for the entire message to arrive
    delay(100);
    // read all the available characters
    while (Serial.available() > 0) {
      // write each character to display as ascii
    int temp = Serial.read();
    lcd.write(temp);
    Serial.println(temp);
    
    } 
    //Serial.flush();
  }
}
http = require('http');
fs = require('fs');
serialport = require('serialport');
 
port = 3000;
host = '127.0.0.1';
SerialPort = serialport.SerialPort;
arduinoPort = '/dev/ttyUSB2';

var printLog = true;
var playerRoundKills = 0;
var playerTotalKills;
var roundLive = false;

//open port
var myPort = new SerialPort(arduinoPort);
parser: serialport.parsers.raw          //rohdatenparser definieren
serialport.on('open', function(){
  console.log('Serial Port Opend');
  serialport.on('data', function(data){
      console.log(data[0]);
  });
});

//implementation of abstract port functions
myPort.on('open', showPortOpen);
myPort.on('data', sendSerialData);
myPort.on('close', showPortClose);
myPort.on('error', showError);

function showPortOpen() {
   console.log('port open. Data rate: ' + myPort.options.baudRate);
}
 
//output incoming data
function sendSerialData(data) {
   console.log(data[0]);
if(typeof data[1] !== 'undefined') console.log(data[1]);
}
 
function showPortClose() {
   console.log('port closed.');
}
 
function showError(error) {
   console.log('Serial port error: ' + error);
}

myPort.on("open", function () {
  console.log('open');
//send data (3) to arduino
myPort.write(3, function(err, results) {
    if(err != null)console.log('Fehler beim ersten senden: ' + err);
    if(results != null)console.log("Killcount erstmals gesendet: "+results);
  });
});
#include <LiquidCrystal.h>

//LDCD Display
LiquidCrystal lcd(9, 8, 6, 5, 4, 3);  //Digitale Pins an dem das Display angeschlossen ist
#define LCD_WIDTH 16                    //Anzahl Spalten des Display (16)
#define LCD_HEIGHT 2                    //Anzahl Zeilen des Display (2)
 

 
#define DEBUG_MODE true        
 
void setup(void)
{
  Serial.begin(9600);  
  byte i;
 
  lcd.begin(LCD_WIDTH, LCD_HEIGHT,1);
 
  lcd.setCursor(0,0);
 
}

void loop() {
  // when characters arrive over the serial port...
  if (Serial.available()) {
    
    //clear display for new killcount
    //lcd.clear();
    // wait a bit for the entire message to arrive
    delay(100);
    // read all the available characters
    while (Serial.available() > 0) {
      // write each character to display as ascii
    int temp = Serial.read();
    lcd.write(temp);
    Serial.println(temp);
    
    } 
    //Serial.flush();
  }
}

I ran into some strange Problemsproblems while trying to send Datadata from my Node.js Server to my Arduino. My Goalgoal is to transfer an IntInt to the Arduino Nano and display it on a LCD. So far iI managed to connect the Serverserver with the arduinoArduino with the help of the serial library (https://github.com/voodootikigod/node-serialportserial library). The arduinoArduino returns all Datadata sent to it via Serial and prints it bytewise to the LCD.

These are the Problemsproblems:

  1. Node.js always opens the port, but is only able to send and receive data when the Serial Monitor from the arduinoArduino IDE is opened.

  2. The Data Node.js is sending seems to have a special format or gets transformed during the Process. If iI send a simple intint like 2, the display shows a y and an undecipherable character, and the data returned looks like this: 49 50 49 13 10 48 13 10. I noticed that the amount of 13 corresponds with the int iint I send (3 send = three 13's come back). When iI enter data into Arduinos own Serial Monitor, it is displayed correctly and returned as ASCII (because I am saving the Serial.read()return value of Serial.read() as an IntInt).

HeresHere's my Serverserver (the relevant parts):

And my Sketchsketch:

I ran into some strange Problems while trying to send Data from my Node.js Server to my Arduino. My Goal is to transfer an Int to the Arduino Nano and display it on a LCD. So far i managed to connect the Server with the arduino with the help of the serial library (https://github.com/voodootikigod/node-serialport). The arduino returns all Data sent to it via Serial and prints it bytewise to the LCD.

These are the Problems:

  1. Node.js always opens the port, but is only able to send and receive data when the Serial Monitor from the arduino IDE is opened.

  2. The Data Node.js is sending seems to have a special format or gets transformed during the Process. If i send a simple int like 2, the display shows a y and an undecipherable character, and the data returned looks like this: 49 50 49 13 10 48 13 10. I noticed that the amount of 13 corresponds with the int i send (3 send = three 13's come back). When i enter data into Arduinos own Serial Monitor, it is displayed correctly and returned as ASCII (because I am saving the Serial.read() as an Int).

Heres my Server (the relevant parts):

And my Sketch:

I ran into some strange problems while trying to send data from my Node.js Server to my Arduino. My goal is to transfer an Int to the Arduino Nano and display it on a LCD. So far I managed to connect the server with the Arduino with the help of the serial library. The Arduino returns all data sent to it via Serial and prints it bytewise to the LCD.

These are the problems:

  1. Node.js always opens the port, but is only able to send and receive data when the Serial Monitor from the Arduino IDE is opened.

  2. The Data Node.js is sending seems to have a special format or gets transformed during the Process. If I send a simple int like 2, the display shows a y and an undecipherable character, and the data returned looks like this: 49 50 49 13 10 48 13 10. I noticed that the amount of 13 corresponds with the int I send (3 send = three 13's come back). When I enter data into Arduinos own Serial Monitor, it is displayed correctly and returned as ASCII (because I am saving the return value of Serial.read() as an Int).

Here's my server (the relevant parts):

And my sketch:

Source Link

Serial Communication to Arduino with Node.js

I ran into some strange Problems while trying to send Data from my Node.js Server to my Arduino. My Goal is to transfer an Int to the Arduino Nano and display it on a LCD. So far i managed to connect the Server with the arduino with the help of the serial library (https://github.com/voodootikigod/node-serialport). The arduino returns all Data sent to it via Serial and prints it bytewise to the LCD.

These are the Problems:

  1. Node.js always opens the port, but is only able to send and receive data when the Serial Monitor from the arduino IDE is opened.

  2. The Data Node.js is sending seems to have a special format or gets transformed during the Process. If i send a simple int like 2, the display shows a y and an undecipherable character, and the data returned looks like this: 49 50 49 13 10 48 13 10. I noticed that the amount of 13 corresponds with the int i send (3 send = three 13's come back). When i enter data into Arduinos own Serial Monitor, it is displayed correctly and returned as ASCII (because I am saving the Serial.read() as an Int).

Heres my Server (the relevant parts):

http = require('http');
fs = require('fs');
serialport = require('serialport');
 
port = 3000;
host = '127.0.0.1';
SerialPort = serialport.SerialPort;
arduinoPort = '/dev/ttyUSB2';

var printLog = true;
var playerRoundKills = 0;
var playerTotalKills;
var roundLive = false;

//open port
var myPort = new SerialPort(arduinoPort);
parser: serialport.parsers.raw          //rohdatenparser definieren
serialport.on('open', function(){
  console.log('Serial Port Opend');
  serialport.on('data', function(data){
      console.log(data[0]);
  });
});

//implementation of abstract port functions
myPort.on('open', showPortOpen);
myPort.on('data', sendSerialData);
myPort.on('close', showPortClose);
myPort.on('error', showError);

function showPortOpen() {
   console.log('port open. Data rate: ' + myPort.options.baudRate);
}
 
//output incoming data
function sendSerialData(data) {
   console.log(data[0]);
if(typeof data[1] !== 'undefined') console.log(data[1]);
}
 
function showPortClose() {
   console.log('port closed.');
}
 
function showError(error) {
   console.log('Serial port error: ' + error);
}

myPort.on("open", function () {
  console.log('open');
//send data (3) to arduino
myPort.write(3, function(err, results) {
    if(err != null)console.log('Fehler beim ersten senden: ' + err);
    if(results != null)console.log("Killcount erstmals gesendet: "+results);
  });
});

And my Sketch:

#include <LiquidCrystal.h>

//LDCD Display
LiquidCrystal lcd(9, 8, 6, 5, 4, 3);  //Digitale Pins an dem das Display angeschlossen ist
#define LCD_WIDTH 16                    //Anzahl Spalten des Display (16)
#define LCD_HEIGHT 2                    //Anzahl Zeilen des Display (2)
 

 
#define DEBUG_MODE true        
 
void setup(void)
{
  Serial.begin(9600);  
  byte i;
 
  lcd.begin(LCD_WIDTH, LCD_HEIGHT,1);
 
  lcd.setCursor(0,0);
 
}

void loop() {
  // when characters arrive over the serial port...
  if (Serial.available()) {
    
    //clear display for new killcount
    //lcd.clear();
    // wait a bit for the entire message to arrive
    delay(100);
    // read all the available characters
    while (Serial.available() > 0) {
      // write each character to display as ascii
    int temp = Serial.read();
    lcd.write(temp);
    Serial.println(temp);
    
    } 
    //Serial.flush();
  }
}

I'd appreciate any suggestions! =)