Skip to content

Instantly share code, notes, and snippets.

@robbles
Last active December 20, 2015 00:29
Show Gist options
  • Save robbles/6042075 to your computer and use it in GitHub Desktop.
Save robbles/6042075 to your computer and use it in GitHub Desktop.
#!/bin/bash
socat -d -d pty,raw,echo=0,link=/tmp/ttysA1 pty,raw,echo=0,link=/tmp/ttysB1 &
socat -d -d pty,raw,echo=0,link=/tmp/ttysA2 pty,raw,echo=0,link=/tmp/ttysB2 &
socat -d -d pty,raw,echo=0,link=/tmp/ttysA3 pty,raw,echo=0,link=/tmp/ttysB3 &
socat -d -d pty,raw,echo=0,link=/tmp/ttysA4 pty,raw,echo=0,link=/tmp/ttysB4 &
socat -d -d pty,raw,echo=0,link=/tmp/ttysA5 pty,raw,echo=0,link=/tmp/ttysB5 &
var SerialPort = require('serialport').SerialPort;
function datalistener() {
console.log('data received:');
console.log(arguments);
}
// Use create_virtual_ports.sh to create these serial ports first
var sp2 = new SerialPort("/tmp/ttysA1", {});
sp2.on("data", datalistener);
var sp3 = new SerialPort("/tmp/ttysA2", {});
sp3.on("data", datalistener);
var sp4 = new SerialPort("/tmp/ttysA3", {});
sp4.on("data", datalistener);
var sp5 = new SerialPort("/tmp/ttysA4", {});
sp5.on("data", datalistener);
var sp5 = new SerialPort("/tmp/ttysA5", {});
sp5.on("data", datalistener);
import serial
import select
paths = [ '/tmp/ttysA1', '/tmp/ttysA2', '/tmp/ttysA3', '/tmp/ttysA4', '/tmp/ttysA5' ]
fp_map = {}
fps = []
for path in paths:
port = serial.Serial(path)
fp = port.fileno()
fp_map[fp] = port
fps.append(fp)
while True:
input_ready, _, _ = select.select(fps, [], [])
for fp in input_ready:
port = fp_map[fp]
data = port.read()
print 'Received from %s: %s' % (port.port, data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment