<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Modbus Archives | DMC, Inc.</title>
	<atom:link href="https://static.dmcinfo.com/blog/tag/modbus/feed/index.xml" rel="self" type="application/rss+xml" />
	<link></link>
	<description></description>
	<lastBuildDate>Thu, 06 Aug 2026 14:57:34 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1.2</generator>

<image>
	<url>https://static.dmcinfo.com/wp-content/uploads/2025/04/site-icon-150x150.png</url>
	<title>Modbus Archives | DMC, Inc.</title>
	<link></link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Communicating Between a Siemens PLC and LabVIEW Using Modbus TCP</title>
		<link>https://static.dmcinfo.com/blog/20491/communicating-between-a-siemens-plc-and-labview-using-modbus-tcp/</link>
		
		<dc:creator><![CDATA[Maddie Drake]]></dc:creator>
		<pubDate>Wed, 27 Nov 2019 13:32:16 +0000</pubDate>
				<category><![CDATA[LabVIEW]]></category>
		<category><![CDATA[Manufacturing Automation & Intelligence]]></category>
		<category><![CDATA[Siemens PLC]]></category>
		<category><![CDATA[Test and Measurement Automation]]></category>
		<category><![CDATA[LabVIEW Programming]]></category>
		<category><![CDATA[Modbus]]></category>
		<category><![CDATA[Siemens Programming]]></category>
		<guid isPermaLink="false">https://static.dmcinfo.com/blog/20491/communicating-between-a-siemens-plc-and-labview-using-modbus-tcp/</guid>

					<description><![CDATA[<p>Have you ever needed to integrate LabVIEW into a Siemens PLC project or vice versa? PLCs offer features such as reliability and safety, whereas LabVIEW has plug and play libraries for a variety of hardware. The list of different benefits these two offerings is long, but what if you need both? Modbus TCP is an [&#8230;]</p>
<p>The post <a href="https://static.dmcinfo.com/blog/20491/communicating-between-a-siemens-plc-and-labview-using-modbus-tcp/">Communicating Between a Siemens PLC and LabVIEW Using Modbus TCP</a> appeared first on <a href="https://static.dmcinfo.com/">DMC, Inc.</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p class="wp-block-paragraph">Have you ever needed to integrate <a href="/services/test-and-measurement-automation/labview-programming">LabVIEW</a> into a <a href="/services/manufacturing-automation-and-intelligence/plc-programming/siemens-s7-plc-programming">Siemens PLC</a> project or vice versa? PLCs offer features such as reliability and safety, whereas LabVIEW has plug and play libraries for a variety of hardware.</p>

<p class="wp-block-paragraph">The list of different benefits these two offerings is long, but what if you need both?</p>

<p class="wp-block-paragraph"><strong>Modbus TCP</strong> is an excellent way to transfer data between the two programs. Modbus is an easy to deploy protocol that enables multiple devices on the same network to communicate. To set up Modbus Communication in <a href="/services/manufacturing-automation-and-intelligence/plc-programming/siemens-s7-plc-programming/tia-portal-development">TIA Portal</a>, drag an &ldquo;MB_Server&rdquo; block to the Main[OB1]. This will be the only rung you need in Main [OB1].</p>

<figure class="wp-block-image"><img decoding="async" alt="program blocks" src="https://static.dmcinfo.com/wp-content/uploads/2025/05/1-program-blocks.png"  /></figure>

<p class="wp-block-paragraph">You will need to create a data block for holding registers, which contain the information you are trying to send and receive. Mine is named <strong>&ldquo;dbHoldingRegisters&rdquo;</strong> and I filled it with an array of random values to send to LabVIEW.</p>

<figure class="wp-block-image"><img decoding="async" alt="" src="https://static.dmcinfo.com/wp-content/uploads/2025/05/2-datablocks-siemens.png"  /></figure>

<p class="wp-block-paragraph">You will also need space in a data block to save configuration information to connect using Modbus. The data block should be of type <strong>TCON_IP_v4</strong>, which includes all address parameters required for establishing a connection. Details on how to populate this are listed below.</p>

<figure class="wp-block-image"><img decoding="async" alt="populate data blocks" src="https://static.dmcinfo.com/wp-content/uploads/2025/05/3-populate-data-blocks.png"  /></figure>

<ul class="wp-block-list">
 <li style="margin-bottom: 10px;"><strong>InterfaceID:</strong> This references hardware identifier number, found in &ldquo;Devices and Networks&rdquo;, pictured below.<br />
 <figure class="wp-block-image"><img decoding="async" alt="" src="https://static.dmcinfo.com/wp-content/uploads/2025/05/4-interfaceid.png"  /></figure></li>
 <li style="margin-bottom: 10px;"><strong>ID:</strong> A unique ID for the connection within the CPU ranging from 1 to 4095. If you have multiple &ldquo;MB_Server&rdquo; blocks, they will each need unique numbers.</li>
 <li style="margin-bottom: 10px;"><strong>Connection Type:</strong> 11 is for TCP connections</li>
 <li style="margin-bottom: 10px;"><strong>Active Established:&nbsp;</strong>Select false for passive connection establishment.</li>
 <li style="margin-bottom: 10px;"><strong>Remote Address:</strong> The default address is 0.0.0.0 (any IP address), but you can enter a specific IP address so that the server only responds to requests from this address. For example, if the IP address of my computer is 192.168.0.73, the addresses will be.
 <ul class="wp-block-list">
  <li style="margin-bottom: 10px;">addr[1] = 192</li>
  <li style="margin-bottom: 10px;">addr[2] = 168</li>
  <li style="margin-bottom: 10px;">addr[3] = 0</li>
  <li style="margin-bottom: 10px;">addr[4] = 73</li>
 </ul>
 </li>
 <li style="margin-bottom: 10px;"><strong>Remote Port:</strong> This is the incoming tunnel port number of the remote connection partner (value range: 1 to 49151) or 0 for any.</li>
 <li style="margin-bottom: 10px;"><strong>Local Port:</strong> This is the outgoing port number of the local connection partner (value range: 1 to 49151), 502 is the default. A Modbus client (LabVIEW) sends its messages on the port 502, and a Modbus server (PLC) receives messages on 502.</li>
</ul>

<p class="wp-block-paragraph">On the LabVIEW side, the code is straightforward. All VIs are taken from the Modbus Master.lvclass and are listed below. You will need to create a TCP connection to the IP address of the PLC as well as use the same local port number (502) in your <strong>&ldquo;serverData&rdquo;</strong> data block. Then, you can write and read registers as you choose. As always, make sure to close the connection when you are done.</p>

<figure class="wp-block-image"><img decoding="async" alt="" src="https://static.dmcinfo.com/wp-content/uploads/2025/05/5-labview-sample-code.png"  /></figure>

<p class="wp-block-paragraph">In this example, the value 999 was written to address 0, and then all the holding register values were read and populated in the &ldquo;register values&rdquo; indicator. And Voila! The random array of values we sent from the PLC has been transmitted successfully. You are now reading and writing data from LabVIEW to a PLC.</p>

<p class="wp-block-paragraph">Happy programming!</p>

<figure class="wp-block-image"><img decoding="async" alt="" src="https://static.dmcinfo.com/wp-content/uploads/2025/05/6-programcode.png"  /></figure>

<p class="wp-block-paragraph">Learn more about <a href="/services/test-and-measurement-automation/labview-programming">DMC&apos;s LabVIEW Programming services</a> and <a href="/contact">contact us</a> with any project inquiries!</p>
<p>The post <a href="https://static.dmcinfo.com/blog/20491/communicating-between-a-siemens-plc-and-labview-using-modbus-tcp/">Communicating Between a Siemens PLC and LabVIEW Using Modbus TCP</a> appeared first on <a href="https://static.dmcinfo.com/">DMC, Inc.</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Turning an Arduino into a Modbus Device</title>
		<link>https://static.dmcinfo.com/blog/24183/turning-an-arduino-into-a-modbus-device/</link>
		
		<dc:creator><![CDATA[DMC]]></dc:creator>
		<pubDate>Mon, 26 Jun 2017 13:47:28 +0000</pubDate>
				<category><![CDATA[Embedded Development & Programming]]></category>
		<category><![CDATA[IoT]]></category>
		<category><![CDATA[Manufacturing Automation & Intelligence]]></category>
		<category><![CDATA[PLC]]></category>
		<category><![CDATA[Arduino]]></category>
		<category><![CDATA[Modbus]]></category>
		<guid isPermaLink="false">https://static.dmcinfo.com/blog/24183/turning-an-arduino-into-a-modbus-device-2/</guid>

					<description><![CDATA[<p>A situation arises&#160;where you have a system that includes a PLC, an HMI, and some peripheral devices.&#160;The HMI displays some information to a user and allows for some basic control&#160;and the PLC manages data collection and communication through an RS485 network using a Modbus RTU protocol. Easy, right? Many PLCs already support RS485 and Modbus [&#8230;]</p>
<p>The post <a href="https://static.dmcinfo.com/blog/24183/turning-an-arduino-into-a-modbus-device/">Turning an Arduino into a Modbus Device</a> appeared first on <a href="https://static.dmcinfo.com/">DMC, Inc.</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph"><b>A situation arises&nbsp;</b>where you have a system that includes a PLC, an HMI, and some peripheral devices.&nbsp;The HMI displays some information to a user and allows for some basic control&nbsp;and the PLC manages data collection and communication through an RS485 network using a Modbus RTU protocol.</p>



<p class="wp-block-paragraph">Easy, right? Many PLCs already support RS485 and Modbus communication and only require a few functions blocks.</p>



<p class="wp-block-paragraph"><strong>There is a twist</strong>, however.&nbsp;The devices with which the PLC is communicating are Arduinos. <strong>Lots of them.</strong></p>



<p class="wp-block-paragraph"><em>So, how can you program an Arduino to act as a Modbus device?</em></p>



<p class="wp-block-paragraph">Turns out the answer is easy.</p>



<p class="wp-block-paragraph"><strong>In this post, I&#8217;ll talk about adding an Arduino to an RS485 network and programming it to function as a Modbus slave device.</strong></p>



<h2 id="h-how-an-arduino-can-speak-over-an-rs485" class="wp-block-heading">How an Arduino can Speak over an RS485</h2>



<p class="wp-block-paragraph">First, let’s talk about Arduino and RS485.</p>



<p class="wp-block-paragraph">Although Arduino supports serial communication through its built-in UART (Universally Asynchronous Receiver/Transmitter), it uses TTL (Transistor-Transistor Logic), not RS485. Both signaling types use serial communication. Serial communication<strong>&nbsp;means data is sent one bit at a time at a specified BAUD rate</strong> (bits per second).&nbsp;</p>



<p class="wp-block-paragraph">However,&nbsp;TTL is single-ended, whereas RS485 relies on a differential signal (<a href="https://community.plm.automation.siemens.com/t5/Testing-Knowledge-Base/Single-Ended-vs-Differential-Inputs/ta-p/355022" rel="no follow" target="_blank">what&#8217;s the difference?</a>). This is far more useful in an industrial setting where signals can be influenced by electrical noise, and devices can be separated dozens or hundreds of meters.</p>



<p class="wp-block-paragraph">To allow an Arduino to speak over an RS485 network, <strong>an additional device must be used to convert TTL to RS485</strong>. There are several devices on the market that do this, but I used this <em>RS485 Transceiver Breakout Board</em>.</p>



<h2 id="h-hardware-set-up-and-serial-ports" class="wp-block-heading">Hardware Set-Up and Serial Ports</h2>



<p class="wp-block-paragraph">This picture shows my hardware setup.</p>



<h2 id="h-" class="wp-block-heading"></h2>



<p class="wp-block-paragraph">Because I’m using an Arduino Mega, 4 serial ports available. If you are unsure how many ports you have, the <a href="https://www.arduino.cc/en/Reference/Serial" rel="no follow" target="_blank">Arduino website</a>&nbsp;provides a description of serial ports on all board models.</p>



<p class="wp-block-paragraph">Did you run out of Serial ports? Don’t forget about <a href="https://www.arduino.cc/en/Reference/SoftwareSerial" rel="no follow" target="_blank">Software Serial</a>!</p>



<figure class="wp-block-image"><img decoding="async" src="https://static.dmcinfo.com/wp-content/uploads/2025/05/Setup-2.jpg" alt=""/></figure>



<p class="wp-block-paragraph">Great! Your Arduino is now&nbsp;networked to the PLC (or another master device) and other non-master devices.</p>



<h2 id="h-installing-arduino-ide" class="wp-block-heading">Installing Arduino IDE</h2>



<p class="wp-block-paragraph">Next is software. I’ll be using the Arduino IDE for software development.</p>



<p class="wp-block-paragraph"><strong>Although Modbus was developed in 1979, it has stood the test of time and is proven to still a reliable industrial communication protocol.</strong> You may choose to write your own C++ library implementation of Modbus, but I opted to use a prewritten library.</p>



<p class="wp-block-paragraph">A quick Google search yields several options. I chose <a href="https://github.com/smarmengol/Modbus-Master-Slave-for-Arduino/blob/master/ModbusRtu.h" rel="no follow">this one</a>.</p>



<h2 id="h-include-modbusrtu-library" class="wp-block-heading">Include ModbusRtu Library</h2>



<p class="wp-block-paragraph">The first step is to include the ModbusRtu library in your sketch.</p>



<p class="wp-block-paragraph"><em>The Modbus constructor takes three parameters:</em></p>



<ol class="wp-block-list">
<li><strong>Slave Address</strong> – this is the Modbus address that the master device will use to make specific queries. In other words, it’s how other devices in the network know who is talking. The address can be any number between 1 and 247; address 0 is reserved for the master. It’s important to ensure there are no duplicate addresses on the network.</li>
</ol>



<ol class="wp-block-list" start="2">
 

<li><strong>Serial Port</strong> &ndash; this tells the library which Serial port to use. Here is the list of options, depending on the number of ports available on your Arduino model:

 

<p class="wp-block-paragraph">0 = Serial</p>



 

<p class="wp-block-paragraph">1 = Serial1</p>



 

<p class="wp-block-paragraph">2 = Serial2</p>



 

<p class="wp-block-paragraph">3 = Serial3</p>



 

<p class="wp-block-paragraph">4 = SoftwareSerial</p>


 </li>


 

<li><strong>TX Enable Pin</strong> &ndash; because this is a 2-wire RS485 network, it is only half duplex. <strong>Half duplex means that a device can only receive or transmit at a time. </strong>By default, the Arduino is in receive mode. Setting the TX enable pin high allows the Arduino to transmit data.</li>


</ol>



<h2 id="h-modbus-array-example" class="wp-block-heading">Modbus_array Example</h2>



<p class="wp-block-paragraph">Below is example code that shows<strong> how we can use our newly created RS485 network and Modbus library</strong><em> </em>using a&nbsp;very simple (and probably unrealistic) scenario.</p>



<p class="wp-block-paragraph">In this scenario, our Arduino is hooked<strong> </strong>up to a toggle switch and an LED. We&nbsp;have an operator who wants to know the state of the switch (on or off) and the ability to remotely toggle the LED. What should we do?</p>



<p class="wp-block-paragraph">First, look at how the Modbus library function that allows for reading and writing:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">C++</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>Modbus::poll(uint16_t *regs, uint8_t u8size)</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #4EC9B0">Modbus</span><span style="color: #D4D4D4">::</span><span style="color: #DCDCAA">poll</span><span style="color: #D4D4D4">(</span><span style="color: #569CD6">uint16_t</span><span style="color: #D4D4D4"> *regs, </span><span style="color: #569CD6">uint8_t</span><span style="color: #D4D4D4"> u8size)</span></span></code></pre></div>



<p class="wp-block-paragraph"><em>*regs is a register table used for communication exchange, and u8size is the size of the register table.</em></p>



<p class="wp-block-paragraph">This method checks if there is an incoming query in its serial buffer. If there is, the library will validate the message (check the device address, data length, and CRC) and subsequently perform the correct function.</p>



<p class="wp-block-paragraph">In other words, to use this function, we must pass it an unsigned 16-bit integer array and its length. The array will contain the data that the master device is reading or writing over.</p>



<p class="wp-block-paragraph">For this simple example, I created an array called <strong>modbus_array</strong> that contains two elements.</p>



<ol class="wp-block-list">
<li>The first is a status bit for the LED. If the value of the first element is 0, the LED will turn off; otherwise, it will be on.</li>



<li>The second element will be the state of the switch.</li>
</ol>



<p class="wp-block-paragraph">Now, let’s look at this from the perspective of the master device.</p>



<p class="wp-block-paragraph"><strong>Toggling the LED</strong></p>



<ol class="wp-block-list">
<li>To toggle the LED, it must send a Modbus message to slave address 1 that writes a single holding register (Modbus function #6) to the first element with a value of 0 or 1.</li>



<li>To read the toggle switch, it must send a message that reads holding registers (Modbus function #3) starting at the second element with a length of 1. The Arduino will respond with the second element.</li>
</ol>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">C++</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>// Includes
#include &lt;ModbusRtu.h>

// Defines
#define LED_PIN 5
#define SWITCH_PIN 6

// Global variables
Modbus modbus_port;

// Modbus registers:
// &#91;0&#93; = LED control
// &#91;1&#93; = Switch state
uint16_t modbus_array[] = {0, 0};

void setup()
{
    // Create Modbus slave:
    // (slave ID = 1, serial port = 1, TX enable pin = 7)
    modbus_port = Modbus(1, 1, 7);

    // Start Modbus communication
    modbus_port.begin(9600);

    pinMode(LED_PIN, OUTPUT);
    pinMode(SWITCH_PIN, INPUT);
}

void loop()
{
    // Process Modbus requests
    modbus_port.poll(
        modbus_array,
        sizeof(modbus_array) / sizeof(modbus_array&#91;0&#93;)
    );

    // Register 0 controls the LED
    if (modbus_array&#91;0&#93; == 0)
    {
        digitalWrite(LED_PIN, LOW);
    }
    else
    {
        digitalWrite(LED_PIN, HIGH);
    }

    // Register 1 reports the switch state
    modbus_array&#91;1&#93; = digitalRead(SWITCH_PIN);
}</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #6A9955">// Includes</span></span>
<span class="line"><span style="color: #C586C0">#include</span><span style="color: #569CD6"> </span><span style="color: #CE9178">&lt;ModbusRtu.h&gt;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #6A9955">// Defines</span></span>
<span class="line"><span style="color: #C586C0">#define</span><span style="color: #569CD6"> LED_PIN 5</span></span>
<span class="line"><span style="color: #C586C0">#define</span><span style="color: #569CD6"> SWITCH_PIN 6</span></span>
<span class="line"></span>
<span class="line"><span style="color: #6A9955">// Global variables</span></span>
<span class="line"><span style="color: #D4D4D4">Modbus modbus_port;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #6A9955">// Modbus registers:</span></span>
<span class="line"><span style="color: #6A9955">// &#91;0&#93; = LED control</span></span>
<span class="line"><span style="color: #6A9955">// &#91;1&#93; = Switch state</span></span>
<span class="line"><span style="color: #569CD6">uint16_t</span><span style="color: #D4D4D4"> modbus_array[] = {</span><span style="color: #B5CEA8">0</span><span style="color: #D4D4D4">, </span><span style="color: #B5CEA8">0</span><span style="color: #D4D4D4">};</span></span>
<span class="line"></span>
<span class="line"><span style="color: #569CD6">void</span><span style="color: #D4D4D4"> </span><span style="color: #DCDCAA">setup</span><span style="color: #D4D4D4">()</span></span>
<span class="line"><span style="color: #D4D4D4">{</span></span>
<span class="line"><span style="color: #6A9955">    // Create Modbus slave:</span></span>
<span class="line"><span style="color: #6A9955">    // (slave ID = 1, serial port = 1, TX enable pin = 7)</span></span>
<span class="line"><span style="color: #D4D4D4">    modbus_port = </span><span style="color: #DCDCAA">Modbus</span><span style="color: #D4D4D4">(</span><span style="color: #B5CEA8">1</span><span style="color: #D4D4D4">, </span><span style="color: #B5CEA8">1</span><span style="color: #D4D4D4">, </span><span style="color: #B5CEA8">7</span><span style="color: #D4D4D4">);</span></span>
<span class="line"></span>
<span class="line"><span style="color: #6A9955">    // Start Modbus communication</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #9CDCFE">modbus_port</span><span style="color: #D4D4D4">.</span><span style="color: #DCDCAA">begin</span><span style="color: #D4D4D4">(</span><span style="color: #B5CEA8">9600</span><span style="color: #D4D4D4">);</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #DCDCAA">pinMode</span><span style="color: #D4D4D4">(LED_PIN, OUTPUT);</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #DCDCAA">pinMode</span><span style="color: #D4D4D4">(SWITCH_PIN, INPUT);</span></span>
<span class="line"><span style="color: #D4D4D4">}</span></span>
<span class="line"></span>
<span class="line"><span style="color: #569CD6">void</span><span style="color: #D4D4D4"> </span><span style="color: #DCDCAA">loop</span><span style="color: #D4D4D4">()</span></span>
<span class="line"><span style="color: #D4D4D4">{</span></span>
<span class="line"><span style="color: #6A9955">    // Process Modbus requests</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #9CDCFE">modbus_port</span><span style="color: #D4D4D4">.</span><span style="color: #DCDCAA">poll</span><span style="color: #D4D4D4">(</span></span>
<span class="line"><span style="color: #D4D4D4">        modbus_array,</span></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #569CD6">sizeof</span><span style="color: #D4D4D4">(modbus_array) / </span><span style="color: #569CD6">sizeof</span><span style="color: #D4D4D4">(</span><span style="color: #9CDCFE">modbus_array</span><span style="color: #D4D4D4">&#91;</span><span style="color: #B5CEA8">0</span><span style="color: #D4D4D4">&#93;)</span></span>
<span class="line"><span style="color: #D4D4D4">    );</span></span>
<span class="line"></span>
<span class="line"><span style="color: #6A9955">    // Register 0 controls the LED</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #C586C0">if</span><span style="color: #D4D4D4"> (</span><span style="color: #9CDCFE">modbus_array</span><span style="color: #D4D4D4">&#91;</span><span style="color: #B5CEA8">0</span><span style="color: #D4D4D4">&#93; == </span><span style="color: #B5CEA8">0</span><span style="color: #D4D4D4">)</span></span>
<span class="line"><span style="color: #D4D4D4">    {</span></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #DCDCAA">digitalWrite</span><span style="color: #D4D4D4">(LED_PIN, LOW);</span></span>
<span class="line"><span style="color: #D4D4D4">    }</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #C586C0">else</span></span>
<span class="line"><span style="color: #D4D4D4">    {</span></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #DCDCAA">digitalWrite</span><span style="color: #D4D4D4">(LED_PIN, HIGH);</span></span>
<span class="line"><span style="color: #D4D4D4">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color: #6A9955">    // Register 1 reports the switch state</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #9CDCFE">modbus_array</span><span style="color: #D4D4D4">&#91;</span><span style="color: #B5CEA8">1</span><span style="color: #D4D4D4">&#93; = </span><span style="color: #DCDCAA">digitalRead</span><span style="color: #D4D4D4">(SWITCH_PIN);</span></span>
<span class="line"><span style="color: #D4D4D4">}</span></span></code></pre></div>



<h2 id="h-conclusion" class="wp-block-heading">Conclusion</h2>



<p class="wp-block-paragraph">Arduinos are usually confined to hobbyist and educational markets because of their low barrier to entry, but this also makes them ideal candidates for prototyping, even in the industrial automation space.</p>



<p class="wp-block-paragraph">Do you have a low fidelity project that could use some low-cost microcontrollers? Check out our <a href="https://static.dmcinfo.com/services/embedded-development-and-embedded-programming">Embedded Development and Programming</a> service area to find out if DMC could be good fit for you! Or, <a href="https://static.dmcinfo.com/contact">Contact Us</a> to get started on a solution today.</p>



<p class="wp-block-paragraph"></p>
<p>The post <a href="https://static.dmcinfo.com/blog/24183/turning-an-arduino-into-a-modbus-device/">Turning an Arduino into a Modbus Device</a> appeared first on <a href="https://static.dmcinfo.com/">DMC, Inc.</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
