Forum Replies Created
-
AuthorPosts
-
RevoluParticipant
Sorry, the code is here:
<pre class=”lang-c prettyprint prettyprinted”>
<span class="com">#include</span> <span class="str"><LiquidCrystal.h></span>
<span class="typ">LiquidCrystal</span><span class="pln"> lcd</span><span class="pun">(</span><span class="lit">12</span><span class="pun">,</span> <span class="lit">11</span><span class="pun">,</span> <span class="lit">4</span><span class="pun">,</span> <span class="lit">5</span><span class="pun">,</span> <span class="lit">6</span><span class="pun">,</span> <span class="lit">7</span><span class="pun">);</span><span class="typ">ModbusMaster</span><span class="pln"> node</span><span class="pun">(</span><span class="lit">1</span><span class="pun">);</span>
<span class="kwd">void</span><span class="pln"> setup</span><span class="pun">()</span> <span class="pun">{</span><span class="pln">
pinMode</span><span class="pun">(</span><span class="lit">3</span><span class="pun">,</span><span class="pln"> OUTPUT</span><span class="pun">);</span><span class="pln">
node</span><span class="pun">.</span><span class="pln">begin</span><span class="pun">(</span><span class="lit">19200</span><span class="pun">);</span><span class="pln">
lcd</span><span class="pun">.</span><span class="pln">begin</span><span class="pun">(</span><span class="lit">16</span><span class="pun">,</span> <span class="lit">2</span><span class="pun">);</span><span class="pun">}</span>
<span class="kwd">void</span><span class="pln"> loop</span><span class="pun">()</span> <span class="pun">{</span>
<span class="typ">uint16_t</span><span class="pln"> m_startAddress</span><span class="pun">=</span><span class="lit">1</span><span class="pun">;</span>
<span class="typ">uint8_t</span><span class="pln"> m_length</span><span class="pun">=</span><span class="lit">1</span><span class="pun">;</span>
<span class="typ">uint8_t</span><span class="pln"> result</span><span class="pun">;</span><span class="pln">digitalWrite</span><span class="pun">(</span><span class="lit">3</span><span class="pun">,</span><span class="pln"> HIGH</span><span class="pun">);</span> <span class="com">// TX</span><span class="pln">
result </span><span class="pun">=</span><span class="pln"> node</span><span class="pun">.</span><span class="pln">readHoldingRegisters</span><span class="pun">(</span><span class="pln">m_startAddress</span><span class="pun">,</span><span class="pln"> m_length</span><span class="pun">);</span><span class="pln">lcd</span><span class="pun">.</span><span class="pln">clear</span><span class="pun">();</span>
<span class="kwd">if</span> <span class="pun">(</span><span class="pln">result </span><span class="pun">==</span><span class="pln"> node</span><span class="pun">.</span><span class="pln">ku8MBSuccess</span><span class="pun">)</span> <span class="pun">{</span><span class="pln">
lcd</span><span class="pun">.</span><span class="pln">print</span><span class="pun">(</span><span class="str">"DATA:"</span><span class="pun">);</span><span class="pln">
digitalWrite</span><span class="pun">(</span><span class="lit">3</span><span class="pun">,</span><span class="pln"> LOW</span><span class="pun">);</span> <span class="com">// RX</span>
<span class="kwd">for</span> <span class="pun">(</span><span class="typ">uint8_t</span><span class="pln"> j </span><span class="pun">=</span> <span class="lit">0</span><span class="pun">;</span><span class="pln"> j </span><span class="pun"><</span><span class="pln"> m_length</span><span class="pun">;</span><span class="pln"> j</span><span class="pun">++)</span><span class="pln"> lcd</span><span class="pun">.</span><span class="pln">print</span><span class="pun">(</span><span class="pln"> node</span><span class="pun">.</span><span class="pln">getResponseBuffer</span><span class="pun">(</span><span class="pln">j</span><span class="pun">),</span><span class="pln"> HEX </span><span class="pun">);</span>
<span class="pun">}</span> <span class="kwd">else</span> <span class="pun">{</span><span class="pln">
lcd</span><span class="pun">.</span><span class="pln">print</span><span class="pun">(</span><span class="str">"ERR "</span><span class="pun">);</span><span class="pln">
lcd</span><span class="pun">.</span><span class="pln">print</span><span class="pun">(</span><span class="pln">result</span><span class="pun">,</span><span class="pln"> HEX</span><span class="pun">);</span>
<span class="pun">}</span><span class="pln">delay</span><span class="pun">(</span><span class="lit">500</span><span class="pun">);</span>
<span class="pun">}</span>
-
AuthorPosts