Wiznet makers

irina

Published September 24, 2025 ©

112 UCC

5 WCC

89 VAR

0 Contests

0 Followers

0 Following

Original Link

[아두이노#614] Arduino에 W5500을 연결해서 C#윈폼으로 모드버스(modbus) TCP의 프로토콜에 대해서 이해하고 통신하는 방법 알아보기!(녹칸다의 아두이노 시즌2)

Arduino + W5500 runs Modbus TCP with C# WinForms UI, showing coil control, register read/write, and key RTU vs TCP differences.

COMPONENTS
PROJECT DESCRIPTION

1. Overview

By combining Arduino with the W5500 Ethernet module, you can easily implement Modbus TCP communication for industrial and IoT applications.
This post explains the differences between Modbus RTU and TCP, introduces common function codes, and demonstrates a C# WinForms UI that controls and monitors LEDs via Modbus TCP.


2. Modbus RTU vs TCP

  • Modbus RTU
    • Requires CRC16 checksum for error detection.
    • Works over serial communication such as RS-485/RS-232.
    • Slave address is represented in 1 byte.
  • Modbus TCP
    • No CRC required, since error handling is done by TCP/IP layers.
    • Adds a 6-byte MBAP header (Transaction ID, Protocol ID, Length, Unit ID).
    • Still keeps the Unit ID field, but in practice devices are identified by IP address.

👉 Conclusion: Modbus TCP is more scalable and suitable for network-based industrial control compared to RTU.


3. Modbus Basics

  • Coils: 1-bit storage (ON/OFF, usually mapped to GPIO pins).
  • Registers: 16-bit storage (used for sensor values or internal variables).
  • Function Codes (FC):
    • 0x05: Write Single Coil
    • 0x01: Read Coils
    • 0x03: Read Holding Registers
    • 0x06: Write Single Register
    • 0x10: Write Multiple Registers

4. Arduino + W5500 Example

(1) Write Single Coil (FC = 0x05)

Control an LED connected to digital pin 2.

  • ON Command
0x00 0x01 0x00 0x00 0x00 0x06 0x01 0x05 0x00 0x00 0xFF 0x00
  • OFF Command
0x00 0x01 0x00 0x00 0x00 0x06 0x01 0x05 0x00 0x00 0x00 0x00

(2) Read Coils (FC = 0x01)

  • Reads multiple coil states from Arduino.
  • Example: LED status is displayed on the WinForms UI.

(3) Address Mapping

  • Coil 0 → Arduino Pin 2
  • Coil 1 → Arduino Pin 3
  • Easily extendable to more GPIO pins and devices.

5. C# WinForms UI Integration

The C# WinForms application acts as the Modbus TCP Master:

  • Input fields for Transaction ID, Unit ID, Coil Address.
  • Buttons for LED ON/OFF control using Write Single Coil (FC=05).
  • A timer periodically issues Read Coils (FC=01) to display LED states in real time.
  • Communication handled over TCP sockets to Arduino (port 502).

6. Implementation Details

Arduino (Slave)

  • Initialize network:
Ethernet.begin(mac, ip, gateway, subnet);
EthernetServer modbusServer(502);
  • Wait for incoming TCP connection, parse MBAP header, extract Function Code.
  • Map coil addresses to GPIO pins.
  • Example: Coil 0 → Pin 2, Coil 1 → Pin 3.
  • Process requests and return responses according to Modbus TCP framing.

C# WinForms (Master)

  • Connect to Arduino:
TcpClient client = new TcpClient(ip, 502);
NetworkStream stream = client.GetStream();
  • Build Modbus TCP frames: MBAP Header + PDU (Function Code + Data).
  • Send Write/Read requests and parse responses.
  • Update the UI with LED state values.

Example (FC = 05: Write Single Coil)

  • Request: Contains coil address + ON/OFF value (0xFF00 = ON, 0x0000 = OFF).
  • Response: Echoes the same data back.

7. Debugging & Tips

  • Endian issues: Modbus uses Big-Endian format, so check byte order.
  • Logging: Print request/response frames in HEX for debugging.
  • Use Modbus Poll or similar tools to test Arduino server before connecting to C#.
  • Define a clear Address Map to expand easily to sensors, relays, and more actuators.

8. Conclusion

This project demonstrates how to implement Modbus TCP with Arduino and W5500 Ethernet, and how to connect it with a C# WinForms UI.
The setup can be applied to:

  • Industrial control systems
  • IoT gateways
  • Remote monitoring applications

 

✅ Key SEO Keywords

  • Arduino Modbus TCP
  • W5500 Ethernet module
  • Modbus RTU vs TCP
  • C# WinForms Modbus example
  • Arduino LED control
Documents
Comments Write