Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
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
00067 if(len(data) % 2 == 1):
00068 data.append(0)
00069
00070
00071 message = []
00072
00073
00074 for i in range(0, len(data)/2):
00075 message.append((data[2*i] << 8) + data[2*i+1])
00076
00077
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
00086
00087 with self.lock:
00088 response = self.client.read_input_registers(0, numRegs)
00089
00090
00091 output = []
00092
00093
00094 for i in range(0, numRegs):
00095 output.append((response.getRegister(i) & 0xFF00) >> 8)
00096 output.append( response.getRegister(i) & 0x00FF)
00097
00098
00099 return output