Modbus Communications Example VB Code

The Watlow Modbus Commumications Example demonstrates the sort of routines necessary to communicate to a Watlow temperature controller through the Modbus protocol using a program written in Visual Basic. This example is written using VB version 6.0, but the concepts and sections of code could be adapted to any version of Visual Basic. Any code or routines used from this example should be thoroughly tested to ensure that it functions properly with your application.

 

This example program makes use of one subroutine and three functions to send and receive modbus information via a communications port. These four routines then in turn make use of several other functions, subroutines, and DLL references to actually get data in and out of the port with the correct timing and in the proper format. The four routines are:

Subroutine

Init_Comms – Initializes the Communications Port object.

Functions

build_modbus_message$ - Builds a modbus packet from the information in the text boxes and returns the formatted packet .

transmit$ - Transmits the information out of the Comm. Port and returns the received response.

convert_modbus_data$ - Converts to data from the modbus packet into usable information and returns that information.

 

 

init_comms(CommPortt%, CommBaudRatee%)

Used to create a Communication’s Port object that will be the port used by the program. The subroutine utilizes two arguments in defining this port. This routine contains code which would allow it to be used to define more than one port at the same time. Some of that code is commented out to fit the scope of this communication example, but could be added back in by an advanced programmer to create more sophisticated applications.

CommPortt%- The Comm Port number of the PC to be used. This would normally be COM1-COM4. The number passed to this routine needs to be an integer numbering from 1 to 16.

CommBaudRatee%- This is the Baud Rate to be established for the Communication Port. The value should be valid baud rate selection expressed as an integer value. Visual Basic allows for Baud Rates from 110 baud up to 256000 baud, but this routine limits baud rate from 110 baud to 56000 baud. Watlow controllers currently support varying ranges of baud rate from as low as 300 baud up to 19200 baud. See the Controller’s User’s Manual or Data Communications Manual for the baud rates available.

Top

 

build_modbus_message$(command_type%, addres%, registerr!, value!() )

This function takes the information that will make up a modbus packet, put it in packet format, perform 2’s complement on negative values, calculate the CRC to be added to the packet and then return the formatted packet as a string. All of the arguments need to be sent to the routine as integer values. This function has four arguments.

command_type% - The modbus command type. Some examples of command types and there values are: multiple read has a value of 3, single write has a value of 6, and loop back has a value of 8.

addres% - The modbus address that is used to address controllers on the bus. This can be a value anywhere between 0 and 247. A value of "0" is a special "broadcast" address that will be received by every controller on the bus and is used for write functions only. The controller does not send a response to this command.

registerr! - The modbus register of the controller to be accessed. This value should be the relative register number (relative numbers start at 0) not the absolute register number(absolute numbers start at 40001)

value!() – For a multiple read command, this array will contain the number of registers to be read in for value(0) or for a write command, it will contain the value to be written to the register in value(0). An "End of data values" pointer should be contain in value(1). This pointer is a value of -30000. It is established as a constant within the example program.

Top

transmit$(device%, message$, timeout!)

This function transmits a packet out of the Communications port and returns the response. First the selected port is opened and the message is sent out. Then the time-out loop is readied and the controller waits for a response. The character delay, which is based on baud rate, determines how long the program waits for each character in the packet. If a character doesn’t come within the allotted time or if there is no response at all, then the routine times out. Three arguments are associated with this routine.

device% - The device number of the Communications Port object to be opened. This number would be 0 in the scope of this application, but could be an alternate number in an advanced application using more than one port. In this application, a constant "PROTOCOL_MODBUS", has been defined to set this value to 0.

message$ - The modbus packet formatted as a string to be sent out to the controller. This would be the string returned by the build_modbus_message$ routine.

timeout! - This is an integer value used in timing by the application. It is set at a value of 2000. A constant, MAX_COMMS_TIMOUT_TIME, has been defined for this value. This value would not need to be changed in most applications.

Top

convert_modbus_data$( MESSAGE_RECEIVED as integer, Response$)

This functions takes the return string the transmit$ function, which contains the modbus packet of the response, and converts the data value into a decimal value. This

MESSAGE_RECEIVED - Specifies whether the packet was an incoming or outgoing packet.

Response$ - This is the packet to be processed.

Top