IC-7410の制御をhttpで(2)

VFO周波数は、リモートホストによって1秒間に数回読み出され、ブラウザにプッシュされます。

remote host % node index.js

index.js

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var port = process.env.PORT || 3000;
var SerialPort = require('serialport');
var serial = new SerialPort('/dev/ttyUSB0',{
    baudrate:19200
});

const bufa = new Buffer('fefe80e017'          , 'hex'); // preamble
const bufz = new Buffer('fd'                  , 'hex'); // postamble
const buf1 = new Buffer('fefe80e0174351fd'    , 'hex'); // CQ
const buf2 = new Buffer('fefe80e01751525a3ffd', 'hex'); // QRZ?
const buf3 = new Buffer('fefe80e003fd'        , 'hex'); // read freq

http.listen(3000,function(){
    console.log('now listening to the port 3000..');
});

app.get('/', function(req, res){
  res.sendFile(__dirname + '/index.html');
});

serial.on('open',function(){
    console.log('serial port /dev/ttyUSB0 is opened.');
    serial.write(buf3);
});

serial.on('data',function(data){
    console.log('received data: ', data);
    if (data[3] == 0x80 &data[4] == 0x03) {
        console.log('received data: ', data[5], data[6], data[7], data[8]);
	var f10   = data[5]>>4 & 0x0f;
	var f1    = data[5]    & 0x0f;
	var f1k   = data[6]>>4 & 0x0f;
	var f100  = data[6]    & 0x0f;
	var f100k = data[7]>>4 & 0x0f;
	var f10k  = data[7]    & 0x0f;
	var f10m  = data[8]>>4 & 0x0f;
	var f1m   = data[8]    & 0x0f;
	var freq  = f10m.toString()+f1m.toString()+","+f100k.toString()+f10k.toString()+f1k.toString()+"."+f100.toString()+f10.toString()+f1+" kHz";
	console.log('freq: ' + freq);
        io.emit('recvmsg', 'VFO A: ' + freq);
    }
});

serial.on('error', function(err) {
  console.log('Error: ', err.message);
})

io.on('connection', function(socket){
    socket.on('message1', function(){
    serial.write(buf1);
  });
    socket.on('message2', function(){
    serial.write(buf2);
  });
  socket.on('your message', function(msg){
    serial.write(bufa);
    serial.write(msg);
    serial.write(bufz);
    io.emit('your message', msg);
  });
});

function sendTime() {
    serial.write(buf3);
}
setInterval(sendTime, 200);

index.html

<!doctype html>
<html>
  <head>
    <title>IC-7410 Rig Control</title>
    <style>
      * { margin: 0; padding: 0; box-sizing: border-box; }
      body { font: 13px Helvetica, Arial; }
      form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
      form input { border: 0; padding: 10px; width: 80%; margin-right: .5%; }
      form button { width: 15%; background: rgb(130, 224, 255); border: none; padding: 10px; }
      #messages { list-style-type: none; margin: 0; padding: 0; }
      #messages li { padding: 5px 10px; }
      #messages li:nth-child(odd) { background: #eee; }
      #messages { margin-bottom: 40px }
      #messages { font-size: 2em }
      #frequency { position: fixed; top: 10px; left: 200px; font-size: 2em }
    </style>
  </head>
  <body>
    <button id="btn1"><font size="5">CQ</font></button>
    <button id="btn2"><font size="5">QRZ?</font></button>
    <div id="frequency"></div>
    <div id="messages"></div>

    <form action="">
      <input id="m" autocomplete="off" /><button>Send</button>
    </form>

    <script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
    <script src="https://code.jquery.com/jquery-1.11.1.js"></script>

    <script>
        var socket = io();
        $('#btn1').click(function(){
            socket.emit('message1');
        });
        $('#btn2').click(function(){
            socket.emit('message2');
        });
        socket.on('recvmsg',function(data){
            $('h1').text(data);
        });
    </script>

    <script>
      $(function () {
        var socket = io();
        $('form').submit(function(){
          socket.emit('your message', $('#m').val());
          $('#m').val('');
          return false;
        });
        socket.on('your message', function(msg){
          $('#messages').append($('<div>').text(msg));
          window.scrollTo(0, document.body.scrollHeight);
        });
        socket.on('recvmsg', function(msg){
	  document.getElementById("frequency").innerHTML = msg;
        });
      });
    </script>
  </body>
</html>