Not connected to anything.
#+END_SRC
On the server side:
#+BEGIN_SRC javascript
//
// ir-serial.js
// by Dylan Holmes
//
// based on hello-serial.js
// requires nodejs, npm, serial, express@4.15.2, ws, express-ws
// BASIC PARAMETERS
const server_port = 8081
const client_address = '127.0.0.1'
const serial_port = "/dev/ttyUSB0"
var baud = 9600 // 19200 // 9600// 115200
example_terminal = function(as_text = true) {
// Create a terminal for speaking with a pcb board over serial.
// Performs the same function as term.py (from class) or picocom.
// Set up serial connection
const SerialPort = require('serialport')
const Readline = require('@serialport/parser-readline')
const port = new SerialPort(serial_port,
{baudRate: baud,
databits:8,
dtr: true,
})
// Serial-opening errors will be emitted as an error event
port.on('error', function(err) {
console.log('Error: ', err.message)
})
// Whenever serial data is received, interpret the data as
// keycodes and print as a string.
// See: https://thisdavej.com/making-interactive-node-js-console-apps-that-listen-for-keypress-events/
port.on('data', function(data) {
console.log(as_text ? data.toString('utf8') : data)
})
// Listen to keyboard in nodejs terminal
const readline = require('readline')
readline.emitKeypressEvents(process.stdin)
process.stdin.setRawMode(true)
console.log("Ready for input: ")
process.stdin.on('keypress', (str, key) => {
// Ctrl-c exits the terminal
if(key.ctrl && key.name === 'c') process.exit()
var debug = false
if(debug) console.log("You pressed the '",str,"' key.", key)
port.write(key.sequence)
})
}
infrared_camera_terminal = function() {
// Speak with a PCB board that is speaking with an infrared
// positioning camera. (part id: 1738-1250-ND)
// Set up serial connection
const SerialPort = require('serialport')
const Readline = require('@serialport/parser-readline')
const port = new SerialPort(serial_port,
{baudRate: baud,
databits:8,
dtr: true,
})
var stream = []
var pos_x = new Array(4)
var pos_y = new Array(4)
var upper_bits
var index = null
// Serial-opening errors will be emitted as an error event
port.on('error', function(err) {
console.log('Error: ', err.message)
})
// Whenever serial data is received, interpret the data as
// the special format of the infrared positional camera.
var process_ir_data = function() {
while( stream.length >= 16 ) {
stream.shift()
// Look for framing data
if( stream[0] == 1 &&
stream[1] == 2 &&
stream[2] == 3 &&
stream[3] == 4 ) {
pos_x[0] = stream[4]
pos_y[0] = stream[5]
upper_bits = stream[6]
pos_x[0] += (upper_bits & 0b00110000) << 4;
pos_y[0] += (upper_bits & 0b11000000) << 2;
pos_x[1] = stream[7]
pos_y[1] = stream[8]
upper_bits = stream[9]
pos_x[1] += (upper_bits & 0b00110000) << 4;
pos_y[1] += (upper_bits & 0b11000000) << 2;
pos_x[2] = stream[10]
pos_y[2] = stream[11]
upper_bits = stream[12]
pos_x[2] += (upper_bits & 0b00110000) << 4;
pos_y[2] += (upper_bits & 0b11000000) << 2;
pos_x[3] = stream[13]
pos_y[3] = stream[14]
upper_bits = stream[15]
pos_x[3] += (upper_bits & 0b00110000) << 4;
pos_y[3] += (upper_bits & 0b11000000) << 2;
console.log(stream[4], stream[5], stream[6])
console.log("x: ",pos_x[0],",\ty: ",pos_y[0])
// console.log("x: ",pos_x[1],",\ty: ",pos_y[1])
stream = stream.slice(16)
return;
}
}
}
port.on('data', function(data) {
stream.push(...data) // apparently js has an ... operator.
process_ir_data()
console.log(data)
})
// Listen to keyboard in nodejs terminal
const readline = require('readline')
readline.emitKeypressEvents(process.stdin)
process.stdin.setRawMode(true)
console.log("Ready for input: ")
process.stdin.on('keypress', (str, key) => {
// Ctrl-c exits the terminal
if(key.ctrl && key.name === 'c') process.exit()
var debug = false
if(debug) console.log("You pressed the '",str,"' key.", key)
port.write(key.sequence)
})
}
// example_terminal(false)
// // infrared_camera_terminal()
// cf example_serial_websocket_server from hello-serial.js
fake_websocket_server = function() {
// Send synthetic (artifically-generated) serial data to the
// websockets to confirm that it works.
// Used in connection with dxh.plot.infrared.html.
// Set up the web socket server
const express = require('express')
const app = express()
require('express-ws')(app);
// Connections maintain a list of who has connected to the
// websocket server, so we know who needs to hear serial messages.
var connections = []
app.get('/', (req, res) => res.send('(The infrared-sockets-serial server is running.)'))
app.listen(server_port, () => console.log(`Infrared Sockets-serial. Listening on port :${server_port}`))
var simulate_data
app.ws('/ftdi', function(ws, req) {
// sockets <-- serial
simulate_data = function(data) {
console.log("artificial data:", data)
// translate into camera format
data = data[0]
var upper_bits = 0
upper_bits |= (data[0] & 0b1100000000) >> 4
upper_bits |= (data[1] & 0b1100000000) >> 2
upper_bits |= 0b1111
data[0] &= 0xff
data[1] &= 0xff
data.push(upper_bits)
data.unshift(0)
for(var i=0;i<6;i++) {
data.push(0xff)
}
var message = {}
message.data = data
console.log(message)
// end translate into camera format
ws.send(JSON.stringify(data))
}
var t = 0
setInterval(function(){
t+=0.1;
var pt = [400+100*Math.cos(t), 100+50*Math.sin(t)]
pt[0] = Math.floor(pt[0])
pt[1] = Math.floor(pt[1])
simulate_data([pt])
}, 40)
// sockets --> serial
ws.on('message', function(msg) {
console.log("html page sent:", msg)
});
});
}
infrared_serial_websocket_server = function() {
// Set up the web socket server
const express = require('express')
const app = express()
require('express-ws')(app);
// Connections maintain a list of who has connected to the
// websocket server, so we know who needs to hear serial messages.
var connections = []
app.get('/', (req, res) => res.send('(The infrared-sockets-serial server is running.)'))
app.listen(server_port, () => console.log(`Infrared Sockets-serial. Listening on port :${server_port}`))
// Set up serial connection
const SerialPort = require('serialport')
const Readline = require('@serialport/parser-readline')
const port = new SerialPort(serial_port,
{baudRate: baud,
databits:8,
dtr: true,
})
port.on('error', function(err) {
console.log('Error: ', err.message)
})
// Enable communication between sockets and serial. This is a
// little messy (nested), because we have to do the sockets <--
// serial connection in a variable scope where ws is defined.
app.ws('/ftdi', function(ws, req) {
// sockets <-- serial
port.on('data', function(data) {
// console.log("serial connection sent:", data.toString('utf8'))
//ws.send(data.toString('utf8'))
ws.send(JSON.stringify(data))
})
// sockets --> serial
ws.on('message', function(msg) {
console.log("html page sent:", msg)
port.flush()
port.write(Buffer.from(msg))
port.flush()
});
});
}
//example_terminal(false);
infrared_serial_websocket_server()
// fake_websocket_server()
#+END_SRC
#+HTML: