In this tutorial, we will explore the scripting capabilities of Roboteq drives to interface with a Torque meter that supports the Modbus protocol. Typically, such communication would necessitate a Modbus Master capable of reading and writing the required registers. Although Roboteq controllers cannot natively function as fieldbus master devices, the Raw redirect function enables the construction, sending, and reading of raw RS485 frames transmitted through the RS485 bus. This allows for the implementation of a simplified version of the Modbus protocol to exchange data with the sensor. 


Torque Meter

The Torque Meter provides the torque value in Nm with a resolution of two decimal places upon receiving a read request. The following information must be referenced in the Torque Sensor's manual to establish the Modbus protocol and to request the torque meter value:





Baud Rate


Roboteq drive and the torque meter must be configured with the same baud rate. The default value for the torque meter’s Baud Rate according to the datasheet is 38400 

Slave ID


The default slave ID of the torque meter, which is required to implement the Modbus protocol. 

Torque Parameter Address


This is the address of the holding register that will return the torque meter value.



With this information, we can construct the following Raw RS-485 frame to request the torque value:


Byte 0

Byte 1

Byte 2

Byte 3

Byte 4

Byte 5

Byte 6

Byte 7










The explanation of each byte follows:    

Byte 0: The node ID of the slave.

Byte 1: The read Holding Register function according to Modbus protocol.

Byte 2: The torque value Holding register ID

Byte 3: Not used in read requests.

Byte 4: Not used in read requests.

Byte 5: Not used in read requests.

Byte 6: The CRC less significant byte 

Byte 7: The CRC most significant byte


The torque meter should reply with a frame having the following format: 


Byte 0

Byte 1

Byte 2

Byte 3

Byte 4

Byte 5

Byte 6

Byte 7

Byte 8











The explanation of each byte follows:      

Byte 0: The node ID of the slave.

Byte 1: The read Holding Register function according to Modbus protocol.

Byte 2: The number of bytes that are being returned.

Byte 3 to 6: The bytes of the Torque value.

Byte 7: The CRC less significant byte. Calculated according to the return values.

Byte 8: The CRC most significant byte. Calculated according to the return values.


Hardware Connections


The hardware connections are illustrated in the following diagram. The torque meter requires 24V, so separate power supplies will be used to power the sensor and the drive. These two power supplies must share a common ground. Please note that this connection should not be made through the GND pin of the drive’s D-sub connector, as permanent damage may occur in the event of disconnection of the main battery’s negative lead. Also, be aware that the RS485A pin of the sensor should be connected to the RS485+ pin of the drive, and the RS485B pin of the sensor to the RS485- pin of the drive. The symbols A, B, +, and – are used interchangeably.


Roborun+ Configurations


The next steps should be followed to enable the Raw redirect on the utility: 

1. Set the serial port’s baud rate. In our case it is 38400.

A screen shot of a computer

Description automatically generated

2. Enable the Raw redirect to the RS485 port.

A screenshot of a computer

Description automatically generated

3. Set the Modbus mode to RS-485 RTU

A screenshot of a computer

Description automatically generated


4. Set the motor torque constant. This parameter will be used by the drive to calculate the motor torque from the measured motor current. Later, the calculated torque can be compared with the measured torque of the sensor to ensure accuracy and consistency in performance monitoring.


A screenshot of a computer

Description automatically generated


MicroBasic Script


The script will implement a simplified version of the Modbus protocol. It will construct and send the required frame to read the torque data. Upon receiving a reply, the script will read the received frame, extract the data bytes, and convert the float value to an integer so it can be displayed on Roborun. The CRC value of the sent frame is predefined based on the frame data, and it will not be calculated by the script. Additionally, the CRC of the received frame will not be checked. However, the script will perform a basic evaluation of the received data to ensure that the node ID of the slave, the function code, and the number of data bytes are correct; otherwise, the frame will be rejected.

The script can be parameterized by modifying the sensor ID (SENSOR_ID), torque register address (TORQUE_ADDR), and the two variables where the measured torque from the sensor and the calculated value from the Roboteq drive will be stored (SENS_VAR and TRQ_VAR).

For more information about the Raw Redirect implementation refer to the respective article.


The script follows: 




'              ATO torque sensor Interface script             | v.1




'Sends a Raw redirect frame to read the sensor's torque data.

'Performs all the necessary convertions and adds the data to 

'the required VAR




' Sensor's default baud rate: 57600

' Senror's default Id: 0x01


' Instructions:


' Enable the raw redirect on Roborun for 485 and 

' enable the modbus475 RTU. Set the serial baud rate to match 

' the sensor's baud rate



option explicit 


#define SENS_VAR          1     'The number of VAR where the actual torque value wil be stored

#define TRQ_VAR            2     'The number of VAR where the drive's measured torque will be stored 

#define SENSOR_ID         0x01 

#define TORQUE_ADDR        0x00 'The addres of the Torque register

#define COMMAND_TYPE     0x03 'Read request


'Below is the required frame to send, to read the Torque value

'CRC is set constanlty according to the sent frame as 0xOBC4


'0x01  | 0x03  | 0x00  | 0x00   | 0x00  | 0x02  | 0xC4 | 0x0B



dim inputBuffer[64as integer     ' Readf up to 64 bytes

dim byte[16as integer             ' Send up to 16 bytes

dim incomingFrameLength as integer 

dim as integer

dim torque as integer


'Frame construction


byte[0] = SENSOR_ID

byte[1] = COMMAND_TYPE

byte[2] = TORQUE_ADDR

byte[3] = 0

byte[4] = 0

byte[5] = 2

byte[6] = 0xC4

byte[7] = 0x0B



setconfig(_SCRO,2'set the script output to USB for debugging. In case it was set to something else.




    gosub Send_MODBUS_RTU_Frame                             ' Read request

    wait(20)                                              ' 50 Hz update rate

    gosub Receive_MODBUS_RTU_Frame                          ' Receive the data

    setcommand(_VAR,SENS_VAR,torque)                        'First var will have the torque sensor value

    setcommand(_VAR,TRQ_VAR,-getvalue(_TRQ,1)*707/1000)     'Second var will have the drive's measured torque value, converted to rms     

goto top





' Send the data with CU function











' Data will be automatically sent after The frame length is set


    setcommand(_CU16)     ' for RS485

    setcommand(_CU20x08' set frame length and Tx frame







    if (getValue(_CD,1)) ' CD returns the number of bytes awaiting on the input buffer and copies them to the read buffer.


        incomingFrameLength getValue(_DDT,1)' get the frame size


        for andwhile incomingFrameLength 


            inputBuffer[i] = getValue(_DDT,i+2'update the input buffer with the data bytes




        if ((inputBuffer[2] = 0x04and (inputBuffer[1] = COMMAND_TYPE and (inputBuffer[0] = SENSOR_ID)) 'A very basic test to confirm the details of the sender


            torque inputBuffer[3]<<24 or inputBuffer[4]<<16 or inputBuffer[5]<<or inputBuffer[6'shift the bytes to add them to the same torque variable


'             print(torque) 'Torque value for debugging

'             print("\n")


        end if


    end if