comModbusTcp.py
Go to the documentation of this file.
00001 # Software License Agreement (BSD License)
00002 #
00003 # Copyright (c) 2012, Robotiq, Inc.
00004 # All rights reserved.
00005 #
00006 # Redistribution and use in source and binary forms, with or without
00007 # modification, are permitted provided that the following conditions
00008 # are met:
00009 #
00010 #  * Redistributions of source code must retain the above copyright
00011 #    notice, this list of conditions and the following disclaimer.
00012 #  * Redistributions in binary form must reproduce the above
00013 #    copyright notice, this list of conditions and the following
00014 #    disclaimer in the documentation and/or other materials provided
00015 #    with the distribution.
00016 #  * Neither the name of Robotiq, Inc. nor the names of its
00017 #    contributors may be used to endorse or promote products derived
00018 #    from this software without specific prior written permission.
00019 #
00020 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00023 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00024 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00025 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00026 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00028 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00029 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00030 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00031 # POSSIBILITY OF SUCH DAMAGE.
00032 #
00033 # Copyright (c) 2012, Robotiq, Inc.
00034 # Revision $Id$
00035 
00036 
00037 
00038 """@package docstring
00039 Module comModbusTcp: defines a class which communicates with Robotiq Grippers using the Modbus TCP protocol. 
00040 
00041 The module depends on pymodbus (http://code.google.com/p/pymodbus/) for the Modbus TCP client.
00042 """
00043 
00044 from pymodbus.client.sync import ModbusTcpClient
00045 from pymodbus.register_read_message import ReadInputRegistersResponse
00046 from math import ceil
00047 import time
00048 import threading
00049 
00050 class communication:
00051 
00052    def __init__(self):
00053       self.client = None
00054       self.lock = threading.Lock()
00055       
00056    def connectToDevice(self, address):
00057       """Connection to the client - the method takes the IP address (as a string, e.g. '192.168.1.11') as an argument."""
00058       self.client = ModbusTcpClient(address)
00059 
00060    def disconnectFromDevice(self):
00061       """Close connection"""
00062       self.client.close()
00063 
00064    def sendCommand(self, data):   
00065       """Send a command to the Gripper - the method takes a list of uint8 as an argument. The meaning of each variable depends on the Gripper model (see support.robotiq.com for more details)"""
00066       #make sure data has an even number of elements   
00067       if(len(data) % 2 == 1):
00068          data.append(0)
00069 
00070       #Initiate message as an empty list
00071       message = []
00072 
00073       #Fill message by combining two bytes in one register
00074       for i in range(0, len(data)/2):
00075          message.append((data[2*i] << 8) + data[2*i+1])
00076 
00077       #To do!: Implement try/except
00078       with self.lock:
00079          self.client.write_registers(0, message)
00080 
00081    def getStatus(self, numBytes):
00082       """Sends a request to read, wait for the response and returns the Gripper status. The method gets the number of bytes to read as an argument"""
00083       numRegs = int(ceil(numBytes/2.0))
00084 
00085       #To do!: Implement try/except 
00086       #Get status from the device
00087       with self.lock:
00088          response = self.client.read_input_registers(0, numRegs)
00089 
00090       #Instantiate output as an empty list
00091       output = []
00092 
00093       #Fill the output with the bytes in the appropriate order
00094       for i in range(0, numRegs):
00095          output.append((response.getRegister(i) & 0xFF00) >> 8)
00096          output.append( response.getRegister(i) & 0x00FF)
00097       
00098       #Output the result
00099       return output


robotiq_modbus_tcp
Author(s): Nicolas Lauzier (Robotiq inc.)
autogenerated on Thu Jun 6 2019 17:58:05