Go Down

Topic: simple modbus master (Read 151 times) previous topic - next topic

Howwee

Sep 16, 2020, 09:22 pm Last Edit: Sep 17, 2020, 04:59 am by Howwee
Can anyone explain how the function 15
FORCE_MULTIPLE_COILS
Works

i have been running some test on this
and cant figure out how to use the
local_start_address

is this the data to read from
or
is it a pointer to another File



modbus_construct(&packets[PACKET1], 1, 15, 1, 8, 1021);

dec 8 = (no_of_registers = packet->data)  // number of coils
so frame 6 = 1 (frame[6] = no_of_bytes;) // number of bytes more
dec 1021 = (packet->local_start_address)
the dec 1021 and 65565 seams to work
but makes no sense to me in binary format ?


part of CPP file:

unsigned char construct_F15()
{
   // function 15 coil information is packed LSB first until the first 16 bits are completed
  // It is received the same way..
  unsigned char no_of_registers = packet->data / 16;
  unsigned char no_of_bytes = no_of_registers * 2;
   
  // if the number of points dont fit in even 2byte amounts (one register) then use another register and pad
  if (packet->data % 16 > 0)
  {
    no_of_registers++;
    no_of_bytes++;
  }
   
  frame[6] = no_of_bytes;
  unsigned char bytes_processed = 0;
  unsigned char index = 7; // user data starts at index 7
   unsigned int temp;
   
  for (unsigned char i = 0; i < no_of_registers; i++)
  {
    temp = register_array[packet->local_start_address + i]; // get the data

frame[index] = temp & 0xFF;

    bytes_processed++;
     
    if (bytes_processed < no_of_bytes)
    {
      frame[index + 1] = temp >> 8;
      bytes_processed++;
      index += 2;
    }
  }
   unsigned char frameSize = (9 + no_of_bytes); // first 7 bytes of the array + 2 bytes CRC + noOfBytes
   return frameSize;
}


pylon

What exactly is your question?

Quote
FORCE_MULTIPLE_COILS
Most implementations call this "Write multiple coils" which explains much better what it actually does.

You posted a small excerpt of what probably is part of a library. But there is no link to the library and you didn't use code tags!

Howwee

i found it
it is the

 temp = register_array[packet->local_start_address + i]; // get the data
 in the ccp file



register_array[packet->local_start_address + i]
its pointing to the wrong location.
supersized no one has noticed this as long as the simplemodbusmaster adruino
has been used ?
or i have a bad file ?

i have temp corrected this by replacing with

unsigned char construct_F15()
{
    // function 15 coil information is packed LSB first until the first 8 bits are completed
    unsigned char frameSize = 10;
    unsigned char temp;
    frame[6] = 1;
    frame[7] = packet->local_start_address ^ 0XFFFF; 
    return frameSize;
}
works for a BYTE (0 - 255)




Go Up