Member Login
member
passwd
remember me on
this computer.

- join now -
- forgot username or password? -

Jump to a Date

Sponsored Communities
Cool stuff
Select a topic of interest:
...and press:
Neat Stuff
Control.com Stuff

Visit our shop for nerds in control lifestyle products.

Fortune
Certainly there are things in life that money can't buy, but it's very funny--
Did you ever try buying them without money?
-- Ogden Nash
RSS Feed
RSS feed Use this link to get an RSS feed of the Control.com article flow, for private, non-commercial use only:
www.control.com/rss/
To get a personalized feed, become a member at no cost.
Select a Page Style
Select one of the following styles:
- BluFu
- Classic
(cookies required)
advertisement
from the department...
Modbus source code in Pascal or C
Communications systems and equipment. topic
Posted by Rob Perry on 22 May, 2002 - 5:15 pm
Can any one tell me where I can get (or buy!) Turbo Pascal or C++ source code to make a PC into a Modbus slave. Physical interface is RS-485.


Posted by Michael Reed on 23 May, 2002 - 8:40 am
One would think it is readily available. I searched and searched to no avail. I then asked if there was an ap note on how to write my own. Still nothing. I then asked for help on what I would need to do, and got lots of help!

Below is a brief flow chart on how I would program it. I haven't finished it, and you will have to consult the Modicon Modbus Protocol Reference Guide (try searching for "PI-MBUS-300") for details for each specific function. Program is based on Modbus RTU using assembly, but should be pretty generic if using C, and I don't think you will have much trouble starting from scratch.

Interrupt. If 3.5 character times have elapsed since last incoming data, assume message is complete, and set flag to decode and respond to message in main program.

Interrupt. Serial Data In. Buffer data. Start 3.5 character timer to trigger above interrupt.

Interrupt. Serial Data Out. Send the next byte of data if any more.

Main Program
Initilization
:Loop
If flag is set in 3.5 character interrupt, then
Confirm CRC. If wrong, don't respond
Confirm address. If wrong, don't respond
Based on function code, execute routine
If response message is required, output data (I plan on buffering data and enabling Serial Data Out interrupt so that I can output next byte in buffer)
Do other non-Modbus routines
Goto Loop

Hope this helps,
Michael


Posted by Anonymous on 23 May, 2002 - 8:40 am
Yeah, I think if you do an internet search for some software called ModScan32, you will find a company that makes both host and slave software. They have a trail package that last for 10 min but you can close it and start it back up. Try that.


Posted by Jiri Baum on 24 May, 2002 - 12:01 pm
The MatPLC has a Modbus module, but I think it's master only at this stage. However, much of the code will be common, so it might still be of help. I know Mario was planning to implement both eventually...

Jiri
--
Jiri Baum <jiri@baum.com.au> http://www.csse.monash.edu.au/~jirib
MAT LinuxPLC project --- http://mat.sf.net --- Machine Automation Tools


Posted by Chris Douce on 7 June, 2002 - 10:16 am
Hi Rob,
I'm currently in the same situation! I've got a RS485 serial slave and I'm beginning to get the first sensible communication back... I'm having some problems in calculating the CRC values. I'm using two different CRC-16 utility classes, both return different data for the same set of data, not matching the CRC values that the modbus device requires...

Do you know of some code that could do the CRC calculations for me?

(Incidentally, I'm using Java as the main language)

Many thanks!
Chris
chrisd@fdbk.co.uk


Posted by Curt Wuollet on 10 June, 2002 - 11:11 am
I think the sample source code on the Modicon site covers this. If not, the modbus driver in our source tree should. "mat.sf.net":http://mat.sf.net

If worse comes to worse, I could probably dig up what I used when I was playing with it. As I recall you had to be careful which parts are checked. And data types are very important with bitwise operations.

Regards
cww


Posted by Rob Hulsebos on 14 June, 2002 - 3:57 pm
Chris Douce wrote:
>I'm using two different CRC-16 utility classes, both return different data for the same set of data, not matching the CRC values that the modbus device requires...

That is very well possible, as there are several variant of a 16 bit CRC and Modbus uses only one of them. In the Modbus specification there is an
elaborate description and even some sample source code, and you can check your code against the sample-messages given with all the functioncodes.
That's how I did it (a long time ago).

Rob Hulsebos


Posted by Panu-Kristian Poiksalo on 28 February, 2003 - 11:41 am
> Do you know of some code that could do the CRC calculations for me?

Hi, here's an ObjectPascal or Delphi function for generating the CRC.

Have fun!
Panu-Kristian Poiksalo

{MODBUS CRC-16 ObjectPascal (Delphi) Routine by Panu-Kristian Poiksalo 2003
This function calculates a checksum STRING for a message STRING for maximum
usability for me... example call for this function would be something like...

var message:string;
begin
message:=#1#5#0#0#255#0;
ModbusSerialPort.Output:= message + crc16string (message);
end;

happy coding! send me a greeting at bancho@microdim.net when you get it to
work and share this code freely with everyone at any web site!}

function crc16string(msg:string):string;
var crc: word;
n,i: integer;
b:byte;
begin
crc := $FFFF;
for i:=1 to length (msg) do begin
b:=ord(msg[i]);
crc := crc xor b;
for n:=1 to 8 do begin
if (crc and 1)<>0
then crc:=(crc shr 1) xor $A001
else crc:=crc shr 1;
end;
end;
result:=chr(crc and $ff)+chr(crc shr 8);
end;


Posted by Aibo on 10 September, 2002 - 2:29 pm
Yes i have MODBUS slave code written in C code for Atmel 809C52 plateform. U can send me e-mail at "hitmd@hotmail.com", mailto:hitmd@hotmail.com


Posted by Ganesh Okade on 4 August, 2008 - 11:39 pm
You can find a professional implementation of Modbus in C, C++ and C# here:
http://www.sunlux-india.com/MODBUSProtocolSuite.htm

Ga nesh Okade
Sunlux Technologies Ltd.

Your use of this site is subject to the terms and conditions set forth under Legal Notices and the Privacy Policy. Please read those terms and conditions carefully. Subject to the rights expressly reserved to others under Legal Notices, the content of this site and the compilation thereof is © 1999-2009 Nerds in Control, LLC. All rights reserved.

Users of this site are benefiting from open source technologies, including PHP, MySQL and Apache. Be happy.

advertisements
Control.com is the largest Automation community on the web. Learn how to advertise here now...
Find the right Modbus device for your project... or list your Modbus device on the largest online Modbus device directory.
Servo, stepping motor control, analog & web HMI in one system!
Servo, steppers, analog, digital & web HMI - Fully Integrated!
Time to incorporate data handling, web HMI and motion in one system!
Our Advertisers
Help keep our servers running...
Patronize our advertisers!