News:

  • June 25, 2026, 03:52:12 PM

Login with username, password and session length

Author Topic: Modbus Writes  (Read 9764 times)

Dmdtrain

  • Jr. Member
  • **
  • Posts: 11
Modbus Writes
« on: February 01, 2023, 01:49:47 PM »
Hello,

When using the MWX command I'm confused how to use the write to multiple registers. I had two separate MWX commands one writing to register 260 and one writing to 270. Both registers use bit 0 and 8. 0 is motor on and 8 is motor direction. Am I correct in that I should be able to use one MWX command for both registers?

Thanks

franji1

  • Bit Weenie
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 3827
    • Host Engineering
Re: Modbus Writes
« Reply #1 on: February 01, 2023, 01:54:32 PM »
No, you cannot write to non-contiguous addresses in one MWX.

There is a Modbus FC for Single Write and another one for Multi-Write of Contiguous addresses.  Since yours are not contiguous, it requires two Modbus Write FC packets, so two MWX instructions.

Dmdtrain

  • Jr. Member
  • **
  • Posts: 11
Re: Modbus Writes
« Reply #2 on: February 01, 2023, 02:01:17 PM »
No, you cannot write to non-contiguous addresses in one MWX.

There is a Modbus FC for Single Write and another one for Multi-Write of Contiguous addresses.  Since yours are not contiguous, it requires two Modbus Write FC packets, so two MWX instructions.

Ah ok. Thanks for the response. When I force values into say D0 for 1 write and D1 for the other write I notice that I can't just jump to 512 for either of them until I step up from 0, 2, 4, 8 etc etc. Does that mean the logic I write needs use a MOVE of those in order up to 512 everytime I want to execute that modbus output?

franji1

  • Bit Weenie
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 3827
    • Host Engineering
Re: Modbus Writes
« Reply #3 on: February 01, 2023, 02:53:31 PM »
Ah ok. Thanks for the response. When I force values into say D0 for 1 write and D1 for the other write I notice that I can't just jump to 512 for either of them until I step up from 0, 2, 4, 8 etc etc. Does that mean the logic I write needs use a MOVE of those in order up to 512 everytime I want to execute that modbus output?

Not sure what you mean. What is 512?

Start with this.

Q1.What Modbus Holding Registers are you writing to?
Q2. For each in Q1, what is the source in your PLC for each of those Holding Registers?

Realize D memory is 32 bit with a range of +/-2 Billion in a Do-more PLC.  Holding Registers in a Modbus Slave are 16 bit with a range of +/-32767 or 0..65535.

Q3a. Are the Holding Registers in the slave actual 32 bits, i.e. occupying 2 WORDs, ala 40260/40261, 40270/40271?
Q3b. Or are they simple WORDs in the slave and you just happen to be using D memory in the Do-more PLC (that's OK as long as you only write 16 bits at a time).

Dmdtrain

  • Jr. Member
  • **
  • Posts: 11
Re: Modbus Writes
« Reply #4 on: February 01, 2023, 05:08:27 PM »
Ah ok. Thanks for the response. When I force values into say D0 for 1 write and D1 for the other write I notice that I can't just jump to 512 for either of them until I step up from 0, 2, 4, 8 etc etc. Does that mean the logic I write needs use a MOVE of those in order up to 512 everytime I want to execute that modbus output?

Not sure what you mean. What is 512?

Start with this.

Q1.What Modbus Holding Registers are you writing to?
Q2. For each in Q1, what is the source in your PLC for each of those Holding Registers?

Realize D memory is 32 bit with a range of +/-2 Billion in a Do-more PLC.  Holding Registers in a Modbus Slave are 16 bit with a range of +/-32767 or 0..65535.

Q3a. Are the Holding Registers in the slave actual 32 bits, i.e. occupying 2 WORDs, ala 40260/40261, 40270/40271?
Q3b. Or are they simple WORDs in the slave and you just happen to be using D memory in the Do-more PLC (that's OK as long as you only write 16 bits at a time).

Apologies, I haven't work with modbus frequently so I may not be explaining myself thouroughly.

1/2. My Do-More PLC is the master to these smart cards that control a few different motors. I am writing to registers 260 and 270 in the cards. 260 is the motor on the left and 270 is the motor on the right. Bit 0 for each is to turn the motor on and bit 8 is to reverse direction. When I force a write of 2 into D0 (source of 260) it starts the motor and vice versa into D1 (source of 270) for the other motor. If I force 512 into D0 or D1 the motors will turn off BUT if I start with writing 2 to turn them on then go to 4, then 8, then 16, then 32, then 64, then 128, then 256, and finally 516 the motors will stay running and switch direction.

I guess I'm confused on why it works like that.

3. Then I'm presuming I should be using 'N's.

Thanks again.


Bolt

  • Hero Member
  • *****
  • Posts: 598
Re: Modbus Writes
« Reply #5 on: February 01, 2023, 10:31:12 PM »
Yes, N memory closely matches the modbus holding registers.  I would assign N0 to one motor and N1 to the other.
You can reference the 0 bit by casting, N0:0 is bit 0 of N0, N0:8 is bit 8. You can then toggle these on or off as a coil in your program, and as long as N0 gets written to your motor controller, it will receive that word containing those bits.
Where it gets confusing is you say you write a 2 to the word, and it starts the motor. The number to is actually 10 in binary, bit 1 true, bit 0 false. And when you say 512 controls reverses direction, that's actualy 1 0000 0000, or only bit 9 true.
As far as stepping from 2, 4, 8, etc, you're loosing me there. Do you have a manual for the controller? Typically each bit in the word means something, so by doing 2, 4, 8, etc you are actually advancing through each bit for a moment, so maybe that in itself is doing something?