# USB Relay
USB relay 模組就是這個,預計用 Node.js 程式輸出電氣訊號控制機械手臂,這個是兩路的(兩個 relay),還有一路或者更多路的。
輸出接點跟一般繼電器的接點一樣
端點 | 全名 | 功能描述 |
---|---|---|
COM | Common(共用端) | 中心端點,根據控制情況與 NC 或 NO 接通 |
NC | Normally Closed(常閉) | 在 未通電 狀態下, 與 COM 接通 |
NO | Normally Open(常開) | 在 未通電 狀態下, 與 COM 斷開 ,通電後才接通 |
接線方式使用 USB-A 轉 USB-B,USB-A 接電腦,USB-B 接模組
# 程式測試
使用 node-hid 套件,請先確定使用的模組支援 HID
const HID = require('node-hid'); | |
// 尋找連接裝置中 USBRelay 名稱的裝置 | |
const deviceList = HID.devices(); | |
const connectedRelays = deviceList.filter((device) => device.product && device.product.indexOf('USBRelay') !== -1); | |
const device = new HID.HID(connectedRelays[0].path); | |
// 啟動第一個 relay | |
device.sendFeatureReport([0x00, 0xFF, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]); | |
// 啟動第二個 relay | |
device.sendFeatureReport([0x00, 0xFF, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]); | |
// 關閉第一個 relay | |
device.sendFeatureReport([0x00, 0xFD, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]); | |
// 關閉第二個 relay | |
device.sendFeatureReport([0x00, 0xFD, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]); |
如果正常啟動繼電器時,模組上對應的繼電器旁 LED 會亮
# 設定 Bytes 說明
Byte 位置 | 說明 |
---|---|
0 | 命令標頭(通常是固定值) |
1 | 控制命令 |
2 | 要控制的繼電器位元組(bitmask) |
3~8 | 充填位元 |
# 程式設計思路
- 使用 class
- constructor 進行 init, 可指定 device path 或自動搜尋
- 每個 relay on/off 使用的 bytes 用 list 儲存
- setOff(channelNumber), setOn(channelNumber)
- 根據機械手臂動作定義,ActionOK (), ActionNG ()
- 切為 on 之後多久復歸原狀態可調整
# 部份程式碼
// 啟動第幾個 relay | |
setOn(number) { | |
const commandList = [ | |
// all | |
[0x00, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], | |
// 第一個 relay | |
[0x00, 0xFF, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], | |
// 第二個 relay | |
[0x00, 0xFF, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], | |
// 第三個 relay | |
[0x00, 0xFF, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], | |
// 第四個 relay | |
[0x00, 0xFF, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], | |
// 第五個 relay | |
[0x00, 0xFF, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], | |
// 第六個 relay | |
[0x00, 0xFF, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], | |
// 第七個 relay | |
[0x00, 0xFF, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], | |
// 第八個 relay | |
[0x00, 0xFF, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00] | |
]; | |
this.device.sendFeatureReport(commandList[number]); | |
} |
// 關閉第幾個 relay | |
setOff(number) { | |
const commandList = [ | |
// all | |
[0x00, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], | |
// 第一個 relay | |
[0x00, 0xFD, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], | |
// 第二個 relay | |
[0x00, 0xFD, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], | |
// 第三個 relay | |
[0x00, 0xFD, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], | |
// 第四個 relay | |
[0x00, 0xFD, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], | |
// 第五個 relay | |
[0x00, 0xFD, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], | |
// 第六個 relay | |
[0x00, 0xFD, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], | |
// 第七個 relay | |
[0x00, 0xFD, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], | |
// 第八個 relay | |
[0x00, 0xFD, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00] | |
]; | |
this.device.sendFeatureReport(commandList[number]); | |
} |
ActionOK() { | |
//relay 1 作為 OK 訊號 | |
this.setOn(1); | |
} |
ActionNG() { | |
//relay 2 作為 NG 訊號 | |
this.setOn(2); | |
} |
# 備註
- 注意輸出控制可承受負載範圍,通常在繼電器上會標示
- 需要 HID 支援的模組
- 參考 https://github.com/josephdadams/USBRelay
- 模組由 USB 供電,拔掉後 relay 狀態會重置