Is there something wrong with the request signal?

Sysprogs forums Forums Other tools & products Is there something wrong with the request signal?

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #13051
    Revolu
    Participant

    I’m having some troubles trying to query a modbus slave with an Arduino through RS485.

    I’ve already succeeded in querying a software modbus slave running on my PC through the USB/COM port using the ModbusMaster libray, hence it shouldn’t be a software issue.

    I read about TTL and level conversions and I put on a circuit like this on a breadboard: (see the attachment: board circuit)

    Using the same firmware/sketch that worked for the software slave, I connected the arduino pin TX and RX to the max485 and A and B to the modbus slave and I issued several requests.

    I can see the signals converted by the MAX485 (CPA1114) though the oscilloscope and it seems to be right. The led on the modbus slave lights on as it sees a modbus transaction. Still, what I read as result of the request is always 0xE0 (invalid slave id) or 0xE2 (timeout).

    I queried the slave with the same equal request using another tool (a RS485/USB converter and CAS Modbus Scanner), and it gives the expected results, that is data 0x01.

    This is the code I’m running on an Arduino Ethernet (with a display for debug purpose):
    <pre class=”lang-c prettyprint prettyprinted”><span class="com">#include</span> <span class="str"><ModbusMaster.h></span>
    <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>

    These are the request signals emitted by the Arduino, that always fail to get a data response, and the other tool, that always succeed:

    (Attachment: Arduino request signal)

    (Attachment: USB/RS485 converter signal)

    (Attachment:Overlap of the two signals)

    Is there something wrong with the request signal? Am I making any mistakes in the circuit or the code?

    Any pointers would be greatly appreciated.

     

    Attachments:
    You must be logged in to view attached files.
    #13056
    Revolu
    Participant

    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>

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.