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
00039
00040 """@package docstring
00041 Module comModbusRtu: defines a class which communicates with Robotiq Grippers using the Modbus RTU protocol.
00042
00043 The module depends on pymodbus (http://code.google.com/p/pymodbus/) for the Modbus RTU client.
00044 """
00045
00046 from pymodbus.client.sync import ModbusSerialClient
00047 from math import ceil
00048
00049 class communication:
00050
00051 def __init__(self):
00052 self.client = None
00053
00054 def connectToDevice(self, device):
00055 """Connection to the client - the method takes the IP address (as a string, e.g. '192.168.1.11') as an argument."""
00056 self.client = ModbusSerialClient(method='rtu',port=device,stopbits=1, bytesize=8, baudrate=115200, timeout=0.2)
00057 if not self.client.connect():
00058 print "Unable to connect to %s" % device
00059 return False
00060 return True
00061
00062 def disconnectFromDevice(self):
00063 """Close connection"""
00064 self.client.close()
00065
00066 def sendCommand(self, data):
00067 """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)"""
00068
00069 if(len(data) % 2 == 1):
00070 data.append(0)
00071
00072
00073 message = []
00074
00075
00076 for i in range(0, len(data)/2):
00077 message.append((data[2*i] << 8) + data[2*i+1])
00078
00079
00080 self.client.write_registers(0x03E8, message, unit=0x0009)
00081
00082 def getStatus(self, numBytes):
00083 """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"""
00084 numRegs = int(ceil(numBytes/2.0))
00085
00086
00087
00088 response = self.client.read_holding_registers(0x07D0, numRegs, unit=0x0009)
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