Unity Pro XL 4.0/Premium: ModbusTCP by READ_VAR & WRITE_VAR

другие контроллеры
Manveru
Posts: 6
Joined: Thu Jun 28, 2012 10:08 am

Unity Pro XL 4.0/Premium: ModbusTCP by READ_VAR & WRITE_VAR

Post by Manveru »

I have a following problem with the Unity Pro XL v 4.0 and Modicon Premium PLC TSX P57 1634M:

I would like to read and write coils and registers through the READ_VAR and WRITE_VAR function (or other programmable method). I wrote my program in ST. However, the output in connection management variable returns code 5 on second element at every attempt to read or write something.

What am I doing wrong? Example invocations:

Code: Select all

READ_VAR(ADDR('{1.158}0.0.254.1'), '%M', 16#1800, 1, %MW50:2, %MW108:4);
WRITE_VAR(ADDR('{1.158}0.0.0.20'), '%MW', 16#1000, 1, %MW109:4, %MW51:2);
I had a version where %MW* variables where declared variables like DATA_IN : ARRAY[0..1] OF INT. Nothing works. PLC even not made any attempt to connect.

The 1.158 XWAY address is a station defined in Ethernet_1 module in the Messaging module as MULTI with IP address of existing ModbusTCP device present in the network.

When I am using scanning feature, it works well, but can read only registers using function code 23. I need other function codes working as well.

Any help/advice appreciated.
pupena_san
Posts: 392
Joined: Fri Dec 18, 2009 3:02 pm
Location: Киев

Re: Unity Pro XL 4.0/Premium: ModbusTCP by READ_VAR & WRITE_

Post by pupena_san »

Upload Unity Project Archive file to rapidshare (*.STA). I want to see it.
Manveru
Posts: 6
Joined: Thu Jun 28, 2012 10:08 am

Re: Unity Pro XL 4.0/Premium: ModbusTCP by READ_VAR & WRITE_

Post by Manveru »

I do not have access to rapidshare from my company's network:

https://docs.google.com/open?id=0B5Z9jS ... lN4dFhUa0k is the link to download the file from Google Drive.

The project does not have much sense in terms of logic - I only want to send/receive something through Modbus/TCP.
pupena_san
Posts: 392
Joined: Fri Dec 18, 2009 3:02 pm
Location: Киев

Re: Unity Pro XL 4.0/Premium: ModbusTCP by READ_VAR & WRITE_

Post by pupena_san »

I need also the structure of the system. Also needed:
- What is a Modbus Server device, which had IP
- To which registers need to read/write (holding, or input)
- Why not set an option "Access" to this device
Manveru
Posts: 6
Joined: Thu Jun 28, 2012 10:08 am

Re: Unity Pro XL 4.0/Premium: ModbusTCP by READ_VAR & WRITE_

Post by Manveru »

I am a newbie in Unity. The device is not Schneider's one. Its IP address is 10.3.101.92 or 10.3.100.92 depending which configuration I use.
The Access is currently disabled, however I tests with that enabled - no change.

I would like to read/write different registers, for example:
  • 0x1000 read 2 words
  • 0x2000 read 7 words
  • 0x1800 write 2 words
  • 0x2800 write 4 words
Value does not matter at this moment - has to be different than zero.
pupena_san
Posts: 392
Joined: Fri Dec 18, 2009 3:02 pm
Location: Киев

Re: Unity Pro XL 4.0/Premium: ModbusTCP by READ_VAR & WRITE_

Post by pupena_san »

Manveru wrote:
I would like to read/write different registers, for example:
  • 0x1000 read 2 words
  • 0x2000 read 7 words
  • 0x1800 write 2 words
  • 0x2800 write 4 words
Value does not matter at this moment - has to be different than zero.
This is Holding Registers or Input Registers?
Manveru
Posts: 6
Joined: Thu Jun 28, 2012 10:08 am

Re: Unity Pro XL 4.0/Premium: ModbusTCP by READ_VAR & WRITE_

Post by Manveru »

I do not have exactly specified, but I think Holding Registers.
pupena_san
Posts: 392
Joined: Fri Dec 18, 2009 3:02 pm
Location: Киев

Re: Unity Pro XL 4.0/Premium: ModbusTCP by READ_VAR & WRITE_

Post by pupena_san »

Code: Select all

ADDR_NODE:='{1.158}SYS';
(*virtual_XWAY_Address {Net.Sta}SYS, for Premium Modbus/TCP always Sta>100
mapping Net.Sta to IP in configuration Ethernet network 
SYS - for access to Modbus/TCP Server*)

(* read help Structure of the Management Parameters *)
(* Management parameters are grouped together in the form of an array of four integers. The values contained in this array can be used to manage communication functions. 

   word 0 (Data managed by the system )        HiByte=Exchange_number, LoByte.0_bit=Activity_bit  
   word 1 (Data managed by the system )        HiByte=operation_report,LoByte=communication_report  
   word 2 (Data managed by the user )          TimeOut 
   word 3 (Data managed by the user or System) Length (for Read_Var and Write_Var managed by the system)  

Activity Bit. 
This bit indicates the execution status of the communication function. 
It is set to 1 when launched and returns to 0 when its execution is complete. 
This is the first bit of the first element of the table. 
Data Area for Management parameters should not overlap with the other data area! 
*)
IF PFourthSecond = 0 and not PARA1[0].0  THEN (*PARA1[0].0 - activity bits*)
READ_VAR(ADDR(ADDR_NODE),
	'%MW',(*%MW for read holding registers, %IW for read input registers*)
	16#1000,(*first object*)
	2,(*count*)
	PARA1,(*Management Parameters, is always an array of 4 int, ARRAY[0..3] OF INT*)
	DATA_RD (*is array of "count" int, ARRAY[0..1] OF INT*)
	);
end_if;

IF PFourthSecond <>0 and (not PARA2[0].0)  THEN
WRITE_VAR(ADDR(ADDR_NODE),
	'%MW', 
	16#1800,
	2,
	DATA_WR,
	PARA2 (*Management Parameters*)
	);
END_IF;
Manveru
Posts: 6
Joined: Thu Jun 28, 2012 10:08 am

Re: Unity Pro XL 4.0/Premium: ModbusTCP by READ_VAR & WRITE_

Post by Manveru »

You are great! I adopted it a little and it finally works! (suc)

I have a question because I understand more less what you have corrected, but tell me, how may I use function codes 1, 2, 4, 5 & 6? While now this program uses 3 and 16. How to set Unit ID in Modbus/TCP? While I am not using it, that may be useful to know.
pupena_san
Posts: 392
Joined: Fri Dec 18, 2009 3:02 pm
Location: Киев

Re: Unity Pro XL 4.0/Premium: ModbusTCP by READ_VAR & WRITE_

Post by pupena_san »

Manveru wrote: how may I use function codes 1, 2, 4, 5 & 6? How to set Unit ID in Modbus/TCP? While I am not using it, that may be useful to know.
Fn 01 - Read Coils: Read_Var objects '%M'
Fn 02 - Read Discrete Inputs : Read_Var objects '%I'
Fn 04 - Read Input Registers:Read_Var objects '%IW'
Fn 05 - Write Single Coil: use Unity function DATA_EXCH or SEND_REQ
Fn 06 - Write Single Holding Register: use Unity function DATA_EXCH or SEND_REQ
For acces to Modbus Slave through Modbus Gateway use Unity function DATA_EXCH or SEND_REQ
I have not tried to use functions DATA_EXCH and SEND_REQ for this purpose
Manveru
Posts: 6
Joined: Thu Jun 28, 2012 10:08 am

Re: Unity Pro XL 4.0/Premium: ModbusTCP by READ_VAR & WRITE_

Post by Manveru »

I experimented a bit and found following conclusions:

Code: Select all

READ_VAR(...,'%MW',...) reads using FC3
READ_VAR(...,'%M',...)  reads using FC1
READ_VAR(...,'%I',...)  reads using FC2
WRITE_VAR(...,'%MW',...) writes using FC16
WRITE_VAR(...,'%M',...)  writes using FC15
but what is wrong with:

Code: Select all

IF PFourthSecond = 1 and (not GEST_OUT[0].0) THEN
    WRITE_VAR(ADDR(ADDR_NODE), '%I', 16#1800, 2, DATA_OUT, GEST_OUT);
END_IF;
I do not see any error, however Wireshark show there is no communication, connection is closed and reopened only.

Additionally, I successfully address Unit ID = 20 by using

Code: Select all

ADDR_NODE := '{1.158}0.0.0.20
so it seems that my only problem was wrong format of variable passed as diagnostic output to these two functions.

But in general programming in Unity is like treading on thin ice for me.
pupena_san
Posts: 392
Joined: Fri Dec 18, 2009 3:02 pm
Location: Киев

Re: Unity Pro XL 4.0/Premium: ModbusTCP by READ_VAR & WRITE_

Post by pupena_san »

Manveru wrote: I experimented a bit and found following conclusions...
so it should work
Manveru wrote: but what is wrong with:

Code: Select all

IF PFourthSecond = 1 and (not GEST_OUT[0].0) THEN
    WRITE_VAR(ADDR(ADDR_NODE), '%I', 16#1800, 2, DATA_OUT, GEST_OUT);
END_IF;
In Modbus there is no function Write Input Bits and Write Input Registers
Manveru wrote: Additionally, I successfully address Unit ID = 20 by using

Code: Select all

ADDR_NODE := '{1.158}0.0.0.20
I suspected this, but was unable to verify
If no UnitID (read/write to device Modbus/TCP) use 'SYS',
although i think

Code: Select all

{1.158}0.0.0.0 or 
{1.158}0.0.0.255  
will work too