-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #475 from brown-ccv/fix-serialport
fix: Add serialport code as its own file again
- Loading branch information
Showing
3 changed files
with
66 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import SerialPort from "serialport"; | ||
|
||
// TODO @brown-ccv #460: Test connections with MockBindings (e.g. CONTINUE_ANYWAY) https://serialport.io/docs/api-binding-mock | ||
|
||
/** | ||
* Retrieve's a serial port device based on either the COM name or product identifier | ||
* If productID is undefined then comVendorName is the COM name, otherwise it's the vendorID | ||
* @param {Array} portList A list of available serial port devices | ||
* @param {string} comVendorName EITHER a com name or the vendor identifier of the desired device | ||
* @param {string | undefined} productId The product identifier of the desired device | ||
* @returns The SerialPort device | ||
*/ | ||
function getDevice(portList, comVendorName, productId) { | ||
if (productId === undefined) { | ||
const comName = comVendorName; | ||
return portList.filter( | ||
// Find the device with the matching comName | ||
(device) => device.comName === comName.toUpperCase() || device.comName === comName | ||
); | ||
} else { | ||
const vendorId = comVendorName; | ||
return portList.filter( | ||
// Find the device with the matching vendorId and productId | ||
(device) => | ||
(device.vendorId === vendorId.toUpperCase() || device.vendorId === vendorId) && | ||
device.productId === productId | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Retrieve's a serial port device based on either the COM name or product identifier | ||
* Returns false if the desired device was not found | ||
* @param {string} comVendorName EITHER a com name or the vendor identifier of the desired device | ||
* @param {string | undefined} productId The product identifier of the desired device | ||
* @returns The SerialPort device | ||
*/ | ||
// TODO @brown-ccv #460: This should fail, not return false | ||
export async function getPort(comVendorName, productId) { | ||
let portList; | ||
try { | ||
portList = await SerialPort.list(); | ||
} catch { | ||
return false; | ||
} | ||
|
||
const device = getDevice(portList, comVendorName, productId); | ||
try { | ||
const path = device[0].comName; | ||
const port = new SerialPort(path); | ||
return port; | ||
} catch { | ||
return false; | ||
} | ||
} | ||
|
||
/** | ||
* Sends event code data to a serial port device | ||
* @param {SerialPort} port A SerialPort device | ||
* @param {number} event_code The numeric code to write to the device | ||
*/ | ||
export async function sendToPort(port, event_code) { | ||
port.write(Buffer.from([event_code])); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters