Serial Code Information From Pybbio - SaratogaEDD2014/RapidPrototyper GitHub Wiki
This page is now obsolete. We are using an arduino to communicate with our software from a desktop because we realized the beaglebone was probably not the best platform for our situation. Please refer to our PySerial and Arduino reference page.
The BeagleBone's processor includes six UART, or Universal Asynchronous Receiver/Transmitter, modules, four of which have their RX and TX lines broken out on the two expansion headers. There are many ways to approach a serial API, from a very simple one-byte-at-a-time reading/writing model, to a very robust and full featured model including support for the CTS and RTS hardware-handshake lines. To keep things simple, PyBBIO's serial API almost exactly mimics that of the Arduino environment (see here). This means that there is (at least for the time being) no support for the CTS and RTS lines, however it does provide friendly routines for reading and writing data, as well as a few extra helper methods.
The BeagleBone's UART modules use 3.3v logic levels; applying voltages greater than 3.3v to the UART pins can cause permanent damage!
Each of the four serial modules are accessed through pre-initialized objects named Serial1, Serial2, Serial4 and Serial5 (the UART3 RX and TX lines are not available). Each is used with the following methods by calling Serialx.method_name(). begin(baud, timeout=1)
Initializes the serial port at the given baud rate. The optional timeout argument is the number of seconds to wait when reading from the RX line before giving up if no data is present. If timeout = 0 then read() will block until data is received. This method must be called before any of the below methods will work.
end()
Closes the serial port. This should be called prior to reinitializing at a new baud rate.
available()
Returns the number of bytes available in the receiving buffer. read()
Returns the next byte from the receiving buffer, or -1 if the timeout set in begin() is reached before any data is present.
peek()
Returns the next byte from the receive buffer without removing it, or -1 if no data is available.
flush()
Removes all data from both the TX and RX buffers.
prints(data, base=None)
The equivalent of Arduino's Serial.print(), but the print symbol is reserved in Python. Prints the given data to the TX line. If the data is a string it is printed as-is. If it is an integer then it may be formatted by passing one of the formatting constants listed below as base, where the default is DEC. If it is a float then base should an integer of the number of decimal points to be printed, where the default is 2. If data is of any other type, e.g. a list or dictionary, it is converted to a string then written.
println(data, base=None)
This behaves the same as prints(), but the data is followed by a carriage return (\r) and a new line (\n).
write(data)
If data is a number it is written as the the least significant byte of its integer value. If it is a string, list or tuple, each value is written sequentially, left-to-right, following the same rule if they are numbers. Returns the number of bytes written, or 0 if given an unsupported type.
Formatting constants for prints() and println():
DEC - Send number as string of decimal value. BIN - Send number as string of binary value. OCT - Send number as string of octal value. HEX - Send number as string of hexadecimal value.
Available serial pins:
Header P8:
Serial5 TX - P8.37 Serial5 RX - P8.38
Header P9:
Serial1 TX - P9.24 Serial1 RX - P9.26
Serial2 TX - P9.21 Serial2 RX - P9.22
Serial4 TX - P9.13 Serial4 RX - P9.11
Last edited by alexanderhiam, a year ago