|
| 1 | +import { SerialPort } from "serialport"; |
| 2 | +import { NodeSerialConnection } from "./dist/index.js"; |
| 3 | + |
| 4 | +const Connect = async () => { |
| 5 | + const connection = new NodeSerialConnection(); |
| 6 | + // COM4 is the port that works for me, you'll have to get your path from SerialPort.list() |
| 7 | + await connection.connect({ |
| 8 | + portPath: "COM4", |
| 9 | + concurrentLogOutput: false, |
| 10 | + }); |
| 11 | + console.log(await SerialPort.list()); |
| 12 | + connection.events.onMessagePacket.subscribe((packet) => { |
| 13 | + onMessage(packet.from, packet.data); |
| 14 | + }); |
| 15 | + |
| 16 | + connection.events.onPrivatePacket.subscribe((packet) => { |
| 17 | + onMessage(packet.from, packet.data); |
| 18 | + }); |
| 19 | + |
| 20 | + connection.events.onLogEvent.subscribe((packet) => { |
| 21 | + console.log("LogEvent: ", packet); |
| 22 | + }); |
| 23 | + |
| 24 | + connection.events.onDeviceMetadataPacket.subscribe((packet) => { |
| 25 | + console.log("DeviceMetadataPacket: ", packet); |
| 26 | + }); |
| 27 | + |
| 28 | + connection.events.onDeviceDebugLog.subscribe((packet) => { |
| 29 | + console.log("DeviceDebugLog: ", packet); |
| 30 | + }); |
| 31 | + |
| 32 | + connection.events.onFromRadio.subscribe((packet) => { |
| 33 | + console.log("FromRadio: ", packet); |
| 34 | + }); |
| 35 | + |
| 36 | + connection.events.onDeviceStatus.subscribe((packet) => { |
| 37 | + console.log("DeviceStatus: ", packet); |
| 38 | + }); |
| 39 | + |
| 40 | + connection.events.onMyNodeInfo.subscribe((packet) => { |
| 41 | + console.log("NodeInfo: ", packet); |
| 42 | + }); |
| 43 | + const onMessage = (sender, message) => { |
| 44 | + console.log(`Message from: ${sender}`); |
| 45 | + console.log(`Message was: ${message}`); |
| 46 | + }; |
| 47 | + |
| 48 | + connection.events.onRemoteHardwarePacket.subscribe((packet) => { |
| 49 | + console.log("Remote Hardware Packet: ", packet); |
| 50 | + }); |
| 51 | + |
| 52 | + connection.events.onRoutingPacket.subscribe((packet) => { |
| 53 | + console.log("Routing packet: ", packet); |
| 54 | + }); |
| 55 | + |
| 56 | + connection.events.onConfigPacket.subscribe((packet) => { |
| 57 | + console.log("Config: ", packet); |
| 58 | + }); |
| 59 | + |
| 60 | + // Request configuration data from device (I think this will help trigger other serial events being processed) |
| 61 | + await connection.configure(); |
| 62 | +}; |
| 63 | + |
| 64 | +Connect().catch((err) => { |
| 65 | + console.log(err); |
| 66 | +}); |
0 commit comments