cModbusSlave ��ο�
? �����
cModbusSlave �� Modbus ��վ����������ʵ���࣬�ṩ�������ӡ�������վ����ά�����ݴ洢�ȹ��ܡ�
? �¼��б�
| �¼��� | ����ʱ�� | ���� |
|---|---|---|
OnStarted | ���������� | �� |
OnStopped | ������ֹͣ | �� |
OnClientConnect | �ͻ������ӣ�TCP ģʽ�� | ClientID (�ͻ��� ID), RemoteAddress (Զ�̵�ַ) |
OnClientDisconnect | �ͻ��˶Ͽ���TCP ģʽ�� | ClientID (�ͻ��� ID), Reason (�Ͽ�ԭ��) |
OnReadRequest | �յ���ȡ���� | ClientID, FunctionCode, Address, Quantity |
OnWriteRequest | �յ�д������ | ClientID, FunctionCode, Data |
OnError | �������� | Description (��������) |
OnDataReceived | �յ����ݣ������ã� | ClientID, Data() (�ֽ�����) |
? ���Բο�
ProtocolType - ������
����: ModbusSlaveProtocolType (ö��)
��д: ��д
ֵ:
| ���� | ֵ | ˵�� |
|---|---|---|
MB_SLAVE_PROTOCOL_RTU | 1 | RTU ģʽ������ͨ�ţ� |
MB_SLAVE_PROTOCOL_TCP | 2 | TCP ģʽ������ͨ�ţ� |
ʾ��:
' ����Ϊ TCP ģʽ
mbSlave.ProtocolType = MB_SLAVE_PROTOCOL_TCP
' ����Ϊ RTU ģʽ
mbSlave.ProtocolType = MB_SLAVE_PROTOCOL_RTUState - ������״̬
����: ModbusSlaveState (ö��)
��д: ֻ��
ֵ:
| ���� | ֵ | ˵�� |
|---|---|---|
MB_SLAVE_STATE_STOPPED | 0 | ��ֹͣ |
MB_SLAVE_STATE_STARTING | 1 | �������� |
MB_SLAVE_STATE_RUNNING | 2 | ������ |
MB_SLAVE_STATE_ERROR | 3 | ����״̬ |
ʾ��:
If mbSlave.State = MB_SLAVE_STATE_RUNNING Then
Debug.Print "������������"
End IfSlaveID - ��վ ID
����: Byte
��д: ��д
˵��: ��վ�豸��ַ��1-247����0 ��ʾ�㲥��ַ��
ʾ��:
mbSlave.SlaveID = 1Defaults - Ĭ�ϳ���
����: ModbusSlaveDefaults (�ṹ��)
��д: ֻ��
ʾ��:
Debug.Print "Ĭ�϶˿�: " & mbSlave.Defaults.TCP_PORTRTU ģʽ����
SerialPort - ��������
����: String
��д: ��д
ʾ��:
mbSlave.SerialPort = "COM1"BaudRate - ������
����: Long
��д: ��д
ʾ��:
mbSlave.BaudRate = 9600DataBits - ����λ
����: Long
��д: ��д
ʾ��:
mbSlave.DataBits = 8Parity - У��λ
����: String
��д: ��д
ֵ: "N" (��), "E" (żУ��), "O" (��У��)
ʾ��:
mbSlave.Parity = "N" ' ��У��StopBits - ֹͣλ
����: Long
��д: ��д
ʾ��:
mbSlave.StopBits = 1TCP ģʽ����
Port - �����˿�
����: Long (ֻ��)
��д: ֻ��
˵��: ��ȡ��ǰ�����Ķ˿ڡ�
BindAddress - ������ַ
����: String
��д: ��д
˵��: TCP ������ַ���ã�v1.1.0+����֧������ȡֵ��
"0.0.0.0"����ַ��� - ������������ӿڣ�Ĭ�ϣ�"127.0.0.1"- ���������ػػ���ַ"192.168.1.100"- ����ָ������ӿ�
ʾ��:
' �������������ӣ�����ȫ��
mbSlave.BindAddress = "127.0.0.1"
mbSlave.Start 502
' ������������ӿڣ�Ĭ�ϣ�
mbSlave.BindAddress = "0.0.0.0"
mbSlave.Start 502
' �� Start ������ָ��������ַ
mbSlave.Start 502, "192.168.1.100"ClientCount - ���ӵĿͻ�����
����: Long (ֻ��)
��д: ֻ��
˵��: TCP ģʽ�������ӵĿͻ���������
ʾ��:
Debug.Print "��ǰ������: " & mbSlave.ClientCount? �����ο�
Start - ����������
�:
Public Sub Start(Optional ByVal PortOrSerial As String = "", Optional ByVal BindAddress As String = "")����:
| ���� | ���� | ˵�� |
|---|---|---|
PortOrSerial | String (��ѡ) | TCP ģʽ���˿ںţ�RTU ģʽ���������� |
BindAddress | String (��ѡ) | TCP ģʽ��������ַ���� "127.0.0.1"����RTU ģʽ���Դ˲�����v1.1.0+�� |
ʾ��:
' TCP ģʽ - ����������
mbSlave.ProtocolType = MB_SLAVE_PROTOCOL_TCP
mbSlave.SlaveID = 1
mbSlave.Start 502
' TCP ģʽ - ָ��������ַ
mbSlave.Start 502, "127.0.0.1" ' ����������
mbSlave.Start 502, "0.0.0.0" ' �������нӿ�
' RTU ģʽ - ����������
mbSlave.ProtocolType = MB_SLAVE_PROTOCOL_RTU
mbSlave.SerialPort = "COM1"
mbSlave.BaudRate = 9600
mbSlave.DataBits = 8
mbSlave.Parity = "N"
mbSlave.StopBits = 1
mbSlave.SlaveID = 1
mbSlave.Start "COM1"StopMe - ֹͣ������
�:
Public Sub StopMe()˵��: �� v1.1.0 ��ʼ��Stop ��������Ϊ StopMe�������� VB �ؼ��ֳ�ͻ��
ʾ��:
mbSlave.StopMeSetCoil - ������Ȧֵ
�:
Public Sub SetCoil(ByVal Address As Long, ByVal Value As Boolean)����:
| ���� | ���� | ˵�� |
|---|---|---|
Address | Long | ��Ȧ��ַ (0-1990) |
Value | Boolean | ��Ȧֵ |
ʾ��:
mbSlave.SetCoil 0, True
mbSlave.SetCoil 1, FalseGetCoil - ��ȡ��Ȧֵ
�:
Public Function GetCoil(ByVal Address As Long) As Boolean����:
| ���� | ���� | ˵�� |
|---|---|---|
Address | Long | ��Ȧ��ַ |
����ֵ: Boolean - ��Ȧֵ
ʾ��:
Dim bValue As Boolean
bValue = mbSlave.GetCoil(0)
Debug.Print "Coil[0] = " & bValueSetDiscreteInput - ������ɢ����ֵ
�:
Public Sub SetDiscreteInput(ByVal Address As Long, ByVal Value As Boolean)����:
| ���� | ���� | ˵�� |
|---|---|---|
Address | Long | ��ɢ�����ַ (0-2000) |
Value | Boolean | ��ɢ����ֵ |
ʾ��:
mbSlave.SetDiscreteInput 0, TrueGetDiscreteInput - ��ȡ��ɢ����ֵ
�:
Public Function GetDiscreteInput(ByVal Address As Long) As Boolean����:
| ���� | ���� | ˵�� |
|---|---|---|
Address | Long | ��ɢ�����ַ |
����ֵ: Boolean - ��ɢ����ֵ
ʾ��:
Dim bValue As Boolean
bValue = mbSlave.GetDiscreteInput(0)SetHoldingRegister - ���ñ��ּĴ���ֵ
�:
Public Sub SetHoldingRegister(ByVal Address As Long, ByVal Value As Integer)����:
| ���� | ���� | ˵�� |
|---|---|---|
Address | Long | �Ĵ�����ַ (0-125) |
Value | Integer | �Ĵ���ֵ (16λ) |
ʾ��:
mbSlave.SetHoldingRegister 0, 1234
mbSlave.SetHoldingRegister 1, 5678GetHoldingRegister - ��ȡ���ּĴ���ֵ
�:
Public Function GetHoldingRegister(ByVal Address As Long) As Integer����:
| ���� | ���� | ˵�� |
|---|---|---|
Address | Long | �Ĵ�����ַ |
����ֵ: Integer - �Ĵ���ֵ
ʾ��:
Dim iValue As Integer
iValue = mbSlave.GetHoldingRegister(0)
Debug.Print "Reg[0] = " & iValueSetInputRegister - ��������Ĵ���ֵ
�:
Public Sub SetInputRegister(ByVal Address As Long, ByVal Value As Integer)����:
| ���� | ���� | ˵�� |
|---|---|---|
Address | Long | �Ĵ�����ַ (0-125) |
Value | Integer | �Ĵ���ֵ (16λ) |
ʾ��:
mbSlave.SetInputRegister 0, 1234GetInputRegister - ��ȡ����Ĵ���ֵ
�:
Public Function GetInputRegister(ByVal Address As Long) As Integer����:
| ���� | ���� | ˵�� |
|---|---|---|
Address | Long | �Ĵ�����ַ |
����ֵ: Integer - �Ĵ���ֵ
ʾ��:
Dim iValue As Integer
iValue = mbSlave.GetInputRegister(0)ClearAllData - �����������
�:
Public Sub ClearAllData()ʾ��:
mbSlave.ClearAllData? �¼����
OnStarted - ����������
�:
Event OnStarted()ʾ��:
Private Sub mbSlave_OnStarted()
Debug.Print "������������"
lblStatus.Caption = "������"
cmdStart.Enabled = False
cmdStop.Enabled = True
End SubOnStopped - ������ֹͣ
�:
Event OnStopped()ʾ��:
Private Sub mbSlave_OnStopped()
Debug.Print "��������ֹͣ"
lblStatus.Caption = "��ֹͣ"
cmdStart.Enabled = True
cmdStop.Enabled = False
End SubOnClientConnect - �ͻ�������
�:
Event OnClientConnect(ByVal ClientID As String, ByVal RemoteAddress As String)����:
| ���� | ���� | ˵�� |
|---|---|---|
ClientID | String | �ͻ��� ID |
RemoteAddress | String | Զ�̵�ַ |
ʾ��:
Private Sub mbSlave_OnClientConnect(ByVal ClientID As String, ByVal RemoteAddress As String)
Debug.Print "�ͻ�������: " & ClientID & " (" & RemoteAddress & ")"
lstClients.AddItem ClientID & " - " & RemoteAddress
End SubOnClientDisconnect - �ͻ��˶Ͽ�
�:
Event OnClientDisconnect(ByVal ClientID As String, ByVal Reason As String)����:
| ���� | ���� | ˵�� |
|---|---|---|
ClientID | String | �ͻ��� ID |
Reason | String | �Ͽ�ԭ�� |
ʾ��:
Private Sub mbSlave_OnClientDisconnect(ByVal ClientID As String, ByVal Reason As String)
Debug.Print "�ͻ��˶Ͽ�: " & ClientID & " - " & Reason
Dim i As Long
For i = 0 To lstClients.ListCount - 1
If InStr(lstClients.List(i), ClientID) > 0 Then
lstClients.RemoveItem i
Exit For
End If
Next i
End SubOnReadRequest - ��ȡ����
�:
Event OnReadRequest(ByVal ClientID As String, ByVal FunctionCode As ModbusSlaveFunctionCode, ByVal Address As Long, ByVal Quantity As Long)˵��: ����վ���Ͷ�ȡ����ʱ������
ʾ��:
Private Sub mbSlave_OnReadRequest(ByVal ClientID As String, ByVal FunctionCode As ModbusSlaveFunctionCode, ByVal Address As Long, ByVal Quantity As Long)
Debug.Print "��ȡ����: " & FunctionCode & ", ��ַ=" & Address & ", ����=" & Quantity
' ���ݹ����봦��
Select Case FunctionCode
Case MB_SLAVE_FC_READ_COILS
Debug.Print "��ȡ��Ȧ"
Case MB_SLAVE_FC_READ_HOLDING_REGISTERS
Debug.Print "��ȡ���ּĴ���"
End Select
End SubOnWriteRequest - �����
�:
Event OnWriteRequest(ByVal ClientID As String, ByVal FunctionCode As ModbusSlaveFunctionCode, ByVal Address As Long, ByRef Data As Variant)˵��: ����վ����д������ʱ������
ʾ��:
Private Sub mbSlave_OnWriteRequest(ByVal ClientID As String, ByVal FunctionCode As ModbusSlaveFunctionCode, ByVal Address As Long, ByRef Data As Variant)
Debug.Print "д������: " & FunctionCode & ", ��ַ=" & Address
' ���ݹ����봦��
Select Case FunctionCode
Case MB_SLAVE_FC_WRITE_SINGLE_REGISTER
Debug.Print "д�뵥���Ĵ���"
Case MB_SLAVE_FC_WRITE_MULTIPLE_REGISTERS
Debug.Print "д�����Ĵ���"
End Select
End SubOnError - ��������
�:
Event OnError(ByVal Description As String)ʾ��:
Private Sub mbSlave_OnError(ByVal Description As String)
Debug.Print "����: " & Description
MsgBox "��������: " & Description, vbExclamation
LogError Description
End SubOnDataReceived - �յ�����
�:
Event OnDataReceived(ByVal ClientID As String, Data() As Byte)˵��: �����¼������ڲ鿴ԭʼ�յ������ݡ�
ʾ��:
Private Sub mbSlave_OnDataReceived(ByVal ClientID As String, Data() As Byte)
Debug.Print ClientID & " �յ� " & (UBound(Data) + 1) & " �ֽ�"
Dim sHex As String
Dim i As Long
For i = 0 To UBound(Data)
sHex = sHex & Hex$(Data(i)) & " "
Next i
Debug.Print "����: " & sHex
End Sub? ����ʾ��
������վʾ��
Option Explicit
Private WithEvents mbSlave As cModbusSlave
Private Sub Form_Load()
Set mbSlave = New cModbusSlave
End Sub
Private Sub cmdStart_Click()
mbSlave.ProtocolType = MB_SLAVE_PROTOCOL_TCP
mbSlave.SlaveID = 1
mbSlave.Start 502
End Sub
Private Sub cmdStop_Click()
mbSlave.StopMe
End Sub
Private Sub mbSlave_OnStarted()
Debug.Print "������"
cmdStart.Enabled = False
cmdStop.Enabled = True
End Sub
Private Sub mbSlave_OnStopped()
Debug.Print "��ֹͣ"
cmdStart.Enabled = True
cmdStop.Enabled = False
End Sub��̬���ݸ���ʾ��
Option Explicit
Private WithEvents mbSlave As cModbusSlave
Private WithEvents tmrUpdate As Timer
Private Sub Form_Load()
Set mbSlave = New cModbusSlave
Set tmrUpdate = New Timer
tmrUpdate.Interval = 1000 ' ÿ�����
mbSlave.ProtocolType = MB_SLAVE_PROTOCOL_TCP
mbSlave.SlaveID = 1
mbSlave.Start 502
End Sub
Private Sub mbSlave_OnStarted()
tmrUpdate.Enabled = True
Debug.Print "��ʼ��̬��������"
End Sub
Private Sub tmrUpdate_Timer()
Dim i As Long
' ����ʱ�����ݵ��Ĵ��� 0-5
Dim dtNow As Date
dtNow = Now
mbSlave.SetHoldingRegister 0, Year(dtNow)
mbSlave.SetHoldingRegister 1, Month(dtNow)
mbSlave.SetHoldingRegister 2, Day(dtNow)
mbSlave.SetHoldingRegister 3, Hour(dtNow)
mbSlave.SetHoldingRegister 4, Minute(dtNow)
mbSlave.SetHoldingRegister 5, Second(dtNow)
' ���´��������ݵ��Ĵ��� 10-20
For i = 10 To 20
Dim iValue As Integer
iValue = ReadSensor(i - 10) ' �Ӵ�������ȡ
mbSlave.SetHoldingRegister i, iValue
Next i
End Sub
Private Function ReadSensor(iSensorID As Long) As Integer
' ��������
ReadSensor = Rnd * 10000
End Function
Private Sub Form_Unload(Cancel As Integer)
tmrUpdate.Enabled = False
mbSlave.StopMe
End Sub��ͻ��˴���ʾ��
Option Explicit
Private WithEvents mbSlave As cModbusSlave
Private m_Clients As Collection
Private Sub Form_Load()
Set mbSlave = New cModbusSlave
Set m_Clients = New Collection
mbSlave.ProtocolType = MB_SLAVE_PROTOCOL_TCP
mbSlave.SlaveID = 1
mbSlave.Start 502
End Sub
Private Sub mbSlave_OnClientConnect(ByVal ClientID As String, ByVal RemoteAddress As String)
Debug.Print "�ͻ�������: " & ClientID & " (" & RemoteAddress & ")"
' ��¼�ͻ�����Ϣ
Dim clientInfo As New Collection
clientInfo.Add ClientID, "ID"
clientInfo.Add RemoteAddress, "Address"
clientInfo.Add Now, "ConnectTime"
m_Clients.Add clientInfo, ClientID
' ���ͻ�ӭ����
mbSlave.SetHoldingRegister 100, Year(Now)
mbSlave.SetHoldingRegister 101, Month(Now)
End Sub
Private Sub mbSlave_OnClientDisconnect(ByVal ClientID As String, ByVal Reason As String)
Debug.Print "�ͻ��˶Ͽ�: " & ClientID
On Error Resume Next
m_Clients.Remove ClientID
On Error GoTo 0
End Sub
Private Sub GetClientInfo(ByVal ClientID As String) As String
On Error Resume Next
Dim clientInfo As Collection
Set clientInfo = m_Clients(ClientID)
If Not clientInfo Is Nothing Then
GetClientInfo = "ID: " & clientInfo("ID") & _
", Address: " & clientInfo("Address") & _
", ConnectTime: " & Format$(clientInfo("ConnectTime"), "hh:mm:ss")
End If
End Sub
Private Sub Form_Unload(Cancel As Integer)
mbSlave.StopMe
Set m_Clients = Nothing
End Sub������: 2026-01-16
������־
2026-01-16 (v1.1.0)
- ����
BindAddress���� - ֧������ TCP ������ַ - ����
Start�������� - ֧��������ʱָ��������ַ - ���·���������
Stop()��StopMe() - ����ö������������
Slave������ModbusFunctionCode��ModbusSlaveFunctionCodeModbusExceptionCode��ModbusSlaveExceptionCodeModbusState��ModbusSlaveStateModbusProtocolType��ModbusSlaveProtocolTypeModbusDefaults��ModbusSlaveDefaults
- ����ʾ��������ͬ������