2017年6月22日 星期四

pi3 nodejs UART socket IO

http://samselectronicsprojects.blogspot.tw/2012/11/nodejs-raspberry-pi-serial.html

serialport = require("serialport").SerialPort;

/**
 * Serial Port Setup.
 */

var portName = '/dev/ttyAMA0'; //This is the standard Raspberry Pi Serial port
var readData = ''; //Array to hold the values read in from the port

var sp = new serialport(portName, {
  baudRate: 9600,
  dataBits: 8,
  parity: 'none',
  stopBits: 1,
  flowControl: false
});

/**
 * Server Code
 */

var server = http.createServer(app).listen(app.get('port'), function(){
  console.log("Express server listening on port " + app.get('port'));
});

var io = require('socket.io').listen(server);

io.sockets.on('connection', function(socket){
console.log("Socket connected"); 
socket.emit('connected', 123);
  });
  
  /*
 * GET home page.
 */
var fs = require('fs')

exports.index = function(req, res){
  res.writeHead(200, {'Content-Type': 'text/html'});
  var contents = fs.readFileSync("./public/index.html", "UTF-8");
  res.end(contents);
//res.render('index');

}

var socket = io.connect('http://your ip address here..');

    socket.on('connected', function(data) {
      console.log('Connected, this is what I received : ', data);
      //you could then emit something here for the server to receive like socket.emit("blah", "helloworld") -hello world is a string of data you are passing the sever here..
    });

socket.on('sendSerial', function(data) {
console.log("Client sent us: " + data + "to send to serial");
    sp.write(mbBuffer, function (err, bytesWritten) {
    console.log('bytes written:', bytesWritten);
}); 

sp.on('data', function (data) {
    console.log(data);
    data = data.toString();
    socket.emit('receiveSerial', data);
}); 

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...');
});

//============================================ 
//APP.JS

/**
 * Module dependencies.
 */

var express = require('express')
  , routes = require('./routes')
  , user = require('./routes/user')
  , http = require('http')
  , path = require('path')
  , serialport = require("serialport").SerialPort;

/**
 * Serial Port Setup.
 */

var portName = '/dev/ttyAMA0'; //This is the standard Raspberry Pi Serial port

var readData = ''; //Array to hold the values read in from the port

var sp = new serialport(portName, {
  baudRate: 9600,
  dataBits: 8,
  parity: 'none',
  stopBits: 1,
  flowControl: false
});

/**

 * Express Setup.
 */

var app = express();

app.configure(function(){
  app.set('port', process.env.PORT || 80);
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.favicon());
  app.use(express.logger('dev'));
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(app.router);
  app.use(express.static(path.join(__dirname, 'public')));
});

app.configure('development', function(){
  app.use(express.errorHandler());
});

app.get('/', routes.index);
app.get('/users', user.list);

/**
 * Actual Server Code
 */

var server = http.createServer(app).listen(app.get('port'), function(){
  console.log("Express server listening on port " + app.get('port'));
});

var io = require('socket.io').listen(server);

  /**
 * Server and socket started, below are all my listeners and emitters
 */

io.sockets.on('connection', function(socket){
  console.log("Socket connected"); 
  socket.emit('connected', 123);

  socket.on('sendSerial', function(data) {
    console.log("Client sent us: " + data + "to send to serial");
    sp.write(mbBuffer, function (err, bytesWritten) {
      console.log('bytes written:', bytesWritten);
    });
  });

  sp.on('data', function (data) {
    console.log(data);
    var mbRec = new Buffer(data, 'utf')
    console.log(mbRec);
    mbRec = mbRec.toString('hex');
    console.log(mbRec);
    socket.emit('serialEvent', mbRec);
  });

  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...');
  });
});

//==============================================
/*
 * GET home page.
 */
var fs = require('fs')

exports.index = function(req, res){
  res.writeHead(200, {'Content-Type': 'text/html'});
  var contents = fs.readFileSync("./public/index.html", "UTF-8");
  res.end(contents);
//  res.render('index');
};

沒有留言:

張貼留言