serial-discovery
工具是通过STDIO进行交互的命令行程序。它接受命令为终止LF n
的普通ASCII字符串,并将响应发送为JSON。
安装最近的Golang环境并进行go build
。可执行的serial-discovery
将在您的工作目录中生成。
启动后,该工具等待命令。可用命令是: HELLO
, START
, STOP
, QUIT
, LIST
和START_SYNC
。
HELLO
命令用于在客户端和发现之间建立可插入的发现协议。命令的格式是:
HELLO <PROTOCOL_VERSION> "<USER_AGENT>"
例如:
HELLO 1 "Arduino IDE"
或者:
HELLO 1 "arduino-cli"
在这种情况下,客户端要求的协议版本为1
(在撰写本文时,没有其他修订版)。对命令的响应是:
{
"eventType" : " hello " ,
"protocolVersion" : 1 ,
"message" : " OK "
}
protocolVersion
是该发现将在其余的通信中使用的协议版本。
开始START
了寻找端口的发现的内部子例程。必须在LIST
或START_SYNC
之前调用此命令。对开始命令的响应是:
{
"eventType" : " start " ,
"message" : " OK "
}
STOP
命令停止了发现内部子例程并释放一些资源。如果客户要暂停发现一段时间,则应调用此命令。对停止命令的响应是:
{
"eventType" : " stop " ,
"message" : " OK "
}
QUIT
命令终止了发现。对退出的反应是:
{
"eventType" : " quit " ,
"message" : " OK "
}
在此之后,输出工具退出。
LIST
命令返回当前可用串行端口的列表。响应的格式如下:
{
"eventType" : " list " ,
"ports" : [
{
"address" : " /dev/ttyACM0 " ,
"label" : " /dev/ttyACM0 " ,
"properties" : {
"pid" : " 0x804e " ,
"vid" : " 0x2341 " ,
"serialNumber" : " EBEABFD6514D32364E202020FF10181E "
},
"hardwareId" : " EBEABFD6514D32364E202020FF10181E " ,
"protocol" : " serial " ,
"protocolLabel" : " Serial Port (USB) "
}
]
}
ports
字段包含可用串行端口的列表。如果串行端口来自USB串行转换器,则在properties
内还报告了USB VID/PID和USB序列号属性。
列表命令是一个单发命令,如果您需要对端口进行连续监视,则应使用START_SYNC
命令。
START_SYNC
命令将工具置于“事件”模式:发现分别每次检测或删除新端口时,发现将add
和remove
事件。对命令的直接响应是:
{
"eventType" : " start_sync " ,
"message" : " OK "
}
之后,发现进入“事件”模式。
add
事件如下:
{
"eventType" : " add " ,
"port" : {
"address" : " /dev/ttyACM0 " ,
"label" : " /dev/ttyACM0 " ,
"properties" : {
"pid" : " 0x804e " ,
"vid" : " 0x2341 " ,
"serialNumber" : " EBEABFD6514D32364E202020FF10181E "
},
"hardwareId" : " EBEABFD6514D32364E202020FF10181E " ,
"protocol" : " serial " ,
"protocolLabel" : " Serial Port (USB) "
}
}
它基本上收集了与list
事件相同的信息,但对于一个端口。调用START_SYNC
后,可能会生成一堆add
事件,以报告开始时在开始时报告所有可用端口。
remove
事件看起来像这样:
{
"eventType" : " remove " ,
"port" : {
"address" : " /dev/ttyACM0 " ,
"protocol" : " serial "
}
}
在这种情况下,仅报告address
和protocol
字段。
发现用法的可能成绩单:
$ ./serial-discovery
START
{
"eventType": "start",
"message": "OK"
}
LIST
{
"eventType": "list",
"ports": [
{
"address": "/dev/ttyACM0",
"label": "/dev/ttyACM0",
"protocol": "serial",
"protocolLabel": "Serial Port (USB)",
"properties": {
"pid": "0x004e",
"serialNumber": "EBEABFD6514D32364E202020FF10181E",
"vid": "0x2341"
},
"hardwareId": "EBEABFD6514D32364E202020FF10181E"
}
]
}
START_SYNC
{
"eventType": "start_sync",
"message": "OK"
}
{ <--- this event has been immediately sent
"eventType": "add",
"port": {
"address": "/dev/ttyACM0",
"label": "/dev/ttyACM0",
"protocol": "serial",
"protocolLabel": "Serial Port (USB)",
"properties": {
"pid": "0x004e",
"serialNumber": "EBEABFD6514D32364E202020FF10181E",
"vid": "0x2341"
},
"hardwareId": "EBEABFD6514D32364E202020FF10181E"
}
}
{ <--- the board has been disconnected here
"eventType": "remove",
"port": {
"address": "/dev/ttyACM0",
"protocol": "serial"
}
}
{ <--- the board has been connected again
"eventType": "add",
"port": {
"address": "/dev/ttyACM0",
"label": "/dev/ttyACM0",
"protocol": "serial",
"protocolLabel": "Serial Port (USB)",
"properties": {
"pid": "0x004e",
"serialNumber": "EBEABFD6514D32364E202020FF10181E",
"vid": "0x2341"
},
"hardwareId": "EBEABFD6514D32364E202020FF10181E"
}
}
QUIT
{
"eventType": "quit",
"message": "OK"
}
$
如果您认为在此项目中发现了漏洞或其他与安全有关的错误,请阅读我们的安全策略,并向我们的安全团队报告该错误吗?测试谢谢!
电子邮件联系人:[email protected]
版权(C)2018 Arduino SA(www.arduino.cc)
该软件是根据GNU通用公共许可证发布的,该许可涵盖了串行发现代码的主体。可以在以下网址找到此许可证的条款
有关详细信息,请参见License.txt。