writing a modbus program for a Open-WRT router using libmodbus C (rewrite Python app to C) -
i trying write program run on open-wrt router, reads registers modbus device. way have found write program in c. have written simple working python app communicating modbus rtu slave device pc:
#!/usr/bin/env python import minimalmodbus import serial m = minimalmodbus.instrument('/dev/ttyusb0', 2) # port name, slave address (in decimal) m.serial.baudrate = 19200 m.serial.bytesize = 8 m.serial.stopbits = 2 m.serial.parity = serial.parity_none data = m.read_registers(0, 2, 3) # 3 = read holding register print "value = ", data[0] print "value b = ", data[1] if (data[0] < 20): send = 1 else: send = 0 m.write_register(2, send, 0, 16) # 16 = write multiple registers
now need rewrite code c using libmodbus or other c modbus library. cannot install python device, has 4mb of space, option use c/c++.
i found this example code raspberry pi, code rpi only:
// access arm running linux #define bcm2708_peri_base 0x20000000 #define gpio_base (bcm2708_peri_base + 0x200000) /* gpio controller */ void *gpio_map; // gpio setup macros. use inp_gpio(x) before using out_gpio(x) or set_gpio_alt(x,y) #define inp_gpio(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3)) #define out_gpio(g) *(gpio+((g)/10)) |= (1<<(((g)%10)*3)) #define set_gpio_alt(g,a) *(gpio+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)<<(((g)%10)*3)) #define gpio_set *(gpio+7) // sets bits 1 ignores bits 0 #define gpio_clr *(gpio+10) // clears bits 1 ignores bits 0 // note: revision2.0 raspberrypis, gpio pins are: 2, 3, 4, 7, 8, 9, 10, 11, 14, 15, 17, 18, 22, 23, 24, 25, 27, 28, 29, 30, 31 additionally available on p5 header) // pinout info see http://elinux.org/rpi_low-level_peripherals#gpio_driving_example_.28c.29 // define gpio number each pin (using rev2 of pi) // t= top row of pins
my device not rpi.
do know examples? not know c except beyond simple hello world program. sorry stupid question.
i feel you... far programming languages supports, looks stuck c/c++ or maybe lua. because trying compile code router pc going have cross-compiling. reason (in case don't know) being pc runs on (most likely) x86_64 or 32 bit architecture(arch) cpu , when compile code normally, compile machine code run on arch. router runs on arm or mips32 or similar arch, (you should able find in dd-wrt or open-wrt databases) have make computers compiler compile code machine code run on different architecture can tricky. i'm no expert on cross-compiling, or on open-wrt, or modding routers in general(pita) if google around should able find tutorials.
there decent tutorial on cross-compiling embedded linux here.
and here tutorial looks walks through process of cross-compiling open-wrt c projects:
also, checking i'm assuming router either has serial port, or plan on using modbustcp rtu gateway, in case going have set program in tcp mode.
from experience working modbus in language can bit intimidating if don't have working understanding of protocol device trying communicate with.(oftentimes devices tricky, double , triple check documentation) additionally every implementation little bit different, can take lot of reading, tweaking , debugging working test, others work right out of box (such minimalmodbus lib python).
here link(not enough rep , copy , paste , add 'h') docs libmodbus: ttp://libmodbus.org/docs/v3.1.1/
i haven't written c code while have cut , pasted snippet documentation, , tried add comments. handles basic rtu connection reading register.
#include <modbus.h> modbus_t *ctx; uint16_t tab_reg[64]; int rc; int i; // create modbus device object /*modbus_t *modbus_new_rtu( const char *device, int baud, char parity, int data_bit, int stop_bit);*/ ctx = modbus_new_rtu("/dev/ttyusb0", 115200, 'n', 8, 1); // check if object created if (ctx == null) { fprintf(stderr, "unable create libmodbus context\n"); return -1; } //int modbus_read_registers(modbus_t *ctx, int addr, int nb, uint16_t *dest); // read register value modbus object value 'rc' rc = modbus_read_registers(ctx, 2, 3, tab_reg); // check make sure read successful if (rc == -1) { fprintf(stderr, "%s\n", modbus_strerror(errno)); return -1; } // i'm guessing on one, convert bytes vale integer... probably? (i=0; < rc; i++) { printf("reg[%d]=%d (0x%x)\n", i, tab_reg[i], tab_reg[i]); } // close port , free memory modbus_close(ctx); modbus_free(ctx);
i advise find tutorial or book such practical c++ o'reilly(that's used @ first), or google search 'c programming tutorials' (i don't have enough rep post more links , don't know enough c post ones anyway) , learn little of language, before else.
if need more libmodbus package after have read through documentation can try stephanes irc channel on freenode #libmodbus or can try here. if having problem specific, worth investing little time in making sure question informative need be.
good luck! :-)
Comments
Post a Comment