4 Replies Latest reply on Nov 16, 2016 7:27 PM by aroberts394

    serialport javascript Intel edison

    jarain78

      Hi all, I would like to know how I can write data using serial port on Intel Edison. Now I can read information but I don't how to write it. I'm using this sertial port library ( https://www.npmjs.com/package/serialport ), this is my code:


      var portName = "/dev/ttyMFD1";
      
      
      var mraa = require('mraa'); //require mraa
      console.log('MRAA Version: ' + mraa.getVersion()); //write the mraa version to the console
      var analogPin0 = new mraa.Aio(0); //setup access analog input Analog pin #0 (A0)
      
      
      SerialPort = require("serialport").SerialPort
      
      
      
      
      serialPort = new SerialPort(portName, {
        baudrate: 57600,
        // defaults for Arduino serial communication
        dataBits: 8, 
        parity: 'none', 
        stopBits: 1, 
        flowControl: false 
      });
      
      
      serialPort.on('open', showPortOpen);
      serialPort.on('write', writeData);
      
      
      //serialPort.on('data', saveLatestData);
      
      
      serialPort.on('close', showPortClose);
      
      
      
      
      function readECG(){
        var analogValue = analogPin0.read();
        //setTimeout(readECG,100);
      }
      
      
      function writeData(){
        serialPort.write("0");
      }
      
      
      function showPortOpen() {
         console.log('port open. Data rate: ' + serialPort.options.baudRate);
      }
      function showPortClose() {
         console.log('port closed.');
      }
      function saveLatestData(data) {
          readdata = data.toString();
          console.log(readdata);
      }
      

       

      Thanks

       

      jarain78

        • 1. Re: serialport javascript Intel edison
          Intel_Peter

          Hello jarain78,

           

          I tried your code and just like you, I was not able to write anything in the serial port, let me run some extra tests to see if I can find out why.

           

          Peter.

          • 2. Re: serialport javascript Intel edison
            jarain78

            Hi I have solved my problem, this is the thread where I found the solution How to enable PIN0/PIN1 for UART usage (TX/RX). And this is the script that use to activate UART (PIN0/PIN1).

            echo 214 > /sys/class/gpio/export 2>&1

            echo high > /sys/class/gpio/gpio214/direction

            echo low > /sys/class/gpio/gpio214/direction

             

            echo 131 > /sys/class/gpio/export 2>&1

            echo mode1 > /sys/kernel/debug/gpio_debug/gpio131/current_pinmux

            echo 249 > /sys/class/gpio/export 2>&1

            echo high > /sys/class/gpio/gpio249/direction

            echo 1 > /sys/class/gpio/gpio249/value

            echo 217 > /sys/class/gpio/export 2>&1

            echo high > /sys/class/gpio/gpio217/direction

            echo 1 > /sys/class/gpio/gpio217/value

            echo out > /sys/class/gpio/gpio131/direction

            echo 0 > /sys/class/gpio/gpio131/value

             

             

            echo 130 > /sys/class/gpio/export 2>&1

            echo mode1 > /sys/kernel/debug/gpio_debug/gpio130/current_pinmux

            echo 248 > /sys/class/gpio/export 2>&1

            echo low > /sys/class/gpio/gpio248/direction

            echo 0 > /sys/class/gpio/gpio248/value

            echo 216 > /sys/class/gpio/export 2>&1

            echo in > /sys/class/gpio/gpio216/direction

            echo in > /sys/class/gpio/gpio130/direction

             

            echo high > /sys/class/gpio/gpio214/direction

             

            And this is my example code:

             

            var mraa = require('mraa'); //require mraa
            console.log('MRAA Version: ' + mraa.getVersion()); //write the mraa version to the console
            
            
            var analogPin0 = new mraa.Aio(0); //setup access analog input Analog pin #0 (A0)
            
            
            // Config serial communication to send information LCD 
            var SerialPort = require("serialport").SerialPort;  
            var port = "/dev/ttyMFD1";  
            var serialPort = new SerialPort(port, {  
              baudrate: 115200  
            }, false);  
            
            
            var analogValue = 0;
            //----------------------------------------------------------------------
            // Send data 
            console.log("Open port: "+ port);  
            serialPort.open(function (error) {  
              if (error) {  
              console.log('Failed to open: '+error);  
              } else {  
              console.log('open');  
              serialPort.on('data', function(data) {  
              console.log('data received: ' + data); 
              analogValue = analogPin0.read(); //read the value of the analog pin
              console.log(analogValue); //write the value of the analog pin to the console
              });  
            
              //write data to serial port every second  
            
            
              setInterval(function () {  
              serialPort.write(String(analogValue)+ "\r\n", function(err) {  
              if(err) {  
              console.log('err ' + err);  
              }else{  
              console.log('Writing data ');  
              }  
              });  
              }, 10);  
              }  
            });
            
            

             

            Thanks

             

            Jarain78

            • 3. Re: serialport javascript Intel edison
              Intel_Peter

              Hello jarain78,

               

              I'm glad you managed to get your project running, if you have any other doubts, don't hesitate to come back to the community.

               

              Peter.

              • 4. Re: serialport javascript Intel edison
                aroberts394

                Hey! I'm a novice in javascript.

                I tried running this code to just read analog data over serial port but I get this error:

                ERROR: TypeError: Object #<Object> has no method 'call'

                  at SerialPort._error (/home/root/node_modules/serialport/lib/serialport.js:148:14)

                  at SerialPort.open (/home/root/node_modules/serialport/lib/serialport.js:160:17)

                  at process._tickCallback (node.js:448:13)

                  at Function.Module.runMain (module.js:499:11)

                  at startup (node.js:119:16)

                  at node.js:935:3