Click here to Skip to main content
Licence CPOL
First Posted 6 Nov 2007
Views 28,946
Downloads 1,694
Bookmarked 27 times

Simple Modbus Slave Simulator

By | 6 Nov 2007 | Article
A simple Modbus slave simulator for testing Modbus master devices.

Screenshot - image294.gif

Introduction

The Modbus Software Slave simulates a serial modbus slave device. You can configure the slave address, registers, and communication settings. It only responds to function 03, and only works in RTU mode.

Background

Some Modbus knowledge is required.

Using the code

The static CRCStuff class might be useful for basic CRC16 calculations. Here is the code:

public static byte[] calculateCRC(ref byte[] messageArray, int dataLength)
{
    byte usCRCHi = 0xFF;
    byte usCRCLo = 0xFF;
    byte[] returnResult = { 0x00, 0x00, 0x00 };
    int index = 0;
    int messageIndex = 0;
    while (dataLength > 0)
    {
        index = usCRCLo ^ messageArray[messageIndex];
        usCRCLo = Convert.ToByte(usCRCHi ^ crcHi[index]);
        usCRCHi = crcLo[index];
        messageIndex++;
        dataLength--;
    }
    //0th item is crcLo
    returnResult[0] = usCRCLo;
    //1st item is crcHi
    returnResult[1] = usCRCHi;
    //2nd item is the total CRC16.
    //returnResult[2] = Convert.ToByte((usCRCHi << 8 | usCRCLo));
    return returnResult;
}
public static bool checkCRC(ref byte[] messageToCheck, int numberOfBytes)
{
    byte[] calculatedCRC;
    calculatedCRC = calculateCRC(ref messageToCheck, numberOfBytes - 2);
    if (calculatedCRC[0] == messageToCheck[numberOfBytes - 2] && 
        calculatedCRC[1] == messageToCheck[numberOfBytes - 1])
            return true;
    return false;
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Mert Ozdag

Software Developer (Senior)
NEC
Turkey Turkey

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralIncorrect number of bytes being returned. PinmemberAndy Fraser3:29 18 Dec '07  
GeneralGave it a 3. The subject deserves a 5, though. PinmemberNick Alexeev11:41 6 Nov '07  
GeneralRe: Gave it a 3. The subject deserver a 5, though. PinmemberJoshua Quick15:45 6 Nov '07  
GeneralMModbus PinmemberGiannakakis Kostas20:07 6 Nov '07  
GeneralRe: MModbus PinmemberMert Ozdag20:48 6 Nov '07  
GeneralRe: MModbus PinmemberJoshua Quick21:16 6 Nov '07  
GeneralRe: Gave it a 3. The subject deserver a 5, though. PinmemberMert Ozdag20:45 6 Nov '07  
GeneralRe: Gave it a 3. The subject deserver a 5, though. PinmemberJoshua Quick21:26 6 Nov '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 6 Nov 2007
Article Copyright 2007 by Mert Ozdag
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid