You are not registered yet. Please click here to register!


 
 
plc storereviewsdownloads
This board is for PLC Related Q&A ONLY. Please DON'T use it for advertising, etc.
 
Try our online PLC Simulator- FREE.  Click here now to try it.

---------->>>>>Get FREE PLC Programming Tips

New Here? Please read this important info!!!


Go Back   PLCS.net - Interactive Q & A > PLCS.net - Interactive Q & A > LIVE PLC Questions And Answers

Reply
 
Thread Tools Display Modes
Old May 22nd, 2020, 04:48 AM   #1
neilsibub
Member
United Kingdom

neilsibub is offline
 
Join Date: Aug 2012
Location: Carlisle
Posts: 19
Crimson 3.1 - Strange Array Behaviour

Hi All,

I am using a Red Lion graphite G12 panel to draw a scatter graph, all works well until I run this code every morning to reset the arrays back to zero:

// Reset graph data at 6am

if(System.Time.Day >0 || System.Time.Day <6){
if(System.Time.Hour ==5)
if(System.Time.Minute ==59 && System.Time.Seconds ==59){
Fill(System.Graph.Time_Array[0], 0, 1440);
Fill(System.Graph.Count_Array[0], 0, 1440);
System.Graph.Door_Counter = 0;}}

All the arrays in System.Graph.Count_Array zero correctly and all the arrays in System.Graph.Time_Array zero correctly except for Array [359] which always retains its value.
I have tried zeroing the array using a for loop and I get the same result, if I write a zero to that array on its own it zeros and stays like that until it is written into again.

Has anyone had anything similair?
  Reply With Quote
Old May 22nd, 2020, 08:05 AM   #2
OkiePC
Lifetime Supporting Member
United States

OkiePC is offline
 
OkiePC's Avatar
 
Join Date: Mar 2005
Location: ENE of Nowhere Oklahoma
Posts: 10,552
Something else is probably overwriting it right after your code runs.

Are those arrays tied to PLC addresses?

If so, and the HMI attempts to write to all those addresses very quickly, perhaps there is a driver limitation affecting it, but if that were the case, I'd expect more than one value not to be cleared.
__________________
You can choose a ready guide in some celestial voice. If you choose not to decide you still have made a choice.
  Reply With Quote
Old May 22nd, 2020, 08:37 AM   #3
drbitboy
Lifetime Supporting Member
United States

drbitboy is offline
 
drbitboy's Avatar
 
Join Date: Dec 2019
Location: Rochester, NY
Posts: 845
hmm, no similar experience.


Array[359] is the last element of the first quarter of the array. What happens if the array is made larger (e.g. 1441, 1444, 1600, 5764) and it clears that (1440 or the full size), although the code would use only the first 1440.



At least you have a workaround, assuming the write of a zero works in the code.


what happens if the Fill is run twice?


sidebar: why no curly braces for if(...Hour)?
  Reply With Quote
Old May 22nd, 2020, 08:53 AM   #4
BryanG
Member
United Kingdom

BryanG is offline
 
Join Date: Feb 2005
Location: Manchester
Posts: 1,481
Just to check it isn't strange, try edit/find global/find, and enter Time_Array[359] as your search parameter.

You could try swapping the line over:
Fill(System.Graph.Count_Array[0], 0, 1440);
Fill(System.Graph.Time_Array[0], 0, 1440);
to see if it changes anything

or
Fill(System.Graph.Time_Array[0], 0, 720);
Fill(System.Graph.Time_Array[0], 720, 720);


Otherwise give Tech Support a contact, they are generally very helpful and knowledgeable.:
https://support.redlion.net/hc/en-us


If I have problems I tend to stick a sleep(500); in somewhere to give the system chance to catch it breath.
__________________
Knowledge is power, share the knowledge.
  Reply With Quote
Old May 22nd, 2020, 09:17 AM   #5
neilsibub
Member
United Kingdom

neilsibub is offline
 
Join Date: Aug 2012
Location: Carlisle
Posts: 19
Quote:
Originally Posted by OkiePC View Post
Are those arrays tied to PLC addresses?
No, they are all generated by the HMI, I am using them to draw a live minute by minute line graph, running from 6am to 5:59am the next day, they just count up from 0 to 1439 (1440 mins) then get zeroed and start again.
  Reply With Quote
Old May 22nd, 2020, 09:22 AM   #6
neilsibub
Member
United Kingdom

neilsibub is offline
 
Join Date: Aug 2012
Location: Carlisle
Posts: 19
Quote:
Originally Posted by drbitboy View Post
hmm, no similar experience.


Array[359] is the last element of the first quarter of the array. What happens if the array is made larger (e.g. 1441, 1444, 1600, 5764) and it clears that (1440 or the full size), although the code would use only the first 1440.



At least you have a workaround, assuming the write of a zero works in the code.


what happens if the Fill is run twice?
I will try these when I'm back at my desk on Tuesday and report back.
  Reply With Quote
Old May 22nd, 2020, 09:24 AM   #7
neilsibub
Member
United Kingdom

neilsibub is offline
 
Join Date: Aug 2012
Location: Carlisle
Posts: 19
Quote:
Originally Posted by BryanG View Post
Just to check it isn't strange, try edit/find global/find, and enter Time_Array[359] as your search parameter.

You could try swapping the line over:
Fill(System.Graph.Count_Array[0], 0, 1440);
Fill(System.Graph.Time_Array[0], 0, 1440);
to see if it changes anything

or
Fill(System.Graph.Time_Array[0], 0, 720);
Fill(System.Graph.Time_Array[0], 720, 720);
I will also give these a go and let you know the outcome.
  Reply With Quote
Old May 22nd, 2020, 09:27 AM   #8
neilsibub
Member
United Kingdom

neilsibub is offline
 
Join Date: Aug 2012
Location: Carlisle
Posts: 19
Quote:
Originally Posted by drbitboy View Post
sidebar: why no curly braces for if(...Hour)?
I've always been a bit confused about the curly brace rule, I always indent my if statements to make it easier to read and sometimes forget to add a curly brace, but it still seems to work. Should I have curly braces around every if statement?
  Reply With Quote
Old May 22nd, 2020, 10:12 AM   #9
drbitboy
Lifetime Supporting Member
United States

drbitboy is offline
 
drbitboy's Avatar
 
Join Date: Dec 2019
Location: Rochester, NY
Posts: 845
Quote:
Originally Posted by neilsibub View Post
I've always been a bit confused about the curly brace rule, I always indent my if statements to make it easier to read and sometimes forget to add a curly brace, but it still seems to work. Should I have curly braces around every if statement?

not really necessary in those cases, but it's safer to, and eliminates the need to memorize precedence rules (i.e. lazy wins).


TL;DR



In many languages, the [if (expression)] applies to exactly and only one statement* after it, and that statement can be, among other things**,

  • <statement without trailing substatement>
  • <if then statement>
  • <for statement>
  • ...
and that <statement without trailing substatement>, can be, among other things

  • a single thingie with a semicolon <summat;> *** or
  • a curly-braced block of thingies { summat; summat; ... }. Also, the

Basically it's all defined so it is unambiguous to say which <if> any later <else> pairs with.



That's the long way around to say that

  • summat;
AND
  • { summat; summat; ...}
AND

  • if (...) summat;
AND

  • if (...) { summat; summat; .. }
are each considered a <statement> in the formal BNF sense, and so can be that "exactly and only one statement" after an <if (...)>


So looking at the code in the OP, none of the curly braces are needed except for the innermost if, because each is <if> is a single "statement." That said, save yourself from having to waste more neurons than necessary (like me) and use curly braces and indentation so you know what you meant.



* the semantics may change but the concept is fairly universal
** from Java BNF I found here
*** I used thingie because the BNF already used statement and the lingo gets too busy after that

Last edited by drbitboy; May 22nd, 2020 at 10:15 AM.
  Reply With Quote
Old May 22nd, 2020, 10:47 AM   #10
neilsibub
Member
United Kingdom

neilsibub is offline
 
Join Date: Aug 2012
Location: Carlisle
Posts: 19
Quote:
Originally Posted by drbitboy View Post
So looking at the code in the OP, none of the curly braces are needed except for the innermost if, because each is <if> is a single "statement." That said, save yourself from having to waste more neurons than necessary (like me) and use curly braces and indentation so you know what you meant.
Thanks for that thorough explanation, if I understand you properly, my code should look like this?
// Reset graph data at 6am

if(System.Time.Day >0 || System.Time.Day <6){
if(System.Time.Hour ==5){
if(System.Time.Minute ==59 && System.Time.Seconds ==59){
Fill(System.Graph.Time_Array[0], 0, 1440);
Fill(System.Graph.Count_Array[0], 0, 1440);
System.Graph.Door_Counter = 0;}}}
  Reply With Quote
Old May 22nd, 2020, 10:49 AM   #11
neilsibub
Member
United Kingdom

neilsibub is offline
 
Join Date: Aug 2012
Location: Carlisle
Posts: 19
Quote:
Originally Posted by neilsibub View Post
Thanks for that thorough explanation, if I understand you properly, my code should look like this?
// Reset graph data at 6am

if(System.Time.Day >0 || System.Time.Day <6){
if(System.Time.Hour ==5){
if(System.Time.Minute ==59 && System.Time.Seconds ==59){
Fill(System.Graph.Time_Array[0], 0, 1440);
Fill(System.Graph.Count_Array[0], 0, 1440);
System.Graph.Door_Counter = 0;}}}
I did indent all this but they seem to get removed.
  Reply With Quote
Old May 22nd, 2020, 11:13 AM   #12
drbitboy
Lifetime Supporting Member
United States

drbitboy is offline
 
drbitboy's Avatar
 
Join Date: Dec 2019
Location: Rochester, NY
Posts: 845
Quote:
Originally Posted by neilsibub View Post
I did indent all this but they seem to get removed.



yes, it will work as you had it, but it could be like that; the nice thing about always using curly braces, even if they are unnecessary because there is only one line, is that you can add lines later. The number of times I have added a line in that situation and then the code didn't work are beyond counting.


For showing code here, put it in code blocks i.e. put a "[code]" in front, and a "[<slash>code]" after it where <slash>code is /code.


E.g. reply to this and you should see how it is done


Code:
indent0
  indent2
  indent2
    indent4

Last edited by drbitboy; May 22nd, 2020 at 11:26 AM.
  Reply With Quote
Old May 22nd, 2020, 11:33 AM   #13
neilsibub
Member
United Kingdom

neilsibub is offline
 
Join Date: Aug 2012
Location: Carlisle
Posts: 19
Quote:
Originally Posted by drbitboy View Post
yes, it will work as you had it, but it could be like that; the nice thing about always using curly braces, even if they are unnecessary because there is only one line, is that you can add lines later. The number of times I have added a line in that situation and then the code didn't work are beyond counting.


For showing code here, put it in code blocks i.e. put a "[code]" in front, and a "[<slash>code]" after it where <slash>code is /code.


E.g. reply to this and you should see how it is done


Code:
indent0
  indent2
  indent2
    indent4
Code:
// Reset graph data at 6am

if(System.Time.Day >0 || System.Time.Day <6){
   if(System.Time.Hour ==5){
     if(System.Time.Minute ==59 && System.Time.Seconds ==59){
        Fill(System.Graph.Time_Array[0], 0, 1440);
        Fill(System.Graph.Count_Array[0], 0, 1440);
        System.Graph.Door_Counter = 0;}}}
Everyday is a school day!
  Reply With Quote
Old May 22nd, 2020, 11:49 AM   #14
drbitboy
Lifetime Supporting Member
United States

drbitboy is offline
 
drbitboy's Avatar
 
Join Date: Dec 2019
Location: Rochester, NY
Posts: 845
Quote:
Originally Posted by neilsibub View Post
Code:
// Reset graph data at 6am

if(System.Time.Day >0 || System.Time.Day <6) {
   if(System.Time.Hour ==5) {
     if(System.Time.Minute ==59 && System.Time.Seconds ==59) {
        Fill(System.Graph.Time_Array[0], 0, 1440);
        Fill(System.Graph.Count_Array[0], 0, 1440);
         System.Graph.Door_Counter = 0;
     }
   }
}
Everyday is a school day!




The funny thing is that that code, the way you had it, but without the curly braces, works in Python (there are no braces; indentation is mandatory). Also, I edited it above to demonstrate a common style convention for the closing braces that is perhaps more clear but wastes a few lines.


N.B. style conventions can start wars on the 'net
  Reply With Quote
Old May 22nd, 2020, 11:55 AM   #15
drbitboy
Lifetime Supporting Member
United States

drbitboy is offline
 
drbitboy's Avatar
 
Join Date: Dec 2019
Location: Rochester, NY
Posts: 845
Quote:
Originally Posted by neilsibub View Post
Code:
if(System.Time.Day >0 || System.Time.Day <6){
...

I don't know what the Red Lion day-of-week convention is here, but whether it's [0=Sunday to 6=Saturday] or [1=Monday to 7=Sunday], I am pretty sure this code runs seven days a week; should that be && instead of ||?


Also, if you care about not doing this on the weekend, then I have to ask why: if it's performance, then you want to invert the order of the if-expressions i.e. System.Time.Seconds outermost, then .Minutes, etc.


I've got to get a life.

Last edited by drbitboy; May 22nd, 2020 at 11:56 AM. Reason: its => it's
  Reply With Quote
Reply
Jump to Live PLC Question and Answer Forum

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Topics
Thread Thread Starter Forum Replies Last Post
Crimson 3.1 Crashes Shell Application CesarTamayo LIVE PLC Questions And Answers 3 November 19th, 2019 12:20 PM
Array issues in Crimson 3.1 Bullzi LIVE PLC Questions And Answers 3 July 5th, 2019 03:19 AM
Red Lion Crimson 3.1 writing array position into tag ijustdunnoaname LIVE PLC Questions And Answers 5 April 26th, 2019 09:43 AM
Sorting a Crimson 3 integer array in ascending order bbaker123 LIVE PLC Questions And Answers 1 February 22nd, 2016 03:00 PM
Variable array size call to add-on instruction - A solution Mr Modbus LIVE PLC Questions And Answers 4 July 17th, 2015 08:59 AM


All times are GMT -4. The time now is 04:39 AM.


.