| OLD | NEW |
| 1 <h1>Accessing Hardware Devices</h1> | 1 <h1>Accessing Hardware Devices</h1> |
| 2 | 2 |
| 3 | 3 |
| 4 <p> | 4 <p> |
| 5 This doc shows you how packaged apps can connect to USB devices | 5 This doc shows you how packaged apps can connect to USB devices |
| 6 and read from and write to a user's serial ports. | 6 and read from and write to a user's serial ports. |
| 7 See also the reference docs for the | 7 See also the reference docs for the |
| 8 <a href="experimental.usb.html">USB API</a> | 8 <a href="experimental.usb.html">USB API</a> |
| 9 and the | 9 and the |
| 10 <a href="experimental.serial.html">Serial API</a>. | 10 <a href="experimental.serial.html">Serial API</a>. |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 <a href="https://github.com/GoogleChrome/chrome-app-samples/tree/master/usb">usb
</a>, | 22 <a href="https://github.com/GoogleChrome/chrome-app-samples/tree/master/usb">usb
</a>, |
| 23 and <a href="https://github.com/GoogleChrome/chrome-app-samples/tree/master/blue
tooth-demo">bluetooth-demo</a> samples. | 23 and <a href="https://github.com/GoogleChrome/chrome-app-samples/tree/master/blue
tooth-demo">bluetooth-demo</a> samples. |
| 24 </p> | 24 </p> |
| 25 | 25 |
| 26 <h2 id="usb">Accessing USB devices</h2> | 26 <h2 id="usb">Accessing USB devices</h2> |
| 27 | 27 |
| 28 <p> | 28 <p> |
| 29 You can use the USB API to send messages to any connected device. | 29 You can use the USB API to send messages to any connected device. |
| 30 </p> | 30 </p> |
| 31 | 31 |
| 32 <h3>Manifest requirement</h3> | 32 <h3 id="manifest">Manifest requirement</h3> |
| 33 | 33 |
| 34 <p> | 34 <p> |
| 35 You must add the "usb" permission | 35 You must add the "usb" permission |
| 36 to the manifest file: | 36 to the manifest file: |
| 37 </p> | 37 </p> |
| 38 | 38 |
| 39 <pre> | 39 <pre> |
| 40 "permissions": [ | 40 "permissions": [ |
| 41 "app.window", | 41 "app.window", |
| 42 "experimental", | 42 "experimental", |
| 43 "usb" | 43 "usb" |
| 44 ] | 44 ] |
| 45 </pre> | 45 </pre> |
| 46 | 46 |
| 47 <h3>Finding a device</h3> | 47 <h3 id="finding_device">Finding a device</h3> |
| 48 | 48 |
| 49 <p> | 49 <p> |
| 50 Every device in a USB bus is identified | 50 Every device in a USB bus is identified |
| 51 by its vendor and product IDs. | 51 by its vendor and product IDs. |
| 52 To find a device, | 52 To find a device, |
| 53 use the <code>findDevice()</code> method | 53 use the <code>findDevice()</code> method |
| 54 which has four parameters: | 54 which has four parameters: |
| 55 </p> | 55 </p> |
| 56 | 56 |
| 57 <pre> | 57 <pre> |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 } | 109 } |
| 110 }; | 110 }; |
| 111 | 111 |
| 112 var onUsbEvent = function(event) { | 112 var onUsbEvent = function(event) { |
| 113 console.log(“USB event!”); | 113 console.log(“USB event!”); |
| 114 }; | 114 }; |
| 115 | 115 |
| 116 chrome.experimental.usb.findDevice(vendorId, productId, {"onEvent": onUsbEvent},
onDeviceFound); | 116 chrome.experimental.usb.findDevice(vendorId, productId, {"onEvent": onUsbEvent},
onDeviceFound); |
| 117 </pre> | 117 </pre> |
| 118 | 118 |
| 119 <h3>USB transfers and receiving data from a device</h3> | 119 <h3 id="usb_transfers">USB transfers and receiving data from a device</h3> |
| 120 | 120 |
| 121 <p> | 121 <p> |
| 122 USB protocol defines four types of transfers: | 122 USB protocol defines four types of transfers: |
| 123 control, bulk, isochronous and interrupt. | 123 control, bulk, isochronous and interrupt. |
| 124 Theoretically they can all occur in both directions:<br> | 124 Theoretically they can all occur in both directions:<br> |
| 125 device-to-host and host-to-device. | 125 device-to-host and host-to-device. |
| 126 Host-to-device is initiated by your code and is described in the next sections. | 126 Host-to-device is initiated by your code and is described in the next sections. |
| 127 </p> | 127 </p> |
| 128 | 128 |
| 129 <p> | 129 <p> |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 <pre> | 164 <pre> |
| 165 var onUsbEvent = function(event) { | 165 var onUsbEvent = function(event) { |
| 166 if (event && event.resultCode===0 && event.data) { | 166 if (event && event.resultCode===0 && event.data) { |
| 167 console.log(“got ”+event.data.length+" bytes"); | 167 console.log(“got ”+event.data.length+" bytes"); |
| 168 } | 168 } |
| 169 }; | 169 }; |
| 170 | 170 |
| 171 chrome.experimental.usb.findDevice( vendorId, productId, {"onEvent": onUsbEvent}
, onDeviceFound); | 171 chrome.experimental.usb.findDevice( vendorId, productId, {"onEvent": onUsbEvent}
, onDeviceFound); |
| 172 </pre> | 172 </pre> |
| 173 | 173 |
| 174 <h3>Sending data - control transfers</h3> | 174 <h3 id="control_transfers">Sending data - control transfers</h3> |
| 175 | 175 |
| 176 <p> | 176 <p> |
| 177 Control transfers are generally used to send configuration | 177 Control transfers are generally used to send configuration |
| 178 or command parameters to a USB device. | 178 or command parameters to a USB device. |
| 179 The method is simple and receives three parameters: | 179 The method is simple and receives three parameters: |
| 180 </p> | 180 </p> |
| 181 | 181 |
| 182 <pre> | 182 <pre> |
| 183 chrome.experimental.usb.controlTransfer(deviceObj, transferInfo, transferCallbac
k) | 183 chrome.experimental.usb.controlTransfer(deviceObj, transferInfo, transferCallbac
k) |
| 184 </pre> | 184 </pre> |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 265 </tr> | 265 </tr> |
| 266 </table> | 266 </table> |
| 267 | 267 |
| 268 <p> | 268 <p> |
| 269 Example: | 269 Example: |
| 270 </p> | 270 </p> |
| 271 | 271 |
| 272 <pre> | 272 <pre> |
| 273 var transferInfo = { | 273 var transferInfo = { |
| 274 "requestType": "vendor", | 274 "requestType": "vendor", |
| 275 "recipient": "device", | 275 "recipient": "device", |
| 276 "direction": "out", | 276 "direction": "out", |
| 277 "request": 0x31, | 277 "request": 0x31, |
| 278 "value": 120, | 278 "value": 120, |
| 279 "index": 0, | 279 "index": 0, |
| 280 "data": [4, 8, 15, 16, 23, 42] | 280 "data": [4, 8, 15, 16, 23, 42] |
| 281 }; | 281 }; |
| 282 chrome.experimental.usb.controlTransfer(deviceObj, transferInfo, optionalCallbac
k); | 282 chrome.experimental.usb.controlTransfer(deviceObj, transferInfo, optionalCallbac
k); |
| 283 </pre> | 283 </pre> |
| 284 | 284 |
| 285 <h3>Sending data - isochronous transfers</h3> | 285 <h3 id="isochronous_transfers">Sending data - isochronous transfers</h3> |
| 286 | 286 |
| 287 <p> | 287 <p> |
| 288 Isochronous transfers are commonly used for streams of data. | 288 Isochronous transfers are commonly used for streams of data. |
| 289 Video and sound, for example, are good candidates for this transfer type. | 289 Video and sound, for example, are good candidates for this transfer type. |
| 290 To send an isochronous transfer, use: | 290 To send an isochronous transfer, use: |
| 291 </p> | 291 </p> |
| 292 | 292 |
| 293 <pre> | 293 <pre> |
| 294 chrome.experimental.usb.isochronousTransfer(deviceObj, isochronousTransferInfo,
transferCallback) | 294 chrome.experimental.usb.isochronousTransfer(deviceObj, isochronousTransferInfo,
transferCallback) |
| 295 </pre> | 295 </pre> |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 351 <td>Expected length of each packet in this transfer.</td> | 351 <td>Expected length of each packet in this transfer.</td> |
| 352 </tr> | 352 </tr> |
| 353 </table> | 353 </table> |
| 354 | 354 |
| 355 <p> | 355 <p> |
| 356 Example: | 356 Example: |
| 357 </p> | 357 </p> |
| 358 | 358 |
| 359 <pre> | 359 <pre> |
| 360 var transferInfo = { | 360 var transferInfo = { |
| 361 "direction": "in", | 361 "direction": "in", |
| 362 "endpoint": 1, | 362 "endpoint": 1, |
| 363 "length": 2560 | 363 "length": 2560 |
| 364 }; | 364 }; |
| 365 var isoTransferInfo = { | 365 var isoTransferInfo = { |
| 366 "transferInfo": transferInfo, | 366 "transferInfo": transferInfo, |
| 367 "packets": 20, | 367 "packets": 20, |
| 368 "packetLength": 128 | 368 "packetLength": 128 |
| 369 }; | 369 }; |
| 370 chrome.experimental.usb.isochronousTransfer(deviceObj, isoTransferInfo, optional
Callback); | 370 chrome.experimental.usb.isochronousTransfer(deviceObj, isoTransferInfo, optional
Callback); |
| 371 </pre> | 371 </pre> |
| 372 | 372 |
| 373 <h3>Sending data - bulk transfers</h3> | 373 <h3 id="bulk_transfers">Sending data - bulk transfers</h3> |
| 374 | 374 |
| 375 <p> | 375 <p> |
| 376 Bulk transfer is a USB transfer type commonly used | 376 Bulk transfer is a USB transfer type commonly used |
| 377 to transfer a large amount of data reliably. | 377 to transfer a large amount of data reliably. |
| 378 The method has three parameters: | 378 The method has three parameters: |
| 379 </p> | 379 </p> |
| 380 | 380 |
| 381 <pre> | 381 <pre> |
| 382 chrome.experimental.usb.bulkTransfer(deviceObj, transferInfo, transferCallback) | 382 chrome.experimental.usb.bulkTransfer(deviceObj, transferInfo, transferCallback) |
| 383 </pre> | 383 </pre> |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 438 only used when direction is "out".</td> | 438 only used when direction is "out".</td> |
| 439 </tr> | 439 </tr> |
| 440 </table> | 440 </table> |
| 441 | 441 |
| 442 <p> | 442 <p> |
| 443 Example: | 443 Example: |
| 444 </p> | 444 </p> |
| 445 | 445 |
| 446 <pre> | 446 <pre> |
| 447 var transferInfo = { | 447 var transferInfo = { |
| 448 "direction": "out", | 448 "direction": "out", |
| 449 "endpoint": 1, | 449 "endpoint": 1, |
| 450 "data": [4, 8, 15, 16, 23, 42] | 450 "data": [4, 8, 15, 16, 23, 42] |
| 451 }; | 451 }; |
| 452 </pre> | 452 </pre> |
| 453 | 453 |
| 454 <h3>Sending data - interrupt transfers</h3> | 454 <h3 id="interrupt_transfers">Sending data - interrupt transfers</h3> |
| 455 | 455 |
| 456 <p> | 456 <p> |
| 457 Interrupt transfers are used to send important notifications. | 457 Interrupt transfers are used to send important notifications. |
| 458 Since all USB communication is initiated by the host, | 458 Since all USB communication is initiated by the host, |
| 459 host code usually polls the device periodically, | 459 host code usually polls the device periodically, |
| 460 sending interrupt IN transfers that will make the device send data back | 460 sending interrupt IN transfers that will make the device send data back |
| 461 if there is anything on the interrupt queue. | 461 if there is anything on the interrupt queue. |
| 462 The method has three parameters: | 462 The method has three parameters: |
| 463 </p> | 463 </p> |
| 464 | 464 |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 528 | 528 |
| 529 <pre> | 529 <pre> |
| 530 var transferInfo = { | 530 var transferInfo = { |
| 531 "direction": "in", | 531 "direction": "in", |
| 532 "endpoint": 1, | 532 "endpoint": 1, |
| 533 "length": 2 | 533 "length": 2 |
| 534 }; | 534 }; |
| 535 chrome.experimental.usb.interruptTransfer(deviceObj, transferInfo, optionalCallb
ack); | 535 chrome.experimental.usb.interruptTransfer(deviceObj, transferInfo, optionalCallb
ack); |
| 536 </pre> | 536 </pre> |
| 537 | 537 |
| 538 <h3>Caveats</h3> | 538 <h3 id="caveats">Caveats</h3> |
| 539 | 539 |
| 540 <p> | 540 <p> |
| 541 On Linux, | 541 On Linux, |
| 542 you must have specific permission to access USB devices. | 542 you must have specific permission to access USB devices. |
| 543 Create a file: <code>/etc/udev/rules.d/50-yourdevicename.rules</code>. | 543 Create a file: <code>/etc/udev/rules.d/50-yourdevicename.rules</code>. |
| 544 Add the following content: | 544 Add the following content: |
| 545 </p> | 545 </p> |
| 546 | 546 |
| 547 <pre> | 547 <pre> |
| 548 SUBSYSTEM=="usb", ATTR{idVendor}=="yourdevicevendor", MODE="0664", GROUP="plugde
v" | 548 SUBSYSTEM=="usb", ATTR{idVendor}=="yourdevicevendor", MODE="0664", GROUP="plugde
v" |
| 549 </pre> | 549 </pre> |
| 550 | 550 |
| 551 <p> | 551 <p> |
| 552 On MacOSX, | 552 On MacOSX, |
| 553 devices which expose a HID profile cannot be managed | 553 devices which expose a HID profile cannot be managed |
| 554 using this low level API due to system restrictions. | 554 using this low level API due to system restrictions. |
| 555 Currently there is no workaround. | 555 Currently there is no workaround. |
| 556 </p> | 556 </p> |
| 557 | 557 |
| 558 <h2 id="serial">Accessing serial devices</h2> | 558 <h2 id="serial">Accessing serial devices</h2> |
| 559 | 559 |
| 560 <p> | 560 <p> |
| 561 You can use the serial API to read | 561 You can use the serial API to read |
| 562 and write from a serial device. | 562 and write from a serial device. |
| 563 </p> | 563 </p> |
| 564 | 564 |
| 565 <h3>Manifest requirement</h3> | 565 <h3 id="requirement">Manifest requirement</h3> |
| 566 | 566 |
| 567 <p> | 567 <p> |
| 568 The "serial" permission is not yet required; | 568 The "serial" permission is not yet required; |
| 569 "experimental" is enough for now: | 569 "experimental" is enough for now: |
| 570 </p> | 570 </p> |
| 571 | 571 |
| 572 <pre> | 572 <pre> |
| 573 "permissions": ["experimental"] | 573 "permissions": ["experimental"] |
| 574 </pre> | 574 </pre> |
| 575 | 575 |
| 576 <h3>Listing available serial ports</h3> | 576 <h3 id="listing">Listing available serial ports</h3> |
| 577 | 577 |
| 578 <p> | 578 <p> |
| 579 To get a list of available serial ports, | 579 To get a list of available serial ports, |
| 580 use the <code>getPorts()</code> method: | 580 use the <code>getPorts()</code> method: |
| 581 </p> | 581 </p> |
| 582 | 582 |
| 583 <pre> | 583 <pre> |
| 584 var onGetPorts = function(ports) { | 584 var onGetPorts = function(ports) { |
| 585 for (var i=0; i<ports.length; i++) { | 585 for (var i=0; i<ports.length; i++) { |
| 586 console.log(ports[i]); | 586 console.log(ports[i]); |
| 587 } | 587 } |
| 588 } | 588 } |
| 589 chrome.experimental.serial.getPorts(onGetPorts); | 589 chrome.experimental.serial.getPorts(onGetPorts); |
| 590 </pre> | 590 </pre> |
| 591 | 591 |
| 592 <h3>Opening a serial device</h3> | 592 <h3 id="opening">Opening a serial device</h3> |
| 593 | 593 |
| 594 <p> | 594 <p> |
| 595 Here's how to open a serial device: | 595 Here's how to open a serial device: |
| 596 </p> | 596 </p> |
| 597 | 597 |
| 598 <pre> | 598 <pre> |
| 599 var onOpen = function(connectionInfo) { | 599 var onOpen = function(connectionInfo) { |
| 600 // The serial device has been opened. Save its id to use later. | 600 // The serial device has been opened. Save its id to use later. |
| 601 var conId = connectionInfo.connectionId; | 601 var conId = connectionInfo.connectionId; |
| 602 // Do whatever you need to do with the opened device. | 602 // Do whatever you need to do with the opened device. |
| 603 } | 603 } |
| 604 // Open the serial device /dev/ttyS01 | 604 // Open the serial device /dev/ttyS01 |
| 605 chrome.experimental.serial.open("/dev/ttyS01", onOpen); | 605 chrome.experimental.serial.open("/dev/ttyS01", onOpen); |
| 606 </pre> | 606 </pre> |
| 607 | 607 |
| 608 <h3>Closing a serial device</h3> | 608 <h3 id="closing">Closing a serial device</h3> |
| 609 | 609 |
| 610 <p> | 610 <p> |
| 611 Here's how to close a serial device: | 611 Here's how to close a serial device: |
| 612 </p> | 612 </p> |
| 613 | 613 |
| 614 <pre> | 614 <pre> |
| 615 var onClose = function(result) { | 615 var onClose = function(result) { |
| 616 console.log(“Serial port closed”); | 616 console.log(“Serial port closed”); |
| 617 } | 617 } |
| 618 chrome.experimental.serial.close(conId, onClose); | 618 chrome.experimental.serial.close(conId, onClose); |
| 619 </pre> | 619 </pre> |
| 620 | 620 |
| 621 <h3>Reading from a serial device</h3> | 621 <h3 id="reading">Reading from a serial device</h3> |
| 622 | 622 |
| 623 <p> | 623 <p> |
| 624 The serial API reads from the serial port and | 624 The serial API reads from the serial port and |
| 625 delivers the read bytes as an ArrayBuffer. | 625 delivers the read bytes as an ArrayBuffer. |
| 626 There is no guarantee that all the available bytes will be read in one chunk | 626 There is no guarantee that all the available bytes will be read in one chunk |
| 627 (they are currently read one byte per time, but this might change in the future)
. | 627 (they are currently read one byte per time, but this might change in the future)
. |
| 628 The following procedure can accumulate read bytes until a new line is read, | 628 The following procedure can accumulate read bytes until a new line is read, |
| 629 and then call a listener with the <code>ArrayBuffer</code> bytes converted to a
String: | 629 and then call a listener with the <code>ArrayBuffer</code> bytes converted to a
String: |
| 630 </p> | 630 </p> |
| 631 | 631 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 649 chrome.experimental.serial.read(connectionInfo.connectionId, onCharRead); | 649 chrome.experimental.serial.read(connectionInfo.connectionId, onCharRead); |
| 650 } | 650 } |
| 651 | 651 |
| 652 /* Convert an ArrayBuffer to a String, using UTF-8 as the encoding scheme. | 652 /* Convert an ArrayBuffer to a String, using UTF-8 as the encoding scheme. |
| 653 This is consistent with how Arduino sends characters by default */ | 653 This is consistent with how Arduino sends characters by default */ |
| 654 var ab2str=function(buf) { | 654 var ab2str=function(buf) { |
| 655 return String.fromCharCode.apply(null, new Uint8Array(buf)); | 655 return String.fromCharCode.apply(null, new Uint8Array(buf)); |
| 656 }; | 656 }; |
| 657 </pre> | 657 </pre> |
| 658 | 658 |
| 659 <h3>Writing to a serial device</h3> | 659 <h3 id="writing">Writing to a serial device</h3> |
| 660 | 660 |
| 661 <p> | 661 <p> |
| 662 The writing routine is simpler than the reading, | 662 The writing routine is simpler than the reading, |
| 663 since the writing can occur all at once. | 663 since the writing can occur all at once. |
| 664 The only catch is that if your data protocol is String based, | 664 The only catch is that if your data protocol is String based, |
| 665 you have to convert your output string to an <code>ArrayBuffer</code> | 665 you have to convert your output string to an <code>ArrayBuffer</code> |
| 666 to compy with write's method signature. | 666 to compy with write's method signature. |
| 667 See the code below: | 667 See the code below: |
| 668 </p> | 668 </p> |
| 669 | 669 |
| 670 <pre> | 670 <pre> |
| 671 var writeSerial=function(str) { | 671 var writeSerial=function(str) { |
| 672 chrome.experimental.serial.write(connectionInfo.connectionId, str2ab(str), onW
rite); | 672 chrome.experimental.serial.write(connectionInfo.connectionId, str2ab(str), onW
rite); |
| 673 } | 673 } |
| 674 var str2ab=function(str) { | 674 var str2ab=function(str) { |
| 675 var buf=new ArrayBuffer(str.length); | 675 var buf=new ArrayBuffer(str.length); |
| 676 var bufView=new Uint8Array(buf); | 676 var bufView=new Uint8Array(buf); |
| 677 for (var i=0; i<str.length; i++) { | 677 for (var i=0; i<str.length; i++) { |
| 678 bufView[i]=str.charCodeAt(i); | 678 bufView[i]=str.charCodeAt(i); |
| 679 } | 679 } |
| 680 return buf; | 680 return buf; |
| 681 } | 681 } |
| 682 </pre> | 682 </pre> |
| 683 | 683 |
| 684 <h3>Flushing a serial device buffer</h3> | 684 <h3 id="flushing">Flushing a serial device buffer</h3> |
| 685 | 685 |
| 686 <p> | 686 <p> |
| 687 You can flush your serial device buffer by issuing the flush command on the API: | 687 You can flush your serial device buffer by issuing the flush command on the API: |
| 688 </p> | 688 </p> |
| 689 | 689 |
| 690 <pre> | 690 <pre> |
| 691 var flushSerial=function(str) { | 691 var flushSerial=function(str) { |
| 692 chrome.experimental.serial.flush(connectionInfo.connectionId, onFlush); | 692 chrome.experimental.serial.flush(connectionInfo.connectionId, onFlush); |
| 693 } | 693 } |
| 694 </pre> | 694 </pre> |
| 695 | 695 |
| 696 <p class="backtotop"><a href="#top">Back to top</a></p> | 696 <p class="backtotop"><a href="#top">Back to top</a></p> |
| OLD | NEW |