Skip to content

Instantly share code, notes, and snippets.

@rezonn
Last active February 12, 2022 06:56
Show Gist options
  • Save rezonn/f12ce1e16f95deddfd8e50b90ac2519f to your computer and use it in GitHub Desktop.
Save rezonn/f12ce1e16f95deddfd8e50b90ac2519f to your computer and use it in GitHub Desktop.
const SerialPort = require('serialport');
const port = new SerialPort('/dev/cu.usbserial-1420', {baudRate: 115200})
// port.write('G0 Z10\n', function(err) {
port.write('M114\n', function(err) {
if (err) {
return console.log('Error on write: ', err.message)
}
//console.log('message written')
})
// Open errors will be emitted as an error event
port.on('error', function(err) {
console.log('Error: ', err.message)
})
// Read data that is available but keep the stream in "paused mode"
port.on('readable', function () {
port.read()
})
// Switches the port into "flowing mode"
port.on('data', function (data) {
console.log('Data:', data.toString())
})
// Pipe the data into another stream (like a parser or standard out)
const Readline = require('@serialport/parser-readline')
const lineStream = port.pipe(new Readline())
import serial
serialPort = serial.Serial('/dev/cu.usbserial-1420', 115200, exclusive = True)
line = "G0 X10" #go to x=10
line = "M114" #go home
line = line + "\n"
serialPort.write(line.encode())
lines = serialPort.read(serialPort.in_waiting).decode().splitlines(False)
print(lines)
xcode-select --install
npm install serialport
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment