|
| 1 | +# Arduino pluggabe discovery for serial ports |
| 2 | + |
| 3 | +The `serial-discovery` tool is a command line program that interacts via stdio. It accepts commands as plain ASCII strings terminated with LF `\n` and sends response as JSON. |
| 4 | + |
| 5 | +## How to build |
| 6 | + |
| 7 | +Install a recent go enviroment (>=13.0) and run `go build`. The executable `serial-discovery` will be produced in your working directory. |
| 8 | + |
| 9 | +## Usage |
| 10 | + |
| 11 | +After startup, the tool waits for commands. The available commands are: `START`, `STOP`, `QUIT`, `LIST` and `START_SYNC`. |
| 12 | + |
| 13 | +#### START command |
| 14 | + |
| 15 | +The `START` starts the internal subroutines of the discovery that looks for ports. This command must be called before `LIST` or `START_SYNC`. The response to the start command is: |
| 16 | + |
| 17 | +```json |
| 18 | +{ |
| 19 | + "eventType": "start", |
| 20 | + "message": "OK" |
| 21 | +} |
| 22 | +``` |
| 23 | + |
| 24 | +#### STOP command |
| 25 | + |
| 26 | +The `STOP` command stops the discovery internal subroutines and free some resources. This command should be called if the client wants to pause the discovery for a while. The response to the quit command is: |
| 27 | + |
| 28 | +```json |
| 29 | +{ |
| 30 | + "eventType": "stop", |
| 31 | + "message": "OK" |
| 32 | +} |
| 33 | +``` |
| 34 | + |
| 35 | +#### QUIT command |
| 36 | + |
| 37 | +The `QUIT` command terminates the discovery. The response to quit is: |
| 38 | + |
| 39 | +```json |
| 40 | +{ |
| 41 | + "eventType": "quit", |
| 42 | + "message": "OK" |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +after this output the discovery terminates. |
| 47 | + |
| 48 | +#### LIST command |
| 49 | + |
| 50 | +The `LIST` command returns a list of the currently available serial ports. The format of the response is the following: |
| 51 | + |
| 52 | +```json |
| 53 | +{ |
| 54 | + "eventType": "list", |
| 55 | + "ports": [ |
| 56 | + { |
| 57 | + "address": "/dev/ttyACM0", |
| 58 | + "label": "/dev/ttyACM0", |
| 59 | + "prefs": { |
| 60 | + "productId": "0x804e", |
| 61 | + "serialNumber": "EBEABFD6514D32364E202020FF10181E", |
| 62 | + "vendorId": "0x2341" |
| 63 | + }, |
| 64 | + "identificationPrefs": { |
| 65 | + "pid": "0x804e", |
| 66 | + "vid": "0x2341" |
| 67 | + }, |
| 68 | + "protocol": "serial", |
| 69 | + "protocolLabel": "Serial Port (USB)" |
| 70 | + } |
| 71 | + ] |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +The `ports` field contains a list of the available serial ports. If the serial port comes from an USB serial converter the USB VID/PID properties are also reported inside `prefs`. Inside the `identificationPrefs` instead we have only the properties useful for product identification (in this case vid/pid only that may be useful to identify the board) |
| 76 | + |
| 77 | +The list command is a one-shot command, if you need continuos monitoring of ports you should use `START_SYNC` command. |
| 78 | + |
| 79 | +#### START_SYNC command |
| 80 | + |
| 81 | +The `START_SYNC` command puts the discovery tool in "events" mode: the discovery will send `add` and `remove` events each time a new port is detected or removed respectively. An `add` and `remove` events looks like the following: |
| 82 | + |
| 83 | +```json |
| 84 | +{ |
| 85 | + "eventType": "add", |
| 86 | + "port": { |
| 87 | + "address": "/dev/ttyACM0", |
| 88 | + "label": "/dev/ttyACM0", |
| 89 | + "prefs": { |
| 90 | + "productId": "0x804e", |
| 91 | + "serialNumber": "EBEABFD6514D32364E202020FF10181E", |
| 92 | + "vendorId": "0x2341" |
| 93 | + }, |
| 94 | + "identificationPrefs": { |
| 95 | + "pid": "0x804e", |
| 96 | + "vid": "0x2341" |
| 97 | + }, |
| 98 | + "protocol": "serial", |
| 99 | + "protocolLabel": "Serial Port (USB)" |
| 100 | + } |
| 101 | +} |
| 102 | +``` |
| 103 | + |
| 104 | +it basically gather the same information as the list event but for a single port. After calling `START_SYNC` a bunch of `add` events may be generated to report all the ports available at the moment of the start. |
| 105 | + |
| 106 | +The `remove` event looks like this: |
| 107 | + |
| 108 | +```json |
| 109 | +{ |
| 110 | + "eventType": "remove", |
| 111 | + "port": { |
| 112 | + "address": "/dev/ttyACM0" |
| 113 | + } |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +in this case only the `address` field is reported. |
| 118 | + |
| 119 | +### Example of usage |
| 120 | + |
| 121 | +Here a possible transcript of the discovery usage: |
| 122 | + |
| 123 | +```json |
| 124 | +$ ./serial-discovery |
| 125 | +START |
| 126 | +{ |
| 127 | + "eventType": "start", |
| 128 | + "message": "OK" |
| 129 | +} |
| 130 | +START_SYNC |
| 131 | +{ |
| 132 | + "eventType": "add", |
| 133 | + "port": { |
| 134 | + "address": "/dev/ttyACM0", |
| 135 | + "label": "/dev/ttyACM0", |
| 136 | + "prefs": { |
| 137 | + "productId": "0x804e", |
| 138 | + "serialNumber": "EBEABFD6514D32364E202020FF10181E", |
| 139 | + "vendorId": "0x2341" |
| 140 | + }, |
| 141 | + "identificationPrefs": { |
| 142 | + "pid": "0x804e", |
| 143 | + "vid": "0x2341" |
| 144 | + }, |
| 145 | + "protocol": "serial", |
| 146 | + "protocolLabel": "Serial Port (USB)" |
| 147 | + } |
| 148 | +} |
| 149 | +{ <--- the board has been disconnected here |
| 150 | + "eventType": "remove", |
| 151 | + "port": { |
| 152 | + "address": "/dev/ttyACM0" |
| 153 | + } |
| 154 | +} |
| 155 | +{ <--- the board has been connected again |
| 156 | + "eventType": "add", |
| 157 | + "port": { |
| 158 | + "address": "/dev/ttyACM0", |
| 159 | + "label": "/dev/ttyACM0", |
| 160 | + "prefs": { |
| 161 | + "productId": "0x804e", |
| 162 | + "serialNumber": "EBEABFD6514D32364E202020FF10181E", |
| 163 | + "vendorId": "0x2341" |
| 164 | + }, |
| 165 | + "identificationPrefs": { |
| 166 | + "pid": "0x804e", |
| 167 | + "vid": "0x2341" |
| 168 | + }, |
| 169 | + "protocol": "serial", |
| 170 | + "protocolLabel": "Serial Port (USB)" |
| 171 | + } |
| 172 | +} |
| 173 | +QUIT |
| 174 | +{ |
| 175 | + "eventType": "quit", |
| 176 | + "message": "OK" |
| 177 | +} |
| 178 | +$ |
| 179 | +``` |
| 180 | + |
| 181 | +## License |
| 182 | + |
| 183 | +Copyright (c) 2018 ARDUINO SA (www.arduino.cc) |
| 184 | + |
| 185 | +The software is released under the GNU General Public License, which covers the main body |
| 186 | +of the serial-discovery code. The terms of this license can be found at: |
| 187 | +https://www.gnu.org/licenses/gpl-3.0.en.html |
| 188 | + |
| 189 | +See [LICENSE.txt](<https://github.com/arduino/serial-discovery/blob/master/LICENSE.txt>) for details. |
| 190 | + |
0 commit comments