1

I'm on a project that involves using the serial connection (hence the Jamod) and coding I came across this

//7. Execute the transaction repeat times
//repeat =10 defined in point 1. (go to the source to read the code)
int k = 0;
do {
  trans.execute();
  res = (ReadInputRegistersResponse) trans.getResponse();
  for (int n = 0; n < res.getWordCount(); n++) {
    System.out.println("Word " + n + "=" + res.getRegisterValue(n));
  }
  k++;
} while (k < repeat);

[Source: http://jamod.sourceforge.net/kb/serial_master_howto.html, to really understand the question I think you guys might need to read the whole example]

My question is why does it repeat the transaction? it doesn't make any sense to me since the repeat variable is taken from the args that are placed in the main and as the tutorial says, its optional. So to sum up, why is that repeat there?

Any help will be very appreciated!

4

2 回答 2

0

我想知道同样的事情,但是使用支持 BTW的Wireshark玩并观察流量。modbus 开箱即用,看到更多流量只是为了学习和理解会发生什么是有用的。但在正常的工作流程中,没有必要重复交易。

于 2012-11-15T13:02:14.497 回答
0

因为重复是可选的,所以它可能默认为 0。如果你给它,循环将执行精确的“重复”次数。使用默认值,代码可能只会执行一次(因为重复为 0)

do-while 构造确保您不需要在 for 循环的保护中执行任何魔术操作以至少执行一次。

于 2011-11-24T15:18:23.837 回答