pyb.USB_VCP.rst 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. .. currentmodule:: pyb
  2. .. _pyb.USB_VCP:
  3. class USB_VCP -- USB virtual comm port
  4. ======================================
  5. The USB_VCP class allows creation of a `stream`-like object representing the USB
  6. virtual comm port. It can be used to read and write data over USB to
  7. the connected host.
  8. Constructors
  9. ------------
  10. .. class:: pyb.USB_VCP()
  11. Create a new USB_VCP object.
  12. Methods
  13. -------
  14. .. method:: USB_VCP.setinterrupt(chr)
  15. Set the character which interrupts running Python code. This is set
  16. to 3 (CTRL-C) by default, and when a CTRL-C character is received over
  17. the USB VCP port, a KeyboardInterrupt exception is raised.
  18. Set to -1 to disable this interrupt feature. This is useful when you
  19. want to send raw bytes over the USB VCP port.
  20. .. method:: USB_VCP.isconnected()
  21. Return ``True`` if USB is connected as a serial device, else ``False``.
  22. .. method:: USB_VCP.any()
  23. Return ``True`` if any characters waiting, else ``False``.
  24. .. method:: USB_VCP.close()
  25. This method does nothing. It exists so the USB_VCP object can act as
  26. a file.
  27. .. method:: USB_VCP.read([nbytes])
  28. Read at most ``nbytes`` from the serial device and return them as a
  29. bytes object. If ``nbytes`` is not specified then the method reads
  30. all available bytes from the serial device.
  31. USB_VCP `stream` implicitly works in non-blocking mode,
  32. so if no pending data available, this method will return immediately
  33. with ``None`` value.
  34. .. method:: USB_VCP.readinto(buf, [maxlen])
  35. Read bytes from the serial device and store them into ``buf``, which
  36. should be a buffer-like object. At most ``len(buf)`` bytes are read.
  37. If ``maxlen`` is given and then at most ``min(maxlen, len(buf))`` bytes
  38. are read.
  39. Returns the number of bytes read and stored into ``buf`` or ``None``
  40. if no pending data available.
  41. .. method:: USB_VCP.readline()
  42. Read a whole line from the serial device.
  43. Returns a bytes object containing the data, including the trailing
  44. newline character or ``None`` if no pending data available.
  45. .. method:: USB_VCP.readlines()
  46. Read as much data as possible from the serial device, breaking it into
  47. lines.
  48. Returns a list of bytes objects, each object being one of the lines.
  49. Each line will include the newline character.
  50. .. method:: USB_VCP.write(buf)
  51. Write the bytes from ``buf`` to the serial device.
  52. Returns the number of bytes written.
  53. .. method:: USB_VCP.recv(data, \*, timeout=5000)
  54. Receive data on the bus:
  55. - ``data`` can be an integer, which is the number of bytes to receive,
  56. or a mutable buffer, which will be filled with received bytes.
  57. - ``timeout`` is the timeout in milliseconds to wait for the receive.
  58. Return value: if ``data`` is an integer then a new buffer of the bytes received,
  59. otherwise the number of bytes read into ``data`` is returned.
  60. .. method:: USB_VCP.send(data, \*, timeout=5000)
  61. Send data over the USB VCP:
  62. - ``data`` is the data to send (an integer to send, or a buffer object).
  63. - ``timeout`` is the timeout in milliseconds to wait for the send.
  64. Return value: number of bytes sent.