ARDUINO DRAGINO YUN SHIELD EXAMPLE

289 views
Skip to first unread message

mahir bülent BAŞEL

unread,
Jun 16, 2016, 3:44:19 AM6/16/16
to modbus-tk
I am trying to implement Yun-ModbusTk-Example by ductsoup on github. (https://github.com/ductsoup/Yun-ModbusTK-Example)

At first, I tried to run modbus tcp slave example (by ljean on github) (https://github.com/ljean/modbus-tk/blob/master/examples/tcpslave_example.py) I connected dragino via putty and called script. I am using ModbusWiev TCP program to poll registers. When I tried to pool, I took an illegal data address error code. 

Then I changed the code

from;

Code: [Select]
slave_1 = server.add_slave(1)
        slave_1.add_block('0', cst.HOLDING_REGISTERS, 100, 100)

to;

Code: [Select]
slave_1 = server.add_slave(1)
        slave_1.add_block('0', cst.HOLDING_REGISTERS, 10, 100)


Now it is working well.

After that I tried to call python script by arduino script. I used 

Code: [Select]
Process p;
  p.begin("python");
  p.addParameter("/mnt/sda1/arduino/www/tcpslave_example.py"); 
  p.run();

  
  while (p.available()>0) {
    char c = p.read();
    Console.print(c);
  }



but I failed again on this case. I realized the pathway is wrong. Then I used symbolic link from root to related path. And now I can call script from Arduino side. 

And finally my problem,

When I try to run example code by ductsoup I am getting the same error. (illegal data address) 

All python script code is given below;

Code: [Select]
#!/usr/bin/env python
# -*- coding: utf_8 -*-
"""
 Usage:
 server.py roStart roLength [rwStart rwLength]
"""

import sys
import time

# add logging capability
import logging
import threading

# add modbus
import modbus_tk
import modbus_tk.defines as cst
import modbus_tk.modbus as modbus
import modbus_tk.modbus_tcp as modbus_tcp
import struct

# add bridge
sys.path.insert(0, '/usr/lib/python2.7/bridge/')
from bridgeclient import BridgeClient as bridgeclient
avr = bridgeclient()

logger = modbus_tk.utils.create_logger(name="console", record_format="%(message)s")

if __name__ == "__main__":

  try:
    #Create the server
    server = modbus_tcp.TcpServer(address='0.0.0.0')
    logger.info("running...")

    server.start()
    slave_1 = server.add_slave(1)

    # todo: add checking for overlaping ranges
    argc = len(sys.argv)
    if argc == 3 or argc == 5:
      logger.info("Initializing %s RO float registers beginning at %s" % (str(sys.argv[2]), str(sys.argv[1])))
      slave_1.add_block('ro', cst.HOLDING_REGISTERS, int(sys.argv[1]), int(sys.argv[2]))
      ro = range(int(sys.argv[1]), int(sys.argv[1]) + int(sys.argv[2]))
    if argc == 5:
      logger.info("Initializing %s RW float registers beginning at %s" % (str(sys.argv[4]), str(sys.argv[3])))
      slave_1.add_block('rw', cst.HOLDING_REGISTERS, int(sys.argv[3]), int(sys.argv[4]))
      rw = range(int(sys.argv[3]), int(sys.argv[3]) + int(sys.argv[4]))

    while True:
      # get values from the bridge
      for i in ro[0::2]:
        i1,i2 = struct.unpack('>HH',struct.pack('f',float(avr.get(str(i)))))
        slave_1.set_values('ro', i, i2)
        slave_1.set_values('ro', i+1, i1)

      # put values to the bridge
      for i in rw[0::2]:
        i2, i1 = slave_1.get_values('rw', i, 2)
        val = struct.unpack('f',struct.pack('>HH',i1,i2))[0]
        avr.put(str(i),'%s' % val)

      time.sleep(0.5)

  finally:
    server.stop()


and Arduino code is given below;

Code: [Select]
#include <Bridge.h>

Process p;
#define BUF_SIZE 32
char buf[BUF_SIZE];

void setup() {
  Serial.begin(115200);
  // Start the bridge
  Bridge.begin();
  // Start the modbus server
  p.begin("python");
  p.addParameter("/mnt/sd/modbus_tcp_slave.py"); // modbus float holding registers  
  p.addParameter("40001");                       // read only start address
  p.addParameter("6");                           // read only number of holding registers
  p.addParameter("40101");                       // read/write start address (optional)
  p.addParameter("6");                           // read/write number of holding registers (optional)
  p.runAsynchronously();
}
void loop() {
  // AVR -> WRT (modbus read only), write some values to the modbus server
  Bridge.put("40001", String(PI));
  Bridge.put("40003", String(millis()));
  Bridge.put("40005", String(5280));

  // AVR <- WRT (modbus read/write), read some values from the modbus server
  Bridge.get("40101", buf, BUF_SIZE);
  Serial.println(atof(buf));
  delay(500);
}



I tried to call script from arduino IDE and with putty also. The return is,

running...
Initializing 6 RO float registers beginning at 40001
Initializing 6 RW float registers beginning at 40101

When I start polling I get an error ;

('192.168.8.7' 53890 ) is connected with socket 5

Modbus Error: Exception code = 2


Can anyone help me how can i fix this error ?

jp mandon

unread,
Jun 23, 2016, 4:28:38 PM6/23/16
to modbus-tk
Exception code 2 is an illegal data address. Try to add 1 to the base address.
Reply all
Reply to author
Forward
Message has been deleted
0 new messages