MBServer Overview
MBServer is two complementary Modbus communication components recommended by modbus.org. It should be noted that although they are both called "MBServer", they are actually two different projects:
- MBServer ActiveX (mbserver.tripod.com) A free ActiveX automation server for communicating with PLCs or other industrial devices on the Windows platform via Modbus RTU/ASCII and Modbus TCP protocols
- MBServer TCP (sourceforge.net/projects/mblogic) An open-source collection of Modbus TCP server/client libraries, including standalone servers, clients, and command-line tools
MBServer ActiveX components
MBServer ActiveX is an out of process ActiveX automation server, which means it can be called by any language that supports COM/OLE - Visual Basic 6, VBA (Excel macros), C #, C++, Delphi, etc. It is a FREEWARE (free software) that allows you to use all features without paying.
Supported functions
- Modbus RTU/ASCII(serial port RS-232/RS-485)
- Modbus TCP(Ethernet communication)
- Function codes: FC01 read coil, FC02 read discrete input, FC03 read hold register, FC04 read input register, FC05 write single coil, FC06 write single register, FC15 write multi coil, FC16 write multi register
- Automatic reconnection mechanism
- Multi threaded safety design
Using MBServer ActiveX in VBA (Excel)
This is one of the most practical scenarios for MBServer ActiveX - directly reading and writing PLC data in Excel:
' VBA 代码:在 Excel 中读取 Modbus TCP 设备数据
Sub ReadModbusData()
Dim mb As Object
Set mb = CreateObject("MBServer.MBServerCtrl.1")
' 配置 Modbus TCP 连接
mb.Protocol = 3 ' 3 = Modbus TCP
' 连接设备
If mb.Connect("192.168.1.100", 502) Then
' 读取从站 1,起始地址 40001,10 个保持寄存器
Dim values As Variant
values = mb.ReadHoldingRegisters(1, 40001, 10)
' 将数据写入 Excel 单元格
Dim i As Integer
For i = 0 To UBound(values)
Cells(i + 1, 1).Value = values(i)
Next i
mb.Disconnect
Else
MsgBox "连接失败!"
End If
End SubUsing MBServer ActiveX in C #
// C# 代码:通过互操作调用 MBServer ActiveX
using System.Runtime.InteropServices;
Type mbType = Type.GetTypeFromProgID("MBServer.MBServerCtrl.1");
dynamic mb = Activator.CreateInstance(mbType);
mb.Protocol = 3; // Modbus TCP
if (mb.Connect("192.168.1.100", 502))
{
// 读取 10 个保持寄存器
Array values = (Array)mb.ReadHoldingRegisters(1, 40001, 10);
foreach (var v in values)
{
Console.WriteLine(v);
}
mb.Disconnect();
}Limitations of MBServer ActiveX
ActiveX technology was born in the 1990s and has gradually been adopted in modern Windows development Replace with updated technologies such as NET and COM Interop. MBServer ActiveX only supports 32-bit Windows and needs to be used through the WOW64 compatibility layer or surrogate process in 64 bit applications. For new projects, it is recommended to use NModbus (C #) or ASComm. NET NET native library. MBServer TCP (MBlogic) is a completely different project positioned as an open-source Modbus TCP toolset, hosted on SourceForge. The license is GPL (suitable for open source projects, commercial projects need to pay attention to compliance).
MBServer TCP (mblogic)
MBServer TCP is a completely different project positioned as an open-source Modbus TCP toolset, hosted on SourceForge. The license is GPL (suitable for open source projects, commercial projects need to pay attention to compliance).
Components
- mbserverd: Independent Modbus TCP server daemon that can be deployed as a system service
- mbclient: Command line Modbus TCP client for testing and script calling
- libmblogic: C language Modbus TCP protocol library that can be called by other programs
Installing MBServer TCP on Linux
# 从 SourceForge 下载源码
git clone https://git.code.sf.net/p/mblogic/code mblogic-code
cd mblogic-code
# 编译(需要 gcc 和 make)
./configure
make
sudo make install
# 启动 Modbus TCP 服务器
mbserverd -p 502 -a 1Testing with MBClient
# 读取 127.0.0.1:502 上从站 1 的保持寄存器 0-9
mbclient -H 127.0.0.1 -p 502 -a 1 -f 3 -s 0 -n 10
# 写入从站 1 的保持寄存器 0
mbclient -H 127.0.0.1 -p 502 -a 1 -f 6 -s 0 -v 1234Modern alternative solutions
Due to the older MBServer ActiveX technology stack and low activity of the MBLogic project, the following alternative solutions are recommended in actual projects:
| Scenario of Requirement | Recommended Solution | Explanation |
|---|---|---|
| Windows. NET/C # development | NModbus or ASComm.net | . NET native library, no need for ActiveX dependencies |
| Reading PLC with Excel VBA | MBServer ActiveX | Still the simplest way for VBA scenes |
| Linux server | Libmodbus or pymodbus | More active C/Python libraries |
| Embedded Linux | libmodbus | Lightweight, supporting RTU and TCP |
Summary
MBServer represents two classic implementation directions of Modbus communication: MBServer ActiveX is designed specifically for Windows desktop applications and Office automation, suitable for quickly building simple Modbus data acquisition interfaces; MBServer TCP (MBLogic) is an open-source Modbus TCP server and command-line tool for Linux environments. Although their technology stack is relatively outdated, they still have value as learning materials and historical references. In actual new projects, it is recommended to choose more modern alternative solutions based on the platform and requirements.
Leave a Reply