my arduino master and slave python code is below. im using 32 bit board so i am using sercom port. i can receive interger value properly but float can't. please tell the way to correct. thank you..
#include <ModbusMaster.h>
#include "wiring_private.h" // pinPeripheral() function
#define PIN_SERIAL1_RX 11
#define PIN_SERIAL1_TX 10
Uart Serial2( &sercom1, PIN_SERIAL1_RX, PIN_SERIAL1_TX, SERCOM_RX_PAD_0, UART_TX_PAD_2 ) ;
void SERCOM1_Handler()
{
Serial2.IrqHandler();
}
// instantiate ModbusMaster object
ModbusMaster node;
void setup()
{
// use Serial (port 0); initialize Modbus communication baud rate
Serial.begin(9600);
// communicate with Modbus slave ID 2 over Serial (port 0)
node.begin(1, Serial2);
Serial2.begin(9600);
// Assign pins 10 & 11 SERCOM functionality
pinPeripheral(PIN_SERIAL1_TX, PIO_SERCOM);
pinPeripheral(PIN_SERIAL1_RX, PIO_SERCOM);
}
void loop()
{
static uint32_t i;
unsigned long j, result;
unsigned long data[2];
i++;
result = (node.readHoldingRegisters(40000,2));
// do something with data if read is successful
if (result == node.ku8MBSuccess)
{
//Serial.print("yes");
for (j = 0; j<1; j++)
{
data[j] = node.getResponseBuffer(j);
Serial.println(data[j]);
//String x=String(data[j]);
//Serial.println(x);
// delay(1000);
}
}
}
#!/usr/bin/env python
# -*- coding: utf_8 -*-
""" Modbus TestKit: Implementation of Modbus protocol in python
(C)2009 - Luc Jean - luc.jean@gmail.com
(C)2009 - Apidev - http://www.apidev.fr
This is distributed under GNU LGPL license, see license.txt
"""
import sys
import struct
import modbus_tk
import modbus_tk.defines as cst
from modbus_tk import modbus_rtu
import serial
#PORT = 0
PORT = '/dev/ttyUSB0'
def main():
"""main"""
logger = modbus_tk.utils.create_logger(name="console", record_format="%(message)s")
#Create the server
server = modbus_rtu.RtuServer(serial.Serial(PORT))
try:
logger.info("running...")
logger.info("enter 'quit' for closing the server")
server.start()
slave_1 = server.add_slave(1)
#slave_1.add_block('ch1',cst.HOLDING_REGISTERS, 40000, 13)
#slave_1.set_values('ch1', 40000,46.4)
slave_1.add_block('ch1', cst.HOLDING_REGISTERS, 40000, 13)
j = struct.unpack('HH',struct.pack('f', 45.15))
slave_1.set_values('ch1', 40000, j)
while True:
cmd = sys.stdin.readline()
args = cmd.split(' ')
if cmd.find('quit') == 0:
sys.stdout.write('bye-bye\r\n')
break
elif args[0] == 'add_slave':
slave_id = int(args[1])
server.add_slave(slave_id)
sys.stdout.write('done: slave %d added\r\n' % (slave_id))
elif args[0] == 'add_block':
slave_id = int(args[1])
name = args[2]
block_type = int(args[3])
starting_address = int(args[4])
length = int(args[5])
slave = server.get_slave(slave_id)
slave.add_block(name, block_type, starting_address, length)
sys.stdout.write('done: block %s added\r\n' % (name))
elif args[0] == 'set_values':
slave_id = int(args[1])
name = args[2]
address = int(args[3])
values = []
for val in args[4:]:
values.append(int(val))
slave = server.get_slave(slave_id)
slave.set_values(name, address, values)
values = slave.get_values(name, address, len(values))
sys.stdout.write('done: values written: %s\r\n' % (str(values)))
elif args[0] == 'get_values':
slave_id = int(args[1])
name = args[2]
address = int(args[3])
length = int(args[4])
slave = server.get_slave(slave_id)
values = slave.get_values(name, address, length)
sys.stdout.write('done: values read: %s\r\n' % (str(values)))
else:
sys.stdout.write("unknown command %s\r\n" % (args[0]))
finally:
server.stop()
if __name__ == "__main__":
main()