4 Replies Latest reply on Jun 16, 2015 4:35 AM by cliffk

    Galileo Gen 2 RX/TX communication via Node.js

    RafaelBorges

      Hello everyone,

       

      I need to create an 'smart' nodeJS interface with a third party equipment and the Galileo Gen2 board. So I've read a bunch of guides to make Galileo read/write using pins 0 & 1 (RX & TX). Now I know those are all specifically for Galileo Gen 1, I tried to port them to Galileo Gen 2 myself, but I've been beating my head on the wall with this:

       

      First of, the third party equipment is not presenting any issues, if I connect using a serial cable to my Mac, I can both read and write.

       

      Solution 1) I tried using voodootikigod/node-serialport (npm install serialport) on my project, and it seems to open up serial just fine.

      Result-> Problem is that it not always reads what is coming off and not always writes to the equipment, (maybe its just writing / reading too quickly?) I've tried using the baudrate as 9600, 19200 and 115200, results are the same. I don't get any error, just sometimes it works, sometimes it doesn't -> I can only think it has something to do with the code, but maybe not?.

      serial.on("open", function () {
        serial.on('data', function(data) {
          console.log('Data received: ' + data.toString('hex'));
        });
        serial.on('error', function(data) {
          console.log('Error received: ' + data.toString('hex'));
        });
        serial.write([0x00.0x18.0x18]);
      });
      
      
      

       

      Solution 2) Tried using MRAA library.

      Result-> It seems to connect, but it does not write to the serial. To read using this method is very hard, at least that was my first impression, as I need to implement something like the loop function on the Arduino by hand. Not sure if there's an easier way and I'm killing myself

      var mraa = require('mraa');
      var serial = new mraa.Uart(0);
      serial.setBaudRate(19200);
      serial.write('001818'); // tried '\u001818', and other buffer compatible types, nothing works here.
      
      
      

       

      Solution 3) Tried to tweak the GPIO switches based on:

      http://www.emutexlabs.com/project/203-getting-started-with-intel-galileo-gen-2

      http://www.malinov.com/Home/sergey-s-blog/intelgalileo-configuringtheserialportinlinux (as a reference)

      https://communities.intel.com/message/265412#265412 (as a reference)

      Result-> This one like the above never worked, not sure if I'm doing something wrong.

       

      Does anyone manage to make this work on an Galileo Gen 2? (via NodeJS) If you can, please post some samples here.

       

      Any help here will be greatly appreciated!

       

      Thanks in advance!

        • 1. Re: Galileo Gen 2 RX/TX communication via Node.js
          Intel_Peter

          Hello tevin_wang,

           

          Well, as you can see in Intel Galileo using 433 RF modules UART on MRAA is not yet available. As of now, all it does is enable the muxes for you and can return the correct TTY device to use. So using MRAA is not going to help you a lot if you are trying to create a serial communication.

          Now regarding how to set the MUXs on the Gen2. You will find the correct set up in How to program UART serial from Linux on Galileo Gen 2?

          Regarding the voodootikigod/node-serialport library. I've never use it but, did you make sure to install the latest version of it?

          My suggestion would be, if you want to use Node.JS, to use Child Process to make a system call in your Galileo, like the one shown in Snippets | DZones.

          Or you could do it directly from Linux like shown in How to program UART serial from Linux on Galileo Gen 2?

           

          Peter.

          • 2. Re: Galileo Gen 2 RX/TX communication via Node.js
            RafaelBorges

            Hello Peter,

             

            Thank you for your reply. I believe the Child Process will be the way to go. I already used it before, for a much simpler task, but I believe it will be better. Now I have a question I want a listener for reading the serial port say /dev/ttyS0. Meaning, I need to keep reading serial unless I want to write something, but even after writing, I want to continue reading, as its very likely my device will respond to my writing. I know it sounds confusing. So I was looking into the nodeJS documentation and found the following code for an asynchronous process:

             

            https://nodejs.org/api/child_process.html#child_process_asynchronous_process_creation

             

            var spawn = require('child_process').spawn,    
                ps    = spawn('ps', ['ax']),    
                grep  = spawn('grep', ['ssh']);
            
            ps.stdout.on('data', function (data)
            {   
                grep.stdin.write(data); 
            });  
            
            ps.stderr.on('data', function (data)
            {
                console.log('ps stderr: ' + data);
            });  
            
            
            ps.on('close', function (code) 
            {
                if (code !== 0) 
                {
                    console.log('ps process exited with code ' + code);   
                }
                grep.stdin.end(); 
            });
            
            grep.stdout.on('data', function (data)
            {
                console.log('' + data);
            }); 
            
            
            grep.stderr.on('data', function (data) 
            {   
                console.log('grep stderr: ' + data);
            });  
            
            
            grep.on('close', function (code) 
            {
                if (code !== 0) 
                {
                    console.log('grep process exited with code ' + code);
                } 
            });
            

             

            My question is, am I going in the right direction? If so, did someone already implemented something that looks similar to this, but on Serial? Any recommendations are welcome!

            • 3. Re: Galileo Gen 2 RX/TX communication via Node.js
              Intel_Peter

              Let me see if I got it right:

              You want two things, right?

              1. You want your Edison to continuously read the serial port.

              2. You want to write on the serial port whenever you want.

              Please correct me if I got the above wrong. If I did got it right then I just described UART.

              What do I mean by this?

              Well, this exactly what the serial port does. You can create a script that runs on the background that adds all the info received on the Rx pin to a certain file. And at the same time you can create another script that writes on Tx.

              I believe you are on the right track and hopefully finish your project soon. Let me know if there's anything else I can help you with.

               

              Peter.

              • 4. Re: Galileo Gen 2 RX/TX communication via Node.js
                cliffk

                Hi Rafael,

                 

                I have just read your problem regarding node serial port. I was using the GEN1 and in the end could not get node serial port to work correctly and do not think it supports it, perhaps this issue has also progressed to the the GEN2 ???, I can share with you a solution and hope this helps . You are on the right track with using child process. I needed to read and write to rx and tx to send wireless data using a node.js web-server . The solution I found was to use plink. If you go to this address  Using the command-line connection tool Plink you will find information, you will need to download it and compile it with makefile. After you compile it should then be placed in your working directory on the Galileo.

                I am also using socket.io and express but I am sure you will be able to see the idea.

                 

                var express =require('express');

                var app = express();

                var http = require('http').Server(app);

                var io = require('socket.io')(http);

                 

                //spawn plink process

                 

                var spawn = require('child_process').spawn,

                    pl    = spawn('./plink', ['-serial', '/dev/ttyS0', '-sercfg', '9600,8,n,1,N']); // plink parameters, force open ports

                  

                pl.on('close', function (code) {

                 

                if (code !==0) {

                    console.log('pl process exited with code ' + code);

                  }

                });

                 

                pl.stdout.on('data', function (data) {

                console.log('data received: ' + data);

                 

                });

                 

                pl.stderr.on('data', function (data) {

                  console.log('stderr: ' + data);

                });

                 

                 

                 

                 

                 

                // open socket io///

                 

                io.on('connection', function(socket){

                 

                socket.on('message', function(msg){

                    pl.stdin.write(msg + '\r\n');  // send msg to plink, write to port

                        console.log('msg= ' + msg);

                  });

                 

                 

                //Read incoming  data from ports

                 

                       pl.stdout.on('data', function (data) {

                        console.log('data=' + data);

                        socket.emit('data',data+'\r\n'); //emit to browser

                   });

                });

                 

                 

                //END SOCKET.IO//

                 

                 

                http.listen(3000, function(){

                  console.log('listening on 169.254.7.121:3000');

                });

                 

                When the plink process is spawned the ports will remain open for reading and writing. To initialize the ports run as .sh file (for GEN1,im not sure with GEN2),

                 

                #!/bin/bash

                 

                # Initialize sysfs to use the uart on pins 0 and 1 as /dev/ttyS0:

                echo -n "4" > /sys/class/gpio/export

                echo -n "40" > /sys/class/gpio/export

                echo -n "41" > /sys/class/gpio/export

                echo -n "out" > /sys/class/gpio/gpio4/direction

                echo -n "out" > /sys/class/gpio/gpio40/direction

                echo -n "out" > /sys/class/gpio/gpio41/direction

                echo -n "strong" > /sys/class/gpio/gpio40/drive

                echo -n "strong" > /sys/class/gpio/gpio41/drive

                echo -n "1" > /sys/class/gpio/gpio4/value

                echo -n "0" > /sys/class/gpio/gpio40/value

                echo -n "0" > /sys/class/gpio/gpio41/value

                stty -F /dev/ttyS0 raw

                stty -F /dev/ttyS0 9600

                 

                If you need me to send or email you these files let me know, compiling the plink file can me tricky.

                 

                Hope this helps ,

                 

                Good Luck!!!!

                 

                CLiff

                1 of 1 people found this helpful