Skip to content

Instantly share code, notes, and snippets.

@phstc
Last active June 27, 2023 18:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phstc/63068dc38873e815086f95ae170db887 to your computer and use it in GitHub Desktop.
Save phstc/63068dc38873e815086f95ae170db887 to your computer and use it in GitHub Desktop.
const { SerialPort } = require('serialport')
async function listSerialPorts() {
await SerialPort.list().then((ports, err) => {
if (err) {
// document.getElementById('error').textContent = err.message
console.error(err.message)
return
} else {
// document.getElementById('error').textContent = ''
}
console.log('ports', ports)
if (ports.length === 0) {
// document.getElementById('error').textContent = 'No ports discovered'
console.error('No ports discovered')
}
// tableHTML = tableify(ports)
// document.getElementById('ports').innerHTML = tableHTML
})
}
function listPorts() {
listSerialPorts()
setTimeout(listPorts, 2000)
}
// Set a timeout that will check for new serialPorts every 2 seconds.
// This timeout reschedules itself.
setTimeout(listPorts, 2000)
listSerialPorts()
{
"name": "turnstile-app",
"version": "1.0.0",
"description": "",
"main": "test.js",
"scripts": {
"test": "node test.js",
"list": "node list.js"
},
"author": "",
"license": "ISC"
}
const { SerialPort } = require('serialport')
async function listSerialPorts() {
await SerialPort.list().then((ports, err) => {
if (err) {
// document.getElementById('error').textContent = err.message
console.error(err.message)
return
} else {
// document.getElementById('error').textContent = ''
}
console.log('ports', ports)
if (ports.length === 0) {
// document.getElementById('error').textContent = 'No ports discovered'
console.error('No ports discovered')
}
// tableHTML = tableify(ports)
// document.getElementById('ports').innerHTML = tableHTML
})
}
function listPorts() {
listSerialPorts()
setTimeout(listPorts, 2000)
}
// Set a timeout that will check for new serialPorts every 2 seconds.
// This timeout reschedules itself.
// setTimeout(listPorts, 2000)
// listSerialPorts()
const ON = [0xff, 0x01, 0x01]
const OFF = [0xff, 0x01, 0x00]
const path = '/dev/tty.usbserial-AB0LBJ9Q'
const serialport = new SerialPort({ path, baudRate: 9600 })
serialport.on('open', function() {
console.log(serialport + ' is open')
async function test(param) {
serialport.write(Buffer.from(param))
console.log('changed to', param === ON ? 'ON' : 'OFF')
setTimeout(() => {
test(ON === param ? OFF : ON)
}, 1000)
}
test(ON)
})
async function test(param) {
const x = serialport.write(param)
console.log('changed to', param === ON ? 'ON' : 'OFF')
setTimeout(() => {
test(ON === param ? OFF : ON)
}, 1000)
}
// test(ON)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment