Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
11 years, 10 months ago.
ModBus RS485 Mbed library
Hello all,
I wonder if there is any MODBUS / RS485 Mbed library available? Can this be used to interface mbed ith Schneider energy meters..
Thanks!
2 Answers
5 years, 11 months ago.
I am using this code, but not getting any data. I am using STM32F4 and MAX485
#include "mbed.h" #include <RS485.h> Serial pc(PC_6, PC_7); RS485 RS485(PA_2,PA_3, PC_3); // Tx, Rx , !RE and DE MAX485 pin DigitalOut ho(PC_3); // this pin should be connected to !RE and DE typedef uint8_t byte; byte regvalue[9]; byte data[9] = {0x0B,0x04,0x00,0x48,0x00,0x02,0xf1,0xdd};//your data int main() { RS485.baud(9600); pc.baud(9600); pc.printf("main\r\n"); while(1) { pc.printf("Starting\r\n"); ho = 1; // Enable sending on MAX485 // RS485.sendMsg(data,sizeof(data)); wait_ms(600); // Must wait for all the data to be sent ho = 0; // Enable receiving on MAX485 pc.printf("Getting data\r\n"); pc.printf(RS485.readable() >0 ? "Yes" : "Not readable \r\n"); if(RS485.readable() >0){ memset(regvalue,0,sizeof(regvalue)); wait_ms(200); RS485.recvMsg(regvalue,sizeof(data),500); wait_ms(200); for (int count = 0; count < 9; count++) { pc.printf("%X - ", regvalue[count]); } }else printf("No Data\r\n"); printf("Done\r\n"); printf(" \r\n"); wait_ms(1000); } }
Not posting an answer because the question is so old, but there is a port of FreeModBus available now and there are two other RTU implementations.
RS485 transmit control may give some problems though. Standard MBED libs don't make it easy to detect end of sending.
posted by Oliver Broad 21 Mar 2016