Click here to Skip to main content
15,891,316 members
Articles / Desktop Programming / Windows Forms

Simple Modbus Slave Simulator

Rate me:
Please Sign up or sign in to vote.
2.87/5 (8 votes)
6 Nov 2007CPOL 108.9K   12.2K   39   9
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:

C++
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)


Written By
Software Developer (Senior) NEC
Turkey Turkey
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalincorrect bytestosend calculation ? Pin
Member 141103408-Jan-19 4:28
Member 141103408-Jan-19 4:28 
GeneralIncorrect number of bytes being returned. Pin
Andy Fraser18-Dec-07 3:29
Andy Fraser18-Dec-07 3:29 
GeneralGave it a 3. The subject deserves a 5, though. Pin
Nick Alexeev6-Nov-07 11:41
professionalNick Alexeev6-Nov-07 11:41 
GeneralRe: Gave it a 3. The subject deserver a 5, though. Pin
Joshua Quick6-Nov-07 15:45
Joshua Quick6-Nov-07 15:45 
GeneralMModbus Pin
Giannakakis Kostas6-Nov-07 20:07
professionalGiannakakis Kostas6-Nov-07 20:07 
GeneralRe: MModbus Pin
Mert Ozdag6-Nov-07 20:48
Mert Ozdag6-Nov-07 20:48 
GeneralRe: MModbus Pin
Joshua Quick6-Nov-07 21:16
Joshua Quick6-Nov-07 21:16 
GeneralRe: Gave it a 3. The subject deserver a 5, though. Pin
Mert Ozdag6-Nov-07 20:45
Mert Ozdag6-Nov-07 20:45 
GeneralRe: Gave it a 3. The subject deserver a 5, though. Pin
Joshua Quick6-Nov-07 21:26
Joshua Quick6-Nov-07 21:26 

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

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