Jump to content

As of July 17, 2015, the LabJack forums here at forums.labjack.com are shut down. New registrations, topics, and replies are disabled. All forums are in a read-only state for archive purposes.

Please visit our current forums at labjack.com/forums to view and make new posts. To post on the current forums, use your labjack.com login account. Your old LabJack forums login credentials have been retired. There are no longer separate logins for labjack.com and LabJack forums.


Photo

u3SampleAndLogToCloudDot

U9 modbus

  • Please log in to reply
5 replies to this topic

#1 catimossi

catimossi
  • Members
  • 3 posts

Posted 25 September 2014 - 02:36 PM

I've setup a U9 with the latest firmware and connected only with ethernet intending to use modbus for communication with a LabjackPython client. I took the example code u3SampleAndLogToCloudDot.py and modified it for the u9 to only write to a local disk (no cloud stuff). It runs fine for a period of 6 hours or so then silently stops writing to the local file. The python file is running on a mac mini os 10.6.8. Does anyone have an experience with the u3SampleAndLogToCloudDot.py that might provide insight to this failure?



#2 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 25 September 2014 - 04:54 PM

For general information on CloudDot, it is no longer in service and the example will probably be removed at some point from the current LabJackPython examples.

 

As for your issue, I'm not exactly sure as the example was intended for CloudDot and not tested for local files. Perhaps this is an issue with file size. Try logging to multiple files to keep file sizes smaller and easier to manage, and see if that helps. So something like (pseudocode):

#imports
...

#Set a value for MAX_DATA_PER_FILE
...

#Open UE9
...

#Open first file
fileName = "readings"
fileNum = 0
file = open(filename + str(fileNum), "w")

dataCnt = 0
while true:
    #Get data from UE9
    ...

    #Write data and increment data count for the current file.
    file.write(data)
    dataCnt += 1
    if dataCnt > MAX_DATA_PER_FILE:
        #Reached our maximum data limit for the file.
        #Reset dataCnt, close the file and open the next
        #file to write data to.
        dataCnt = 0
        fileNum += 1
        file.close()
        file = open(fileName + str(fileNum), "w")
...


#3 catimossi

catimossi
  • Members
  • 3 posts

Posted 25 September 2014 - 08:59 PM

I finally got a stack dump. The problem doesn't seem to be file size or time related (I've tried 3-30 second sample intervals). It appears to be a time-out waiting for a modbus response (I'm reading registers A11-A14). The code is pretty much the same as in the example except there is no cloud access just writing to stdout. Is there a way to handle this case in my code?

 

bash-3.2$ python sample.py > ~/Public/data11.txt
Traceback (most recent call last):
  File "sample.py", line 40, in <module>
    signal.pause()
  File "sample.py", line 22, in sampleAndPost
    reading = d.readRegister(MODBUS_REGISTER, numReg = MODBUS_REG_TO_READ)
  File "/Users/catimossi/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/LabJackPython.py", line 378, in readRegister
    response = self._modbusWriteRead(pkt, numBytes)
  File "/Users/catimossi/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/LabJackPython.py", line 512, in _modbusWriteRead
    result = self.read(numBytes, modbus = True)
  File "/Users/catimossi/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/LabJackPython.py", line 284, in read
    return self._readFromUE9TCPHandle(numBytes, stream, modbus)
  File "/Users/catimossi/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/LabJackPython.py", line 314, in _readFromUE9TCPHandle
    rcvString = self.handle.modbus.recv(numBytes)
socket.timeout: timed out



#4 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 26 September 2014 - 12:36 AM

Try increasing the timeout and see if that helps. LabJackPython defaults it to three seconds, but you can modify it like so before opening the UE9:

import UE9
import LabJackPython

LabJackPython.SOCKET_TIMEOUT = 6 #6 second socket timeout for the UE9 connection.
#open UE9

You can also catch the "socket.timeout" exception from the readRegister call, and retry the call when necessary. 



#5 catimossi

catimossi
  • Members
  • 3 posts

Posted 26 September 2014 - 10:08 AM

Thanks the socket timeout info was helpful. I'm trying to figure out how to handle the 'socket.timeout' exception. I don't see a 'raise' exception in the code for this but I'm no expert on exception handling!

# call this function to read modbus registers every read interval (e.g. every 10 seconds)
def sampleAndPost(*args):
    try:
        reading = d.readRegister(MODBUS_REGISTER, numReg = MODBUS_REG_TO_READ)
    except LabJackError as e:
        print "Missing sample because of a Labjack error: ({0}): {1}".format(e.errno, e.strerror)
    except:
        print "Missing sample. Unknown exception"



#6 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 26 September 2014 - 10:40 AM

It should look something like ("import socket" beforehand):

except socket.timeout:
    print "Missed sample because communications timed out."

This timeout is raised by the socket class that is used in LabJackPython.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users