====== Arduino IDE · Connecting an HMI with Modbus RTU ====== [[:en-us_guide:hmi_connection|HMI Connection overview]] · [[:en-us_guide:hmi_connection:arduino|Arduino IDE setup]] In Modbus RTU, the HMI sends requests as the **Master** and the product answers as the **Slave**. In this SDK, ''M'' bits map to Coils and ''D'' words map to Holding Registers. Check whether your HMI driver expects 0-based or 1-based numbers before entering an address. The example on this page uses **MPINO-8A4R-S · RS-232 ''Serial1'' · station 1 · 9600 bps · 8N1**. See [[:en-us_guide:hmi_connection:wiring|Wiring the HMI]] for channels and wiring, and [[:en-us_guide:hmi_connection:arduino|Arduino IDE setup]] for the development environment. ===== 1. Addresses for the HMI screen ===== ^ Screen element ^ SDK memory ^ 0-based address in the Modbus request ^ Operation ^ | Communication check number | ''D0'' | Holding Register ''0'' | Read with FC03, repeats 0~99 | | Number input | ''D1'' | Holding Register ''1'' | Write 0~9999 with FC06/FC16 | | Returned value | ''D2'' | Holding Register ''2'' | Read with FC03, same as D1 | | Test switch and lamp | ''M0'' | Coil ''0'' | Toggle write with FC05/FC15, read with FC01 | If your HMI editor uses 1-based or ''4xxxx'' numbers, check the [[:en-us_guide:hmi_connection:screen#check_the_address_format|address conversion table]]. Number format, switch, and lamp settings are covered together in [[:en-us_guide:hmi_connection:screen|Building the HMI Screen]]. ===== 2. Enter the complete code in a new sketch ===== This simple example exchanges internal memory values at the addresses above. It does not use any real input or relay output pins. #include unsigned long lastTick = 0; void setup() { ImodbusRTUMem(16, 16); // Communication memory, including M0 and D0~D2 ImodbusRTUAdr(0, 0); // External addresses also start at 0 ImodbusRTUInit(Serial1, 1, 9600); lastTick = millis(); } void loop() { if (M == nullptr || D == nullptr) return; ImodbusRTU(); unsigned long now = millis(); if (now - lastTick >= 1000UL) { lastTick = now; D[0] = (D[0] >= 99) ? 0 : D[0] + 1; } if (D[1] > 9999) D[1] = 9999; D[2] = D[1]; } ''ImodbusRTUMem()'' allocates the memory at startup, and ''ImodbusRTU()'' handles HMI requests on every loop. ===== 3. Verify, upload, and build the HMI screen ===== After uploading the sketch, place the HMI elements using the screen example below and the address table above. {{ :en-us_guide:hmi_connection:practice-screen.svg?420 |D0 communication check number, D1 input, D2 returned value, M0 test switch and lamp }} - Save the sketch and run **Verify** in Arduino IDE. - Check **Tools → Board** and the upload COM port, then select **Upload**. See [[:en-us_guide:getting_started|Getting Started with Arduino IDE]]. - In [[:en-us_guide:hmi_connection:screen|Building the HMI Screen]], assign Holding Registers ''0~2'' and Coil ''0'' to external device elements, then transfer the HMI project. - Turn on the product and the HMI, then test read → write → bit in [[:en-us_guide:hmi_connection:check|Checking Operation and Troubleshooting]]. {{ :en-us_guide:hmi_connection:write-read-flow.svg?420 |The HMI writes D1 and M0, then reads back the product's D2 and M0 to check the result }} Expected results and where to look for each symptom are listed in [[:en-us_guide:hmi_connection:check|Checking Operation and Troubleshooting]].