UNPKG

8.43 kBMarkdownView Raw
1# PandaJS
2Interract with your [panda OBD-II Dongle](https://shop.comma.ai/products/panda-obd-ii-dongle) from JavaScript.
3
4Supports
5 * [x] Browser
6 * [x] Node
7 * [ ] React Native
8
9## Installation
10`npm i --save @commaai/pandajs`
11
12or
13
14`yarn add @commaai/pandajs`
15
16## Usage
17```js
18import Panda from '@commaai/pandajs';
19
20// create instance
21var panda = new Panda();
22
23// register listener
24panda.onMessage((msg) => {
25 console.log(msg.address, msg.busTime, msg.data, msg.bus);
26});
27
28// start reading data
29panda.start();
30
31// pause data
32panda.pause();
33
34// resume reading data
35panda.unpause();
36
37// close connects and take everything down
38panda.disconnect();
39```
40
41## API
42
43#### `new Panda([options])` -> `Panda`
44Creates a new Panda instance
45
46* `options` (*optional* object)
47 * `selectDevice`: (*optional* `function(devices, callback)`) A user defined function for selecting which available device to use, parameters are an array of discovered devices and a callback function. The method can either return the desired device, return a promise, or use the callback function. There is no timeout for this method, so make sure it eventually does one of those three things. *This option does nothing in browser mode since webusb has it's own UI for selecting the device.*
48 * `wifi`: (*optional* `boolean`) Enables wifi mode, communicates with the panda device over an already established wifi connection with it. This option will throw errors if you enable it in browser mode.
49
50### Methods
51#### `Panda.connect()` -> `Promise: string`
52Tell the Panda instance to connect to USB. Does not start reading messages until you call `start`. On success, returns the ID of the USB device found.
53
54#### `Panda.disconnect()` -> `Promise: boolean`
55Disconnect from the USB device and stop everything. Returns true if it was running before.
56
57#### `Panda.start()` -> `Promise: string`
58Tell the Panda instance to connect to USB if it's not already and start reading messages. On success, returns the ID of the USB device found.
59
60This function is the same as running
61```js
62panda.connect()
63 .then(() => panda.unpause());
64```
65
66#### `Panda.pause()` -> `boolean`
67Pauses reading in messages from the active connection. Returns true only if the stream was not already paused. While paused messages will queue up on the Panda device, however this queue has a limited size so pausing may result in missed messages.
68
69#### `Panda.unpause()` -> `boolean`
70Pauses reading in messages from the active connection. Returns true only if the stream was paused.
71
72#### `Panda.resume()` -> `boolean`
73Alias for `unpause`.
74
75#### `Panda.isConnected()` -> `boolean`
76Returns true if the panda instance is currecntly connected to a USB device.
77
78#### `Panda.isPaused()` -> `boolean`
79Returns true if the panda instance currently has message reading paused
80
81#### `Panda.getHealth()` -> `HealthStatus`
82Requests the current health of the Panda unit. Only available while connected, returns the health object on success or errors.
83
84* `HealthStatus`: (object)
85 * `voltage`: (number)
86 * `current`: (number)
87 * `isStarted`: (boolean)
88 * `controlsAreAllowed`: (boolean)
89 * `isGasInterceptorDetector`: (boolean)
90 * `isStartSignalDetected`: (boolean)
91 * `isStartedAlt`: (boolean)
92
93#### `Panda.getDeviceMetadata()` -> `[string, string]`
94Requests both the serial number and secret from the Panda device. This is used internally to power `getSerialNumber` and `getSecret`.
95
96Returns an array with 2 items in, the serial number and then the secret.
97
98#### `Panda.getSerialNumber()` -> `string`
99Requests the serial number.
100
101This function is the same as running
102```js
103panda.getDeviceMetadata()
104 .then((result) => result[0])
105```
106
107#### `Panda.getSecret()` -> `string`
108Requests the secret used for wifi among other things.
109
110This function is the same as running
111```js
112panda.getDeviceMetadata()
113 .then((result) => result[1])
114```
115
116#### `Panda.getVersion()` -> `string`
117Requests the string version number, output will look something like `v1.0.1-11d45090-RELEASE`
118
119#### `Panda.isGrey()` -> `boolean`
120Query if the Panda device is a grey Panda or a normal Panda. Returns true if it is grey.
121
122#### `Panda.setSafetyMode(mode)` -> `void`
123Sets the safety mode on the Panda device.
124
125 * `mode`: (*required* `SafetyMode`) The safety mode to enter. Must be a valid safety mode, safety modes are exposed as constants on the root of the library.
126
127```js
128import { SAFETY_NOOUTPUT, SAFETY_HONDA, SAFETY_TOYOTA, SAFETY_HONDA_BOSCH, SAFETY_TOYOTA_NOLIMITS, SAFETY_ALLOUTPUT, SAFETY_ELM327 } from '@commaai/pandajs';
129```
130
131### Events
132#### `Panda.onMessage(listener)` -> `function`
133Register a handler for receiving messages. This should be done before calling `start` to avoid missing any messages. Returns an `unlisten` function that will disable the listener when called.
134
135* `listener`: (*required*, `function (messageData)`) Handler to call each time messages are received
136 * `messageData`: (object)
137 * `time`: (number) High precision time in which this event was received.
138 * `canMessages`: (Array<CanMessage>)
139 * `bus`: (number) The bus this message came from
140 * `address`: (number) The bus specific address for the message
141 * `busTime`: (number) The recieve time according to the bus
142 * `data`: (ByteArray) Raw data for this message
143
144#### `Panda.onError(listener)` -> `function`
145Register an error handler. Returns an `unlisten` function that will disable the listener when called.
146
147* `listener`: (*required*, `function (err)`) Handler to call when errors occur
148 * `err`: (object)
149 * `error`: (Error) Error object incurred
150 * `event`: (string) Description of where the error occured
151
152#### `Panda.onConnect(listener)` -> `function`
153Register a handler to run when successfully connecting to a Panda device. Returns an `unlisten` function that will disable the listener when called.
154
155* `listener`: (*required*, `function (usbId)`) Handler to call when connected
156 * `usbId`: (string) The ID of the USB device connected to.
157
158#### `Panda.onDisconnect(listener)` -> `function`
159Register a handler to run when disconnecting from a Panda device. Returns an `unlisten` function that will disable the listener when called.
160
161* `listener`: (*required*, `function (usbId)`) Handler to call when disconnected
162 * `usbId`: (string) The ID of the USB device disconnected from.
163
164## Command Line Tools
165This package ships with 2 CLI tools. Check the `--help` on each to see all of their options.
166
167#### `dump-can`
168This is used to dump metadata about the incoming CAN messages, or output the entire messages with `-a`.
169
170Examples:
171```bash
172$ dump-can
173Connected: ba435375436c2a6d
174{ voltage: 12.123,
175 current: 4.093,
176 isStarted: false,
177 controlsAreAllowed: false,
178 isGasInterceptorDetector: false,
179 isStartSignalDetected: false,
180 isStartedAlt: false }
181Connect finished, waiting then reading all messages...
182Start reading...
183Message count: 1
184First message CAN count: 256
185First CAN message: { address: 420,
186 busTime: 54858,
187 data: <Buffer 00 66 00 02 00 00 00 0b>,
188 bus: 0 }
189Message count: 1
190First message CAN count: 256
191First CAN message: { address: 1024,
192 busTime: 6626,
193 data: <Buffer 79 00 02 00 0a 78 00 09>,
194 bus: 1 }
195```
196```bash
197$ dump-can -an
198{"time":7.3178732019999995,"canMessages":[{"address":1024,"busTime":53980,"data":[121,0,2,0,10,120,0,9],"bus":1},{"address":420,"busTime":54066,"data":[0,102,0,2,0,0,0,11],"bus":0},{"address":1024,"busTime":54115,"data":[121,0,2,0,10,120,0,9],"bus":1}]}
199{"time":7.318214842000001,"canMessages":[{"address":420,"busTime":54202,"data":[0,102,0,2,0,0,0,11],"bus":0},{"address":1024,"busTime":54250,"data":[121,0,2,0,10,120,0,9],"bus":1},{"address":420,"busTime":54338,"data":[0,102,0,2,0,0,0,11],"bus":0}]}
200{"time":7.31846793,"canMessages":[{"address":1024,"busTime":54385,"data":[121,0,2,0,10,120,0,9],"bus":1},{"address":420,"busTime":54474,"data":[0,102,0,2,0,0,0,11],"bus":0}]}
201```
202
203#### `control-panda`
204This is a convenience tool wrapping each of the get and set methods exposed by this library.
205
206Examples:
207```bash
208$ ./control-panda.js wifi
209SID: panda-2baf789dacdacb64
210Password: aGBw61RwD0
211```
212```bash
213$ ./control-panda.js version
214v1.0.1-11d45090-RELEASE
215```
216
217## Contributing
218`yarn run test`
219
220# License
221MIT @ comma.ai
222
223Read more about how to get started hacking your car with [panda](https://shop.comma.ai/products/panda-obd-ii-dongle) [here](https://medium.com/@comma_ai/a-panda-and-a-cabana-how-to-get-started-car-hacking-with-comma-ai-b5e46fae8646).