chipKIT® Development Platform

Inspired by Arduino™

Modbus Library for PIC32MX795

Created Thu, 05 Jun 2014 03:20:55 +0000 by JohnL


JohnL

Thu, 05 Jun 2014 03:20:55 +0000

Looking for a Modbus RTU serial Master and Slave protocol MPIDE Library for PIC32MX controllers. Master for PIC32MX795.

Similar to Simple-Modbus for Arduino.

https://code.google.com/p/simple-modbus/downloads/list

Cheers John


JohnL

Thu, 05 Jun 2014 03:38:12 +0000

Should have searched the forum in more detail, apologies.

One existing old thread on Modbus: http://www.chipkit.net/forum/search.php?keywords=MODBUS+RTU&terms=all&author=&sc=1&sf=all&sk=t&sd=d&sr=posts&st=0&ch=300&t=0&submit=Search

Link to Modbus code:

https://sites.google.com/site/jpmzometa/arduino-mbrt/arduino-modbus-master

Initial news is Good. Master compiles successfully on Duinomite PIC32MX795. Will report further after more tests.

Supports two most important functions 3,16 writing/reading holding registers.

UPDATE

After sorting out Serial ports on Duinomite, above library appears to work OK.

However, SimpleModbus for Arduino appears easier to use, and possibly more comprehensive communication diagnostics.

Have tried importing Simple Modbus Master V10 into MPIDE 023.

https://code.google.com/p/simple-modbus/downloads/detail?name=SimpleModbusMasterV10.zip&can=2&q=

After compiling get followinfg errors.

In file included from SimpleModbusMasterArduino.cpp:1:0: C:\Users\user\Desktop\MPIDE\Working1\libraries\SimpleModbusMasterV10/SimpleModbusMaster.h:81:21: fatal error: Arduino.h: No such file or directory compilation terminated.

#include "Arduino.h" is in SimpleModbusMaster.h library file ???

I am relatively new to Arduino and MPIDE platform and C programing to resolve this error.

John


JohnL

Fri, 06 Jun 2014 00:31:33 +0000

I'm sorry have just realized that this topic should have been posted in Library section.

Maybe Moderator can move this topic across to Library section.

Anyway, have been looking into SimpleModbus Master library for Arduino a bit further.

MPIDE doesn't recognize Arduino.h, have tried changing Arduino.h to WProgram.h as suggested somewhere else.

MPIDE then throws compiling errors, about "unsigned Char".

Being relative newbie to C programming I don't know what is exactly involved.

Is unsigned Char dealt with in Arduino.h? Is there a workaround in MPIDE?

I think a good Modbus RTU library such as SimpleModbus would be very useful with MPIDE for PIC32 processors, particularly with larger projects requiring more speed and memory memory.

Regards John


GrahamM242

Fri, 06 Jun 2014 08:06:53 +0000

It's helpful if you can post the actual error(s) that you've encountered if you can. For what it is worth, I took a really quick look (<2 minutes) at the library, and couldn't get the example to compile on Arduino 1.0.5-r2 with Teensyduino 1.18.

However, looking at the code in SimpleModbusMaster.cpp, I can see that there's a call to

(*ModbusPort).begin(baud, byteFormat);

. However, the MPIDE Serial.begin function only accepts one parameter, the baud rate.

It would appear there may be a requirement for the serial hardware to be put into a mode with 8 data bits and two stop bits. The PIC32 hardware can do this (PDSEL bit on UxMODE register), but I don't know if this is easily exposed via the libraries, or if direct register access is needed.

Otherwise porting looks to be relatively simple - presumably you could test this in the real world?


GrahamM242

Fri, 06 Jun 2014 08:08:31 +0000

For what it is worth, I took a really quick look (<2 minutes) at the library, and couldn't get the example to compile on Arduino 1.0.5-r2 with Teensyduino 1.18.

Slight embarrassment - this was because I had last been working with a board that used USB CDC for serial, not the hardware serial class... Switching to a real serial port fixed this! :oops:


JohnL

Fri, 06 Jun 2014 10:56:58 +0000

With

#include "Arduino.h" in SimpleModbusMaster.h library file.

get following error.

In file included from SimpleModbusMasterArduino.cpp:1:0: C:\Users\user\Desktop\MPIDE\PIC32\IDE\mpide-0023-windows-20130715.\hardware\pic32\libraries\SimpleModbusMasterV10/SimpleModbusMaster.h:87:21: fatal error: Arduino.h: No such file or directory compilation terminated.

by changing #include "Arduino.h" to #include "WProgram.h"

get following errors

In file included from SimpleModbusMasterArduino.cpp:1:0: C:\Users\user\Desktop\MPIDE\PIC32\IDE\mpide-0023-windows-20130715.\hardware\pic32\libraries\SimpleModbusMasterV10/SimpleModbusMaster.h:93:17: error: expected ';' before 'id' C:\Users\user\Desktop\MPIDE\PIC32\IDE\mpide-0023-windows-20130715.\hardware\pic32\libraries\SimpleModbusMasterV10/SimpleModbusMaster.h:94:17: error: expected ';' before 'function' C:\Users\user\Desktop\MPIDE\PIC32\IDE\mpide-0023-windows-20130715.\hardware\pic32\libraries\SimpleModbusMasterV10/SimpleModbusMaster.h:110:17: error: expected ';' before 'connection' C:\Users\user\Desktop\MPIDE\PIC32\IDE\mpide-0023-windows-20130715.\hardware\pic32\libraries\SimpleModbusMasterV10/SimpleModbusMaster.h:120:26: error: expected ',' or '...' before 'id' C:\Users\user\Desktop\MPIDE\PIC32\IDE\mpide-0023-windows-20130715.\hardware\pic32\libraries\SimpleModbusMasterV10/SimpleModbusMaster.h:128:29: error: expected ',' or '...' before 'byteFormat' SimpleModbusMasterArduino.cpp: In function 'void setup()': SimpleModbusMasterArduino.cpp:144:70: error: too many arguments to function 'void modbus_construct(Packet*, unsigned int)' C:\Users\user\Desktop\MPIDE\PIC32\IDE\mpide-0023-windows-20130715.\hardware\pic32\libraries\SimpleModbusMasterV10/SimpleModbusMaster.h:119:6: note: declared here SimpleModbusMasterArduino.cpp:147:74: error: too many arguments to function 'void modbus_construct(Packet*, unsigned int)' C:\Users\user\Desktop\MPIDE\PIC32\IDE\mpide-0023-windows-20130715.\hardware\pic32\libraries\SimpleModbusMasterV10/SimpleModbusMaster.h:119:6: note: declared here SimpleModbusMasterArduino.cpp:174:35: error: 'SERIAL_8N2' was not declared in this scope

Most of the errors at the start are related to unsigned Char?

ie. small snip of actual SimpleModbusMaster.h library file code.

typedef struct
{
  // specific packet info
  unsigned Char id;
  unsigned Char function;
  unsigned int address;
	// For functions 1 &amp; 2 data is the number of points
  // For functions 3, 4 &amp; 16 data is the number of registers
  // For function 15 data is the number of coils
  unsigned int data; 
  unsigned int* register_array;
  
  // modbus information counters
  unsigned int requests;
  unsigned int successful_requests;
	unsigned int failed_requests;
	unsigned int exception_errors;
  unsigned int retries;
  	
  // connection STATUS of packet
  unsigned Char connection;

majenko

Fri, 06 Jun 2014 12:25:55 +0000

WTH is a Char? Is it a strange wrapper class? And if so, how can you have an unsigned class?

Methinks the author of that library is a bit of a n00b...


JohnL

Sat, 07 Jun 2014 00:31:30 +0000

SOLVED*

unsigned Char

was the problem with text editor that I was using :oops:, should be > unsigned char

(lower case c in char), and it is only a data type.

SimpleModbusMasterV6 compiles successfully on Olimex Duinomite/Mega boards with PIC32MX795 and on Fubarino SD 1.5.

Only need to make a change in SimpleModbusMaster.h file

change #include Arduino.h to #include WProgram.h

Default serial port is used and can be changed in library file SimpleModbusMaster.cpp

SimpleModbusMasterV6 uses standard serial port number of stop bits.

SimpleModbusMasterV10 uses configurable number of stop bits, and throws a compiling error. Will need to look into it later.

At this stage I will be using only SimpleModbusSlaves , so standard serial port configuration is OK.


GrahamM242

Sat, 07 Jun 2014 11:39:46 +0000

SimpleModbusMasterV10 uses configurable number of stop bits, and throws a compiling error. Will need to look into it later.

This is because the underlying class that drives the serial port doesn't support specifying the serial format. This can be fixed, although the best place to do that is in the hardwareserial class, rather than the code you're working with.