====== MPINO STUDIO2 · Connecting an HMI with Modbus RTU ====== [[:en-us_guide:hmi_connection:studio2|← STUDIO2 installation and start]] · [[:en-us_guide:hmi_connection:screen|Next: Building the HMI Screen →]] · [[:en-us_guide:hmi_connection:studio2:cnet|Optional: LS Electric Cnet →]] When you set the protocol to **Modbus RTU Slave** in MPINO STUDIO2, the app generates the product's request-handling code. The HMI sends requests as the **Modbus RTU Master**. The internal ''D'' words the HMI reads and writes map to Holding Registers, and ''M'' bits map to Coils. The addresses in STUDIO2's generated code are different from those in the Arduino IDE SDK examples. The table shows **0-based addresses inside the Modbus request** that STUDIO2's generated code accepts. If your HMI takes 1-based Coil numbers, ''M0'' may appear as ''1001''. For the numbers to enter in 1-based or ''4xxxx'' notation, check the HMI driver manual and follow [[:en-us_guide:hmi_connection:screen#check_the_address_format|the HMI address format]]. ^ HMI element ^ STUDIO2 memory ^ Modbus area ^ 0-based address in the request ^ | Number that changes every second | ''D0'' | Holding Register | ''2000'' | | Number input | ''D1'' | Holding Register | ''2001'' | | Number that returns the input value | ''D2'' | Holding Register | ''2002'' | | Switch and check lamp | ''M0'' | Coil | ''1000'' | | Lamp for the ladder result | ''M1'' | Coil | ''1001'' | This simple example uses only internal memory on **MPINO-8A4R-S · RS-232 ''Serial1'' · station 1 · 9600 bps · 8N1**. It does not use real I/O terminals. For channels, wiring, installation, and product selection, see [[:en-us_guide:hmi_connection:wiring|Wiring the HMI]] and [[:en-us_guide:hmi_connection:studio2|STUDIO2 installation and start]]. ===== 1. New project and internal memory ===== - Select ''MPINO-8A4R(T)-S'' under **File → New** and save the project as ''HMI_First_Test.mp2''. - Under **Settings → Memory Area Settings**, set P to ''64'' and M, D, C, T, and R to ''16'' each. These numbers are **counts**, not addresses. - Under **Settings → Ladder Pin Map Settings**, clear **Use** for all physical input and output pins. This program uses only internal ''M'' and ''D''. {{ :en-us_products:mpino_studio2:board-select-modal-marked.svg?900 |MPINO STUDIO2 board selection window }} The communication terminals differ by product. Check your product's port in the [[:en-us_guide:hmi_connection:wiring#choose_a_communication_channel|channel table]]. ===== 2. One ladder rung and the main code ===== In the Explorer, open **Ladder → ladderLoop**. Place an **NO contact** (''F2'') ''M0'' at row 1, column 1, and an **output coil** (''F9'') ''M1'' at row 1, column 2. ''M1'' is an internal bit, not a real relay output. {{ :en-us_guide:hmi_connection:internal-ladder.svg?420 |Exercise circuit where contact M0 drives internal coil M1 }} Open the ''main'' code tab under **Code** and enter the code below. This code goes in ''main'' of the STUDIO2 project. It is not a stand-alone sketch for Arduino IDE. #include "mpino_runtime.h" #include "ladder_generated.h" unsigned long lastTick = 0; void setup() { M[0] = false; M[1] = false; D[0] = 0; D[1] = 0; D[2] = 0; lastTick = millis(); } void loop() { ladderLoop(); unsigned long now = millis(); if (now - lastTick >= 1000UL) { lastTick = now; D[0] = (D[0] + 1) % 100; } D[2] = D[1]; } ''D0'' repeats 0~99 once per second. When the HMI writes a number to ''D1'', ''D2'' follows it. When ''M0'' turns on, the ladder turns on ''M1''. **STUDIO2 generates the communication initialization and request handling from the communication settings**, so do not add ''ImodbusRTUInit()'' or ''ImodbusRTU()'' to this code. ===== 3. Run → Communication ===== **Enter the values in the table below.** The figure shows an initial screen to locate each item. Its protocol **''None''** and baud rate **''115200''** are not the values for this exercise. Change them to ''Modbus RTU Slave'' and ''9600'', then save. ^ Figure no. ^ Item ^ Value ^ | ① | Channel | ''Serial1'' — the RS-232 port on this product | | ② | Protocol | ''Modbus RTU Slave'' | | ③ | Slave ID | ''1'' | | ④ | Baud Rate | ''9600'' | | ⑤·⑥·⑦ | Data Bits · Stop Bits · Parity | ''8'' · ''1'' · ''None'' | {{ :en-us_products:mpino_studio2:communication-settings-marked.svg?1000 |Initial screen showing where each item is. The protocol None and 115200 in the figure are only an example; this exercise changes them to Modbus RTU Slave and 9600. }} Enter these values under **Run → Communication** and press ''Ctrl+S'' to save. On the HMI side, set the RS-232 port you connected to **Modbus RTU Master** with the same station number and communication settings. ===== 4. Build, upload, and HMI addresses ===== - Run **Run → Build** (''Ctrl+R'') and check for the success message in Build Output. - In the status bar, select **the PC COM port of the MP Download Cable for your product**, then run **Run → Upload** (''Ctrl+U''). Confirm that the upload log in the bottom panel **ends normally**. The upload port and ''Serial1'' for the HMI are separate connections. For screens and steps, see [[:en-us_products:mpino_studio2:first_project#select_a_port_and_upload|Select a Port and Upload]]. - Connect the three HMI numbers and the bit switch and lamp according to the address table above. Set numbers to **signed 16-bit integer, 1 word, 0 decimal places, scale 1**, and set the input range to ''0~9999''. Actual HMI communication in this example must be checked separately for your HMI model and cable. **Do not assign the same Serial1 to ladder monitoring or user serial code as well.** First assign this page's Modbus addresses in [[:en-us_guide:hmi_connection:screen|Building the HMI Screen]], then continue with [[:en-us_guide:hmi_connection:check|Checking Operation and Troubleshooting]]. To try Cnet as well, [[:en-us_guide:hmi_connection:studio2:cnet|switch the same program to LS Electric Cnet →]].