(*
  PRG_MB_TEST - Modbus RTU driver test program.

  Configures three channels and exercises the channel scheduler:
  - Channel 0: register, automatic read/write (device 1, port 2).
  - Channel 1: register, read-only, on demand (device 2, port 3).
  - Channel 2: coils, read/write (device 3, port 2).

  Calls every Modbus POU (master/slave init, TCP master and TCP slave
  init and MTB reconfiguration) so the program compiles and exercises
  them all; the overlapping port configurations are intentional and only
  serve compile/debug coverage.

  Channel storage (MB_CHANNELS, c_MB_CHANNELS_NUM) is declared in
  GVL_MB_TEST.csv. The demo values are written to the device buffers
  themselves (D600.. for channel 0, M500.. for channel 2 - the iDDevNum
  values below), so the program does not depend on any additional
  application labels. GVL_MB_TEST.csv also aliases those devices as
  g_iAutoReg0..2, g_iDemandReg0..2, g_xCoil0..7 for monitoring; importing
  it is optional.

  The L02 Ethernet port setup at the top exercises the EIP_* functions of
  this library and L02_SET_IP of the Utils library, so a compile needs the
  Utils library and its IP_PLC_IP constant in the project. Only one role
  of the port is usable at a time: D8395 is written by EIP_INIT (0) and by
  the Modbus TCP init calls (3/4) in the same scan - coverage only.
*)

(* Enable interrupts - required by the ADPRW communication instruction. *)
EI(TRUE);

(* --- L02 Ethernet / EtherNet/IP setup (Utils library) --- *)
(* The trigger is M8002 (one scan only): L02_SET_IP applies the settings
   through the self-resetting M8197, which restarts the L02 interface, so
   it must never be tied to TRUE. Both EtherNet/IP and Modbus TCP are set
   up here for coverage only - the modes are mutually exclusive and the
   Modbus TCP initialisation below overwrites D8395 in the same scan. *)
(* EtherNet/IP with 2 remote couplers: D8395 := 0, D8325 := 2 *)
xEipInit := EIP_INIT(M8002, 2);

(* Remote coupler 1 - 8 DI, 8 DO, 4 AI, 2 AO points.
   DI/DO registers hold BYTES: R23834 = 1 (8 DI bits / 8), R23836 = 1 (8 DO).
   AI/AO registers hold WORDS:  R23835 = 4, R23837 = 2 (points as-is) *)
xEipRemote := EIP_INIT_REMOTE(EIP_REMOTE1, 8, 8, 4, 2);

(* PLC IP 192.168.0.100 -> R23807/R23808, applied by M8197 *)
xSetIp := L02_SET_IP(M8002, IP_PLC_IP, 192, 168, 0, 100);

(* Build the serial-port bit-field (9600 baud, 8 data bits, no parity, 1 stop bit). *)
dwPortSettings := MB_PORT_SETTINGS(MB_PARITY_NONE, MB_STOPBIT_1, MB_BPS_9600);

(* Call every Modbus POU so the test program compiles and exercises all of
   them. The calls intentionally overlap on the same ports (master + slave +
   Mitsubishi protocol) and do not make logical sense together - they are
   here for compile/debug coverage only.
   M8013 = 1 s clock: re-triggers the initialisation every second so the
   port settings survive the startup overwrite. *)
M0 := MB_MASTER_INIT_PORT2(M8013, dwPortSettings);
M0 := MB_MASTER_INIT_PORT3(M8013, dwPortSettings);
M0 := MB_SLAVE_INIT_PORT2(M8013, 1, dwPortSettings);
M0 := MB_SLAVE_INIT_PORT3(M8013, 1, dwPortSettings);
M0 := MB_SLAVE_INIT_TCP(M8013, 502); (* L02 Ethernet port as Modbus TCP server *)
M0 := MB_MASTER_INIT_TCP(MEP(M8013), 502, 1); (* L02 Ethernet port as Modbus TCP master *)
M0 := MTB_SLAVE_PORT2(M8013);
M0 := MTB_SLAVE_PORT3(M8013);

(* One-time channel configuration. *)
IF M8002 THEN
    (* Timeout and clear-on-start tuning are passed to fbMbProcess below. *)

    (* Channel 0 - register, automatic read/write (device 1, port 2). *)
    MB_CHANNELS[0].xEnabled := TRUE;
    MB_CHANNELS[0].iDDevNum := 600;      (* D600 - value buffer *)
    MB_CHANNELS[0].iNum := 3;
    MB_CHANNELS[0].iReg := K16384;
    MB_CHANNELS[0].iRF := H3;            (* read holding registers *)
    MB_CHANNELS[0].iWF := H6;            (* write single register *)
    MB_CHANNELS[0].iDev := H1;
    MB_CHANNELS[0].tCycle := 20;         (* 20 x 50 ms = 1 s *)
    MB_CHANNELS[0].iWR := MB_READ_WRITE;
    MB_CHANNELS[0].iPort := MB_PORT_2;
    MB_CHANNELS[0].xWriteOnChange := TRUE;

    (* Channel 1 - register, read-only, on demand (device 2, port 3). *)
    MB_CHANNELS[1].xEnabled := TRUE;
    MB_CHANNELS[1].iDDevNum := 610;      (* D610 - value buffer *)
    MB_CHANNELS[1].iNum := 3;
    MB_CHANNELS[1].iReg := K16385;
    MB_CHANNELS[1].iRF := H3;            (* read holding registers *)
    MB_CHANNELS[1].iWF := H6;            (* unused - channel is read-only *)
    MB_CHANNELS[1].iDev := H2;
    MB_CHANNELS[1].tCycle := 0;          (* manual only *)
    MB_CHANNELS[1].iWR := MB_READ;
    MB_CHANNELS[1].iPort := MB_PORT_3;

    (* Channel 2 - coils, read/write (device 3, port 2). *)
    MB_CHANNELS[2].xEnabled := TRUE;
    MB_CHANNELS[2].iDDevNum := 500;      (* M500 - coil channel uses M space *)
    MB_CHANNELS[2].iNum := 8;
    MB_CHANNELS[2].iReg := K0;
    MB_CHANNELS[2].iRF := H1;            (* read coils *)
    MB_CHANNELS[2].iWF := H5;            (* write single coil *)
    MB_CHANNELS[2].iDev := H3;
    MB_CHANNELS[2].tCycle := 10;         (* 10 x 50 ms = 500 ms *)
    MB_CHANNELS[2].iWR := MB_READ_WRITE;
    MB_CHANNELS[2].iPort := MB_PORT_2;
    MB_CHANNELS[2].xWriteOnChange := TRUE;
END_IF;

(* Channel scheduler - call every scan. *)
fbMbProcess(mb_xEnable := TRUE, mb_iBuffer := 200,
    mb_iClearOnStart := MB_CLEAR_ALL,
    mb_tStartDelay := T#5s,    
    mb_iTimeoutCount := 2,
    mb_iSuspendRetry := 80,
    mb_iTimeoutTime := 4);

(* On-demand read of channel 1 (register, read-only). *)
IF M1 THEN
    MB_CHANNELS[1].xReadOnce := TRUE;
    IF MB_CHANNELS[1].xDone THEN
        M1 := FALSE;
        MB_CHANNELS[1].xReadOnce := FALSE;
    END_IF;
END_IF;

(* Demo: write a value to channel 0 - written immediately (xWriteOnChange).
   D600 is the value buffer of channel 0 (iDDevNum := 600). *)
IF M2 THEN
    D600 := 1234;
    M2 := FALSE;
END_IF;

(* Demo: toggle a coil on channel 2 - written immediately (xWriteOnChange).
   M500 is the value buffer of channel 2 (iDDevNum := 500). *)
IF M3 THEN
    M500 := NOT M500;
    M3 := FALSE;
END_IF;

(* --- Remote EIP I/O access (this library) --- *)
xDi1 := EIP_DI(EIP_REMOTE1, 0);              (* reads M5000 *)
xDo1 := EIP_DO(EIP_REMOTE2, 5);              (* reads M6205 *)
iAi1 := EIP_AI(EIP_REMOTE1, 2);              (* reads R23102 *)
iAo1 := EIP_AO(EIP_REMOTE3, 0);              (* reads R23400 *)
xDoSet := EIP_DO_SET(EIP_REMOTE2, 5, TRUE);  (* M6205 := TRUE *)
iAoSet := EIP_AO_SET(EIP_REMOTE3, 0, 1000);  (* R23400 := 1000 *)
