DancingQuanta / pyusbiss

Python API for USB-ISS Multifunction USB Communications Module
MIT License
2 stars 3 forks source link

HELP: What I am doing wrong here with SPI ? #17

Open abhatikar opened 3 years ago

abhatikar commented 3 years ago

Description

Describe what you were trying to get done. I am trying to read readings from OPC N2 sensor via SPI I am using the library https://github.com/dhhagan/py-opc which using this library to access the OPC N2.

Tell us what happened, what went wrong, and what you expected to happen. I am getting this error when I run the test

python test-device.py Traceback (most recent call last): File "test-device.py", line 10, in spi = SPI("/dev/ttyUSB0") File "/root/spi_dev/lib/python3.6/site-packages/usbiss/spi.py", line 16, in init self._usbiss = USBISS(port) File "/root/spi_dev/lib/python3.6/site-packages/usbiss/usbiss.py", line 63, in init self.get_iss_info() File "/root/spi_dev/lib/python3.6/site-packages/usbiss/usbiss.py", line 110, in get_iss_info raise USBISSError("Could not get version details") usbiss.usbiss.USBISSError: Could not get version details

What I Did

I have connected OPC n2 via spi to adafruit ft232H board. Connections OPC FT232H

5V <------> 5V GND <------> GND MOSI <------> D1 MISO <------> D2 SCK <------> D0 SS <------> C0

Paste the command(s) you ran and the output.
If there was a crash, please include the traceback here.
abhatikar commented 3 years ago

I tried this

(spi_dev) root@iotuser-Blade:~/tes_opc/py-opc# python
Python 3.6.9 (default, Jan 26 2021, 15:33:00) 
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from usbiss.usbiss import USBISS
>>> port = '/dev/ttyUSB0'
>>> cxn = USBISS(port)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/root/spi_dev/lib/python3.6/site-packages/usbiss/usbiss.py", line 63, in __init__
    self.get_iss_info()
  File "/root/spi_dev/lib/python3.6/site-packages/usbiss/usbiss.py", line 110, in get_iss_info
    raise USBISSError("Could not get version details")
usbiss.usbiss.USBISSError: Could not get version details
>>> 
DancingQuanta commented 3 years ago

Hi, thanks for trying to use my library. Unfortunately my library is NOT designed to work with the adafruit ft232H board. The library was written for USB-ISS board.

But there is a way you can use your board to work with OPC-N2 and its py-opc library.

You need to the pyftdi library to make a connection between your FT board and your PC. Please see this website and look at the section "CircuitPython on Personal Computers" to learn how to use pyftdi.

Then you need to write a class that py-opc can use to interface with your board. Below is an example class that you must fill in yourself. The key thing is this class must pretend to be spidev class that py-opc expects.

class SPI(object):
    """SPI class that open a connection to ftdi device and behaves like spidev class.
    """

    def __init__(self, args):
        # Setup connection to FT board, the arguments to this function must go to pyftdi to open a port
        self.cnxn = 

    def xfer(self, data):
        """
        Perform a SPI transection over pyftdi port. The spidev send a number of bytes in and get same number of bytes back (conveyed belt). This function must do the same.
        :param data: List of bytes
        :returns: List of bytes
        :rtype: List of bytes
        """
        return self.connection.func() # What function of pyftdi you need to send and receive data?

And then use the following code to make a connection

spi = SPI(args)
alphasense = opc.OPCN2(spi)

These are just example code and are not tested. I have not used the FT board or pyftdi before so I cannot help further. Do check back if you need some more pointers though.

abhatikar commented 3 years ago

@DancingQuanta Thanks for detailed explanation. Let me study your answer, looks interesting..! Thanks a lot.

DancingQuanta commented 3 years ago

Not a problem. Do feel free to ask me more questions if you have any. Try and see this as a learning opportunity!

On Fri, 14 May 2021, 20:02 Abhijeet Bhatikar, @.***> wrote:

@DancingQuanta https://github.com/DancingQuanta Thanks for detailed explanation. Let me study your answer, looks interesting..! Thanks a lot.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/DancingQuanta/pyusbiss/issues/17#issuecomment-841442598, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACAHOVIQ6GZD357BF7Y7CYTTNVXVXANCNFSM442JNQJQ .

abhatikar commented 3 years ago

@DancingQuanta Check this out. Refer to the limitation section. Since this OPC N2 is a SPI mode 1 device, do you think it will work with the FT232H hardware ?

DancingQuanta commented 3 years ago

Well that is disappointing your FT232H will not be able to communicate with your OPC-N2. Sorry about that. This shows that the protocols such as SPI are more complicated than we can expect as they can come in different varieties. If you are needing a adapter to connect to a PC you can use different devices such as USB-ISS which this library was developed for or a Raspberry Pi with spidev library. The key thing is to read the datasheet of the OPC-N2 and SPI adapter to ensure that they have the same SPI mode.

abhatikar commented 3 years ago

I will move to using the RPI directly with the OPC N2 and gather data. Will let you know how I progress.