Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SPI-USB Adapter with OPC-N2 example does not work with any python version #70

Closed
tshu opened this issue Jun 22, 2018 · 20 comments
Closed

Comments

@tshu
Copy link

tshu commented Jun 22, 2018

Related to #69, running the example script for the device using python3 results in

Traceback (most recent call last):
  File "reviewTest.py", line 1, in <module>
    from usbiss.spi import SPI
ImportError: No module named 'usbiss'

Attempting to use python results in

ERROR:opc:Could not parse the fimrware version from ????????????????????????????????????????????????????????????
Traceback (most recent call last):
  File "/home/pi/py-opc/opc/__init__.py", line 77, in __init__
    self.firmware['version'] = int(re.findall("\d{3}", infostring)[-1])
IndexError: list index out of range
ERROR:opc:Could not parse the fimrware version from ?
Traceback (most recent call last):
  File "/home/pi/py-opc/opc/__init__.py", line 77, in __init__
    self.firmware['version'] = int(re.findall("\d{3}", infostring)[-1])
IndexError: list index out of range
WARNING:opc:Data transfer was incomplete

in addition to the device's fan continually running until unplugged.

@DancingQuanta
Copy link
Contributor

DancingQuanta commented Jun 23, 2018 via email

@DancingQuanta
Copy link
Contributor

DancingQuanta commented Jun 23, 2018

Ah. If you wanted python 3 to work then execute pip3 install pyusbiss. Both Python 2 and python 3 have different site-packages and consequently different pip.

@DancingQuanta
Copy link
Contributor

Your second issue after executing python2 appears to be a bad communication.
Can you connect your sensor to the adaptor and wait for a minute before executing the test-device.py script? We have numerous Github issues regarding the connection problems and one of the solutions was to wait between physically connecting the sensor and executing any commands.

@DancingQuanta
Copy link
Contributor

DancingQuanta commented Jun 25, 2018

Oh, your traceback does show that you successfully connected to your sensor.
The py-opc library attempts to communicate with your sensor a number of times before it succeeded. Your traceback shows two failed attempts and third attempt is successful. You can see the same index out of range error two times but it does not stop here because it managed to connect properly. Thus the test-device.py continues to run until it did something that triggers a WARNING.

Your real problem is the last line in the traceback WARNING:opc:Data transfer was incomplete. This was due to test-device.py executing the histogram command at

print (alpha.histogram())

This histogram can have issues because you have to wait a bit between executing histogram.

Debugging this problem is difficult because the traceback for the failed attempts pollutes the traceback of other exceptions and so made it hard to see what is the problem. There must be a way to not show the traceback from the failed connection attempts after successful connection.
Lastly the warning and error messages from the logger does not show where they comes from.

@dhhagan
Copy link
Owner

dhhagan commented Jun 25, 2018

@tshu Can you let me know which version of py-opc is installed (opc.__version__)? I thought I fixed several of these in the newest release after speaking with Alphasense on some of the issues. Thanks!

@tshu
Copy link
Author

tshu commented Jun 25, 2018

Great, so both python2 and python3 work with test-device.py after using pip3 and waiting after USB connection. I will occasionally fail to connect 2-3 times, but it will always run the script in the end. If there can be a note about the wait time in the readme, that would be great.

However, referring back to the original problem in this issue of running the example code from the readme, both python2 and python3 will attempt to connect 0-3 times before terminating with `WARNING:opc:Data transfer was incomplete'. Fan remains on. I can still run test-device.py to regain control of the device and have the fans turn off at the end of the script.

@dhhagan currently running 1.6.0

@DancingQuanta
Copy link
Contributor

DancingQuanta commented Jun 25, 2018

What is output of the following script (modified from readme)

import usbiss
from usbiss.spi import SPI
import opc
from time import sleep

spi = SPI("/dev/ttyACM0")

spi.mode = 1
spi.max_speed_hz = 500000

alpha = opc.OPCN2(spi, debug=True)

# Turn the opc ON
alphasense.on()

# Read the information string
print (alphasense.read_info_string())

# Turn the opc OFF
alphasense.off()

@tshu
Copy link
Author

tshu commented Jun 25, 2018

pi@raspberrypi:~/py-opc $ python3 reviewTest2.py 
Traceback (most recent call last):
  File "reviewTest2.py", line 14, in <module>
    alphasense.on()
NameError: name 'alphasense' is not defined
pi@raspberrypi:~/py-opc $ python reviewTest2.py 
Traceback (most recent call last):
  File "reviewTest2.py", line 14, in <module>
    alphasense.on()
NameError: name 'alphasense' is not defined

@dhhagan
Copy link
Owner

dhhagan commented Jun 25, 2018

@DancingQuanta @tshu It would appear the script/code above is incorrect - it should be alpha.on() since that is what the OPCN2 class instance was initiated as. I have just merged the previous readme changes into v1.6.1. Care to check?

@DancingQuanta
Copy link
Contributor

DancingQuanta commented Jun 25, 2018

Opps, I made a name mistake. Some corrections here

import usbiss
from usbiss.spi import SPI
import opc
from time import sleep

spi = SPI("/dev/ttyACM0")

spi.mode = 1
spi.max_speed_hz = 500000

alpha = opc.OPCN2(spi, debug=True)

# Turn the opc ON
alpha.on()

# Read the information string
print (alpha.read_info_string())

# Turn the opc OFF
alpha.off()

@tshu
Copy link
Author

tshu commented Jun 25, 2018

pi@raspberrypi:~/py-opc $ python reviewTest2.py 
ERROR:opc:Could not parse the fimrware version from ?
Traceback (most recent call last):
  File "/home/pi/py-opc/opc/__init__.py", line 77, in __init__
    self.firmware['version'] = int(re.findall("\d{3}", infostring)[-1])
IndexError: list index out of range
������������������������������������������������������������
pi@raspberrypi:~/py-opc $ python3 reviewTest2.py 
ERROR:opc:Could not parse the fimrware version from ?
Traceback (most recent call last):
  File "/home/pi/py-opc/opc/__init__.py", line 77, in __init__
    self.firmware['version'] = int(re.findall("\d{3}", infostring)[-1])
IndexError: list index out of range
������������������������������������������������������������

Getting some garbage outputs.

@DancingQuanta
Copy link
Contributor

DancingQuanta commented Jun 25, 2018

Could you replace print(alpha.read_info_string()) with print(repr(alpha.read_info_string())) and try again please? The function read_info_string() should return a string. repr() will show what type of data it is.

@tshu
Copy link
Author

tshu commented Jun 25, 2018

pi@raspberrypi:~/py-opc $ python reviewTest2.py 
ERROR:opc:Could not parse the fimrware version from ?
Traceback (most recent call last):
  File "/home/pi/py-opc/opc/__init__.py", line 77, in __init__
    self.firmware['version'] = int(re.findall("\d{3}", infostring)[-1])
IndexError: list index out of range
'\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01'
pi@raspberrypi:~/py-opc $ python3 reviewTest2.py 
ERROR:opc:Could not parse the fimrware version from ?
Traceback (most recent call last):
  File "/home/pi/py-opc/opc/__init__.py", line 77, in __init__
    self.firmware['version'] = int(re.findall("\d{3}", infostring)[-1])
IndexError: list index out of range
'\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01'

@DancingQuanta
Copy link
Contributor

DancingQuanta commented Jun 25, 2018

Your output shows that it is getting blank data from your sensor.
Can you add sleep(1) before each line containing alpha and run the script?
Also make sure to update your py-opc to v1.6.1.

@tshu
Copy link
Author

tshu commented Jun 25, 2018

That did the trick.

pi@raspberrypi:~/py-opc $ python3 reviewTest2.py 
'OPC-N2 FirmwareVer=OPC-018.2..............................BD'
pi@raspberrypi:~/py-opc $ python reviewTest2.py 
'OPC-N2 FirmwareVer=OPC-018.2..............................BD'

The only necessary sleep is between turnon and the read. The following code works:

import usbiss
from usbiss.spi import SPI
import opc
from time import sleep

spi = SPI("/dev/ttyACM0")

spi.mode = 1
spi.max_speed_hz = 500000

alpha = opc.OPCN2(spi, debug=True)

# Turn the opc ON
alpha.on()

sleep(1)
# Read the information string
print(repr(alpha.read_info_string()))

# Turn the opc OFF
alpha.off()

@dhhagan These edits can go in the readme examples.

@DancingQuanta
Copy link
Contributor

We probably need to improve alpha.on(). A sleep could be added to end of alpha.on(). Or have a timer in the library that track which command have been sent. So that the script itself can work without being slowed down until it execute another alpha command which then will invoke sleep with time period equal to alphasense's response then execute. Like a debt.

@dhhagan
Copy link
Owner

dhhagan commented Jun 26, 2018

@DancingQuanta Yea, I tried to avoid doing anything other than wrapping the SPI commands, but it seems that it is causing so much trouble for most users (myself included). I went ahead and added sleeps in the necessary places and will push a bit later on today as I have some free time.

@DancingQuanta
Copy link
Contributor

Can I suggest a decorator to wrap some methods to add sleep to beginning of a method if the method was called in the specified time period since last method called. Like a cool down. We know that a method such as on takes a while to make the device ready.
Every time a method such as on is called the time of this call is recorded. Next time a method (any method) is called, the new time is compared to old time and the difference is used to delay execution of called method.

This way the time spent waiting for sleep in each method to work would be less if we could use the time in between calls as part of waiting.

@dhhagan
Copy link
Owner

dhhagan commented Jun 27, 2018

@DancingQuanta I'll go ahead and merge a few PR's and push right now - maybe then take a look and see what you think is best? A decorator would be fine as you suggest - I think I just added a few sleeps. Give me 30 min or so.

@dhhagan
Copy link
Owner

dhhagan commented Jun 27, 2018

@tshu I've added some notes to the readme. Should be okay now?

@dhhagan dhhagan closed this as completed Jun 29, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants