Skip to content

MOD_WRITE_MULTIPLE_COILS doesn't works! #4

@Bob590

Description

@Bob590

File: MODEthCommsProcessor.cpp @ line 587.

Practically the code uses count instead of byteCount that despite the name means number of bits!

The correct code is the following:

             case MOD_WRITE_MULTIPLE_COILS  :
                // unpack into the SIM memory on WORD of sim memory for every BIT in the data 
                //WORD numBytes;

                 numBytesInReq = modMsg.**byteCount**/8; // byteCount == bit count
                 if (modMsg.**byteCount**%8)  // if we overflow a byte
                    numBytesInReq++;

                 {
                 CString deb;
                    deb.Format("Write multiple outputs coils from %d for %d bits.\n", modMsg.address, modMsg.**byteCount**);
                    OutputDebugString(deb);
                    SockDataMessage(deb);
					if (m_CommsDecodeShow) CRS232Port::WriteToFile(deb);  // Added statement on 2016-12-26 by DL to save to Log file
                 }
                 numRegs = numBytesInReq * 8;   // repaint X bits // 2015-01-19 Multiplied by 8 to get total BITS by DL

                 for (i=0;i<numBytesInReq;i++)
                 {
                 WORD bitOffset;
                    for (bitOffset=0;bitOffset<8;bitOffset++)
                    {
                       if ((i*8)+bitOffset < modMsg.**byteCount**)  // Changed <= to only < by DL on 2018-11-27
                       {
                          if (*(BYTE*)modMsg.dataPtr & (0x01<<bitOffset))
                             PLCMemory.SetAt(requestMemArea, seperationOffset + modMsg.address+(i*8)+bitOffset, 1);
                          else
                             PLCMemory.SetAt(requestMemArea, seperationOffset + modMsg.address+(i*8)+bitOffset, 0);
                       }
                    }
                    modMsg.dataPtr++;
                 }
                 break;

Please don't ask me to create a pull request... I'm developing a modbus code for my own program and I have no time to do that.
If you want to insert the suggested changes help yourself and test them!
To verify I have used this document: Modbus_Application_Protocol_V1_1b3.pdf found on modbus.org

See you,
Bob

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions