Inconsistence between documentation example and actual API
Closed this issue · 0 comments
T-TROUCHKINE commented
Hello,
I was testing the API with the first code example available here : https://donjonscaffold.readthedocs.io/en/latest/getting_started.html
The exact code in the documentation is:
from scaffold import Scaffold
# Connect to the board.
# This will open the serial device and check for hardware version
scaffold = Scaffold('/dev/ttyUSB1')
# Configure UART0 for operation
uart = scaffold.uart0
uart.baudrate = 115200
# Connect UART0 signals to board pins
# << operator connects signals. Left operand is the destination, right operand
# is the source (the order is important)
# In this example, D0 and D1 are configured as outputs, D2 is configured as an
# input.
scaffold.d0 << uart.tx
scaffold.d1 << uart.trigger
uart.rx << scaffold.d2
# UART is now ready to use
uart.send('Hello world !')
which gave me this error:
Traceback (most recent call last):
File "/home/kerzas/Documents/Dev/Python/scaffold_board/main.py", line 8, in <module>
uart = scaff.uart0
NameError: name 'scaff' is not defined
I corrected it by modifying the line 8 such as:
uart = scaffold.uart0
Then I had a new error:
Traceback (most recent call last):
File "/home/kerzas/Documents/Dev/Python/scaffold_board/main.py", line 21, in <module>
uart.send('Hello world !')
AttributeError: 'UART' object has no attribute 'send'
After reading the API, the correct function to use seems to be transmit()
, so I modified the line 21 to:
uart.transmit('Hello world !')
Then I had a new error:
Traceback (most recent call last):
File "/home/kerzas/Documents/Dev/Python/scaffold_board/main.py", line 21, in <module>
uart.transmit('Hello world !')
File "/home/kerzas/.local/lib/python3.9/site-packages/scaffold/__init__.py", line 698, in transmit
self.reg_data.write(
File "/home/kerzas/.local/lib/python3.9/site-packages/scaffold/__init__.py", line 368, in write
self.__parent.bus.write(
File "/home/kerzas/.local/lib/python3.9/site-packages/scaffold/__init__.py", line 2001, in write
datagram += data[offset:offset + chunk_size]
TypeError: can't concat str to bytearray
I solved this by encoding the string using the encode()
function. So I modified to line 21 to:
uart.transmit('Hello world !'.encode())
Then the script executed smoothly and the LED was blinking =)
Therefore I suggest to update the documentation here https://donjonscaffold.readthedocs.io/en/latest/getting_started.html with the following code:
from scaffold import Scaffold
# Connect to the board.
# This will open the serial device and check for hardware version
scaffold = Scaffold('/dev/ttyUSB1')
# Configure UART0 for operation
uart = scaffold.uart0
uart.baudrate = 115200
# Connect UART0 signals to board pins
# << operator connects signals. Left operand is the destination, right operand
# is the source (the order is important)
# In this example, D0 and D1 are configured as outputs, D2 is configured as an
# input.
scaffold.d0 << uart.tx
scaffold.d1 << uart.trigger
uart.rx << scaffold.d2
# UART is now ready to use
uart.transmit('Hello world !'.encode())
Regards !