Configuration

SpiDev.open(bus, device)

Open the specified SPI device on the given bus.

The bus number identifies the SPI port to which the peripheral device is connected, and the device number indicates the chip select line that is used specifically for the peripheral device.

For example, for the Quanser Mechatronic Sensors Trainer USB on Windows, its SPI port is typically 0 (so bus 0). To use its SPI CS line for the chip select on its user header, the device number is 4 (the four digital lines are 0..3 and one of them could also have been used as a chip select).

Parameters
  • bus (int) – An integer representing the bus number, which identifies the SPI port used.

  • device (int) – An integer representing the device number, which identifies the pin used for the chip select.

Raises

StreamError – Raised on an error in the underlying Quanser Stream API. A suitable error message may be retrieved using get_error_message.

Example

>>> import quanser.communications.spidev as spidev
>>>
>>> spi = spidev.SpiDev()
>>> spi.open(0, 4) # Bus 0, Device 4
>>> spi.max_speed_hz = 5000000 # 5 MHz
>>> spi.mode = 0b00 # L3G4200D uses SPI mode 0
>>> try:
>>>     ...
>>> finally:
>>>     spi.close()
SpiDev.open_path(path)

Open the SPI device at the indicated path.

The path is typicallly /dev/spidev<bus>.<device> where <bus> is the bus number and <device> is the device number, such as /dev/spidev0.0.

The bus number identifies the SPI port to which the peripheral device is connected, and the device number indicates the chip select line that is used specifically for the peripheral device.

For example, for the Quanser Mechatronic Sensors Trainer USB on Windows, its SPI port is typically 0 (so bus 0). To use its SPI CS line for the chip select on its user header, the device number is 4 (the four digital lines are 0..3 and one of them could also have been used as a chip select).

Parameters

path (string) – A string containing the path to the spidev device, such as “/dev/spidev0.4”.

Raises

StreamError – Raised on an error in the underlying Quanser Stream API. A suitable error message may be retrieved using get_error_message.

Example

>>> import quanser.communications.spidev as spidev
>>>
>>> spi = spidev.SpiDev()
>>> spi.open_path("/dev/spidev0.4") # Bus 0, Device 4
>>> spi.max_speed_hz = 5000000 # 5 MHz
>>> spi.mode = 0b00 # L3G4200D uses SPI mode 0
>>> try:
>>>     ...
>>> finally:
>>>     spi.close()
SpiDev.close()

Closes the SPI device.

Raises

StreamError – Raised on an error in the underlying Quanser Stream API. A suitable error message may be retrieved using get_error_message.

Example

>>> import quanser.communications.spidev as spidev
>>>
>>> spi = spidev.SpiDev()
>>> spi.open(0, 4) # Bus 0, Device 4
>>> spi.max_speed_hz = 5000000 # 5 MHz
>>> spi.mode = 0b00 # device uses SPI mode 0
>>> try:
>>>     ...
>>> finally:
>>>     spi.close()