beautypg.com

B&B Electronics MODSCAN32 - Manual User Manual

Page 47

background image

47

Visual Basic Example


The example Visual Basic application included with the ModScan32 distribution files is a simple take-off
on the ModScan application itself. The Form creates four array points, (one each for inputs, coils, input
registers, and holding registers). A timer is used to read and update the data once per second. Edit controls
are used to define the addresses as shown below:

Code for the example follows:


Public m_svr As IModSca

Dim PollHandle(4) As Integer

‘Handle for each of the four array points

Dim status(4) As Integer

‘Status return value for each ReadValue request

Dim SlaveDevice As Integer

‘All points use same slave address

Dim StartAddress(4) As Long

‘starting address for each array


Dim Modbus_Id(4) As Long

‘ prefix for selected address

Dim Counter As Integer
Dim temp As Integer
Dim Modbus_Addr As Long

‘qualified 5-digit modbus address





Private Sub coiladdress_Change()

‘ User has changed address for coil array
‘ recalculate 5-digit modbus address
‘ and ModifyPollRequest accordingly

If (IsNumeric(coiladdress.Text)) Then
StartAddress(1) = coiladdress.Text
Modbus_Addr = Modbus_Id(1) + StartAddress(1)
status(1) = m_svr.ModifyPollRequest(PollHandle(1), SlaveDevice, Modbus_Addr, 10)
StatusMsg (status(1))
End If

End Sub


Private Sub Device_Change()

‘ User has changed slave device
‘ Modify all four PollRequests

If (IsNumeric(Device.Text)) Then
SlaveDevice = Device.Text

Modbus_Addr = Modbus_Id(0) + StartAddress(0)
temp = m_svr.ModifyPollRequest(PollHandle(0), SlaveDevice, Modbus_Addr, 10)

Modbus_Addr = Modbus_Id(1) + StartAddress(1)
temp = m_svr.ModifyPollRequest(PollHandle(1), SlaveDevice, Modbus_Addr, 10)

Modbus_Addr = Modbus_Id(2) + StartAddress(2)
temp = m_svr.ModifyPollRequest(PollHandle(2), SlaveDevice, Modbus_Addr, 10)