Good morning,
I'm a newby to codesys (V3.5.7), I'm currently reading the 'input variable' by modbus by using the slave modbus/Channel slave modbus/Read Holding Registers and declaring them in global variable.
I can write on this node as well by using 'Write Multiple Register' but I need to write the value only when I change it, not cyclically.
To better explain, I have a net in which the same value should be written from local (plc on the machine) or remote (plc controlled by codesys). I have to maintain the last value written (from local or from remote, same importance). I'm sure it should be done quickly but I can't find the solutions. Any suggestions?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi
i think you should be able to do it with the 'Rising Edge'. You just Need a variable that would be triggered when the Value changes and this could be mapped to the 'Rising Edge' variable.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
2 writing, one is for the trigger variable and the other for the value I want to change.
1 reading, that is pointed to the same value of the second reading.
I was espected that if I put one on the trigger variable (which come to true), the value on the writing should pass to the reading as well. But no way to connect the trigger variable to the action that identify the changing of the value on the plc?
Thank you very much.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hello,
I would like to do smt simmilar.. I want to save value everytime it changes and cyclicly every 15 min for ex.. How i detect change? Did you succed to write program for it?
Thank you.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
just read the source variable cyclically to know the value. Then compare with one you previously saved. If values are different, then trigger your write.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Well.. I have problem with comparing bytes. With BOOL variable works fine:
IF NOT new = Old THEN
Save := TRUE;
Old := New;
END_IF
With BOOL All good, Save goes TRUE but with BYTE not.. Nothing happens.. Any ideas?
Update: I used memcmp and memcpy. It works now.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2019-02-21
Originally created by: scott_cunningham
The statement IF NOT xxx = yyy will only work for BOOLs. For other types simply use IF New <> Old. Use memcmp when you have complicated variables like structures or arrays - I am not sure this always works: IF structX = structY... (aka deep compare)
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Yes I used memcmp and memcpy and it worked for comparing it. But.. How do I say I want to know if the difference is more then 5 for ex. MEMCMP has outputs just less, equal or more..
Just comparing:
IFMEMCMP(pBuf1 :=ADR(Byte1) , pBuf2:=ADR(SaveByte1) , n:=1)<>0THEN
    Trig_Byte1 :=TRUE;
    MEMCPY(destAddr:=ADR(SaveByte1) , srcAddr:=ADR(Byte1) , n:=1);END_IF
Update:
Smt like that: save input into pointer, save also saved value (new value with thich you compare to the pointer) that compare that..
VARInput_Byte1Â Â AT%IB1 Â Â : BYTE; //our read variablenew_Byte1Â Â Â Â Â Â : BYTE;//variable into which we save it
Â
pBUf1  : POINTERTOUDINT; //pointer for our variablepBuf2  : POINTERTOUDINT;//pointer to variable where we save readed variabledelta    : UDINT :=5;END_VARpBuf1   :=Input_Byte1;pBuf2  :=new_Byte1CASEnSTEPOF0:
  MEMCPY(destAddr:=ADR(new_Byte1), srcAddr :=ADR(Input_byte1),n:=1);
  nStep :=1;
 Â
1:
  IF((pbuf1-pBuf2)>delta)OR((pBuf1-pbuf2<(-delta))THEN
  MEMCPY(destAddr:=ADR(new_Byte1), srcAddr :=ADR(Input_byte1),n:=1); //save if delta
  END_IFEND_CASE
 Â
 Â
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2019-02-26
Originally created by: scott_cunningham
Did you actually test this, because when you do this: pbuf1- pBuf2, you are asking the compiler to subtract pointer positions, not the value inside...
If you have UDINTs to compare:
Val1 : UDINT;Val2 : UDINT;IFABS(Val2-Val1)>5THEN
  ...
END_IF
You need the Absolute value in case Val1 is bigger than Val2. Again, you can do math and less than/greater than/equal compares with any number data type.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I tested it with real inputs and looks like that my delta was approx equally big as the position difference. I look it one more time now a bit longer and yes you have right. THANK YOU!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I have the same issue with Modbus Master and writing to coils.
The GVL.CoilIn variable is mapped to a reading channel that is polling the server in "Cyclic" mode.
The GVL.CoilOut variable is mapped to the same coil in writing channel in "Rising edge" mode.
The code below is toggling the coil on the Modbus Server every time the variable GVL.Output0 is set to TRUE.
What I want is the Modbus coil to be set to 1 when the GVL.Output0 is TRUE and reset to 0 when then GVL.Output0 is FALSE.
I tried to take the falling edge of the GVL.Output0 to reset the coil to 0 but it doesn't work.
Any suggestions?
GVL.Input0 :=GVL.CoilIn0;R_TRIG_0(CLK:=GVL.Output0);IFR_TRIG_0.QTHEN
  IFGVL.CoilIn0THEN
    GVL.CoilOut0 :=FALSE;
  ELSE
    GVL.CoilOut0 :=TRUE;
  END_IFEND_IF
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Made it work for coils and now have to find a way so it works for holding registers as well.
GVL.Input0 :=GVL.CoilIn;Write :=FALSE;R_TRIG_0(CLK:=GVL.Output0);F_TRIG_0(CLK:=GVL.Output0);//TriggerforModbuscoilwriteWrite :=R_TRIG_0.QORF_TRIG_0.Q;IFWriteTHEN
  IFGVL.Input0THEN
    GVL.CoilOut :=FALSE;
  ELSE
    GVL.CoilOut :=TRUE;
  END_IF
  Write :=TRUE;END_IF
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Good morning,
I'm a newby to codesys (V3.5.7), I'm currently reading the 'input variable' by modbus by using the slave modbus/Channel slave modbus/Read Holding Registers and declaring them in global variable.
I can write on this node as well by using 'Write Multiple Register' but I need to write the value only when I change it, not cyclically.
To better explain, I have a net in which the same value should be written from local (plc on the machine) or remote (plc controlled by codesys). I have to maintain the last value written (from local or from remote, same importance). I'm sure it should be done quickly but I can't find the solutions. Any suggestions?
Hi
i think you should be able to do it with the 'Rising Edge'. You just Need a variable that would be triggered when the Value changes and this could be mapped to the 'Rising Edge' variable.
Thankyou very much for the respons.
I made something like this:
2 writing, one is for the trigger variable and the other for the value I want to change.
1 reading, that is pointed to the same value of the second reading.
I was espected that if I put one on the trigger variable (which come to true), the value on the writing should pass to the reading as well. But no way to connect the trigger variable to the action that identify the changing of the value on the plc?
Thank you very much.
Hello,
I would like to do smt simmilar.. I want to save value everytime it changes and cyclicly every 15 min for ex.. How i detect change? Did you succed to write program for it?
Thank you.
just read the source variable cyclically to know the value. Then compare with one you previously saved. If values are different, then trigger your write.
Well.. I have problem with comparing bytes. With BOOL variable works fine:
IF NOT new = Old THEN
Save := TRUE;
Old := New;
END_IF
With BOOL All good, Save goes TRUE but with BYTE not.. Nothing happens.. Any ideas?
Update: I used memcmp and memcpy. It works now.
Originally created by: scott_cunningham
The statement IF NOT xxx = yyy will only work for BOOLs. For other types simply use IF New <> Old. Use memcmp when you have complicated variables like structures or arrays - I am not sure this always works: IF structX = structY... (aka deep compare)
Hello,
Yes I used memcmp and memcpy and it worked for comparing it. But.. How do I say I want to know if the difference is more then 5 for ex. MEMCMP has outputs just less, equal or more..
Just comparing:
Update:
Smt like that: save input into pointer, save also saved value (new value with thich you compare to the pointer) that compare that..
Originally created by: scott_cunningham
Did you actually test this, because when you do this: pbuf1- pBuf2, you are asking the compiler to subtract pointer positions, not the value inside...
If you have UDINTs to compare:
You need the Absolute value in case Val1 is bigger than Val2. Again, you can do math and less than/greater than/equal compares with any number data type.
I tested it with real inputs and looks like that my delta was approx equally big as the position difference. I look it one more time now a bit longer and yes you have right. THANK YOU!
I have the same issue with Modbus Master and writing to coils.
The GVL.CoilIn variable is mapped to a reading channel that is polling the server in "Cyclic" mode.
The GVL.CoilOut variable is mapped to the same coil in writing channel in "Rising edge" mode.
The code below is toggling the coil on the Modbus Server every time the variable GVL.Output0 is set to TRUE.
What I want is the Modbus coil to be set to 1 when the GVL.Output0 is TRUE and reset to 0 when then GVL.Output0 is FALSE.
I tried to take the falling edge of the GVL.Output0 to reset the coil to 0 but it doesn't work.
Any suggestions?
Made it work for coils and now have to find a way so it works for holding registers as well.