Home
JAQForum Ver 20.06
Log In or Join  
Active Topics
Local Time 13:07 28 Apr 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Open/Close weirdo....

Author Message
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9063
Posted: 03:03am 25 Feb 2017
Copy link to clipboard 
Print this post

This has me a little confused.

I have two subs that I call as and when needed, to log system errors and other system activity. They usually work fine, but tonight they have started doing odd things.

The two routines are:





...and the error I am getting from this code is:





...yet, if I run these directly from the command prompt, there is no problem:





It's damn peculiar. I have searched for all instances of "#4" in the code using MMEDIT's search feature, and have confirmed that each and every time that handle #4 is opened, it is always closed again. It is never left open.

Note also that as part of the SUB being called, I close that handle before leaving the routine. The next time I call that routine, I get this error message about the handle already being open.

BUT, I can call either sub from the command line as many times as I like - no error message, but when the code is running, I do. This suggests that I have accidentally left handle #4 open somewhere else, but according to my MMEDIT search for "#4", anywhere I open it, it also gets closed.

2AM so time to stop - my brain is not working so well at this time of night, but if anyone has any ideas of things for me to look for, I would appreciate the prod.

EDIT: One last thing before bed, I tried simply putting in a "Close #4" just before the open command, but then I get a different error: "File number is not open.", along with mm.errno being 16. If I remove that CLOSE #4 and run the code again, I get the other error about the file already being open - WTF?! It can't be both....



Edited by Grogster 2017-02-26
Smoke makes things work. When the smoke gets out, it stops!
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 3659
Posted: 03:45am 25 Feb 2017
Copy link to clipboard 
Print this post

In case you might call one as a result of an error, that might cause it.

(Suppose DEBUG was garbage, or ON ERROR something or ...)

You could reduce the window for such by moving the CLOSE up a line and by setting TXT$ before the OPEN.

JohnEdited by JohnS 2017-02-26
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 1985
Posted: 05:37am 25 Feb 2017
Copy link to clipboard 
Print this post

Hi Grogster.

What PIC is this running on?

check also that you don't have errors in your error handler (unlikely if it has been unaltered for some time) - if you get an error and jump to your handler, which also has an error, which jumps to your handler... you would be trying to open the same stream number multiple times.

I have learned that when I get weird crap start to happen in stuff that has been running for ages, I flash the PIC. Has fixed me up a couple of times. It seems the 32MXs like to corrupt their core every so often.

Is this the same system you had your odd GUI problem with?

h

Edited by CaptainBoing 2017-02-26
 
disco4now

Guru

Joined: 18/12/2014
Location: Australia
Posts: 844
Posted: 07:26pm 25 Feb 2017
Copy link to clipboard 
Print this post

Hi Grogster,

If I add a tick timer as below and call your sub while they are being used in the main loop then the errors you are experiencing show up.

SETTICK 500, tick1

sub tick1
WRITE_ERRLOG("HELLO WORLD two")
end sub



I think the
PRINT #4,TXT$

statement is processor friendly and knows it could take awhile so completes in the background. This means any interrupts (e.g settick,touch down etc) can be serviced while its writing to file. If one on these also writes an error then the SUB is called again before its finished, so the file OPEN/CLOSE confusion happens.

The code below works by having two separate functions.
ADD_ERROR(a$) which puts any pending errors into a list, and
WRITE_ERRORLOG() which is only ever called from the main loop and writes the
errors in the list to file.
Run the code below and you will see it mostly wring 1 error, but as the tick happens is writes 3 at once.

hope this makes sense.

Regards
Gerry









  Quote  
DIM ERRORS$(10),ecount=0
SETTICK 500, tick1


do
for x= 1 to 50
CHDIR "\"
ADD_ERROR(
"HELLOW WORLD1 ")
WRITE_ERRLOG()
CHDIR "\LOG"
next x

' Just open and close so its not getting too big
OPEN "ERRORS.LOG" for OUTPUT AS #4
CLOSE #4
LOOP


SUB WRITE_ERRLOG()
LOCAL TXT$
CHDIR "\LOG\"
OPEN "ERRORS.LOG" for APPEND AS #4
if ecount>0 then
scount=ecount:ecount=
0
for x=1 to scount
TXT$=
"["+DATE$+", "+TIME$+"] - "+errors$(x)
? x
PRINT #4,TXT$
next x
end if
CLOSE #4
end SUB


SUB ADD_ERROR(A$)
ecount=ecount+
1
errors$(ecount)=a$
end SUB

SUB tick1

ADD_ERROR(
"HELLOW WORLD 2" )
ADD_ERROR(
"HELLOW WORLD 3" )
'WRITE_ERRLOG("HELLO WORLD two")


end SUB


Latest F4 Latest H7
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9063
Posted: 08:18pm 25 Feb 2017
Copy link to clipboard 
Print this post

Hi all. Thanks for the replies.

[Quote=CaptainBoing]s this the same system you had your odd GUI problem with?[/Quote]

Yes, but that problem was TOTALLY my fault, as I was deleting controls as soon as you touched them, in order to create new ones on the new page. I now define all the controls, and use the GUI SETUP commands to organise all my pages, and this is working much, much better.

  disco4now said   I think the
PRINT #4,TXT$
statement is processor friendly and knows it could take awhile so completes in the background. This means any interrupts (e.g settick,touch down etc) can be serviced while its writing to file. If one on these also writes an error then the SUB is called again before its finished, so the file OPEN/CLOSE confusion happens.


Nice thinking - that will be it exactly!

I am not using any tick timers in this code, all processing is done in the main loop, and the serial port buffer acts beautifully as a message queue, allowing them to build up there if they have to, and they will be processed once the code makes it back to the main loop. That way, no messages are dropped or missed, as the serial port queue holds on to them for me. Ahhhh, those beautiful serial port buffers.....

I AM however, using touch interrupts, so I will study this in a bit more detail, as I THINK I know how this is happening now that you mention the above - touching the "OK" button on the LCD when a message arrives, will push the code back to the main menu, at which point it processes the next message, and COULD loop to the errors or system log subs, and try to open the file when it is not finished with it yet from the last message.

I think I will fix this by adding something like: GUI INTERRUPT 0 and DO:LOOP UNTIL LOF(#4)=255 to FORCE the logging routines to complete cleanly and close the file BEFORE being allowed to exit the sub, or respond to touch again.

I would have thought that writing to the SD card would have been so fast as to not have to really worry about this issue, but there you go. I will keep the thread updated.
Smoke makes things work. When the smoke gets out, it stops!
 
MicroBlocks

Guru

Joined: 12/05/2012
Location: Thailand
Posts: 2209
Posted: 05:58am 26 Feb 2017
Copy link to clipboard 
Print this post

If writing to a SD card is non-blocking then there should be a way to poll for its status so that you have the possibility to know when it is finished.

If it is really non-blocking then a CLOSE should be blocking. so that you are sure that after the close all data is written and the file properly closed.
I think this one is for Geoff as what you experience is not how it should be.

Microblocks. Build with logic.
 
panky

Guru

Joined: 02/10/2012
Location: Australia
Posts: 1094
Posted: 08:08pm 26 Feb 2017
Copy link to clipboard 
Print this post

@G

I am pretty sure it is a re-entrance problem as I can simulate here with the same error message (MMX running 5.3)

By setting a flag to indicate the handle is open prior to the open and clearing it after the close, a test within each routine to wait until the other user of handle #4 has finnished then makes sure there are no conflicts.

The test code below shows how it would work.



' Testing File re-entrance code
MAIN:
Print "Call error log subroutine at "+Time$
SetTick 1000,tick
t1$="test1"
'*** clear the file handle flag for initial use
handle_open_flag = 0
'***
do
' now call error log routine
write_errorlog t1$
' we should have hung in there for 5 secs but we will get
' an interrupt in 1 sec to run sys log
Pause 100
loop
End

' we just want an interrupt every second to test the re-entrance
Sub tick
write_syslog t1$
End Sub


'-----------------------
' ERROR LOGGING ROUTINE
'-----------------------
Sub WRITE_ERRORLOG(T1$)
print "writing error log at "+time$
Local TXT$
Chdir "\LOG\"
'*** open file handle test
do while handle_open_flag=1
loop
handle_open_flag=1
'****
Open "ERRORS.LOG" For APPEND As #4
TXT$="["+Date$+", "+Time$+"] - "+TXT$
Print #4,TXT$
If DEBUG Then Print "ERROR LOG - "+TXT$
'**
' pause here to allow simulated re-entrance from tick interrupt
' delete this pause for normal usage
Pause 5000
'**
Close #4
' *** now clear the open file handle flag
handle_open_flag=0
'***
End Sub

'------------------------
' SYSTEM LOGGING ROUTINE
'------------------------
Sub WRITE_SYSLOG(T1$)
Local TXT$
print "Writing sys log at "+time$
Chdir "\LOG\"
'*** open file handle test
do while handle_open_flag=1
loop
handle_open_flag=1
'****
Open "SYSTEM.LOG" For APPEND As #4
TXT$="["+Date$+", "+Time$+"] - "+TXT$
Print #4,TXT$
If DEBUG Then Print "SYSTEM LOG - "+TXT$
Close #4
' *** now clear the open file handle flag
handle_open_flag=0
'***
End Sub

Cheers Doug

... almost all of the Maximites, the MicromMites, the MM Extremes, the ArmMites, the PicoMite and loving it!
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9063
Posted: 01:38am 27 Feb 2017
Copy link to clipboard 
Print this post

@ Jean - Yes, I too would love to know if the SD writing is non-blocking or not. My own personal guess is that it is NOT, in that it will be buffered so the main program can carry on with other things. How that works exactly, we would need Geoff or Peter(matherp) to comment on as they know the source well(so does kiiid). My guess would be that the PRINT #4 command posts the data to the BUFFER and not the card, and the CLOSE #4 flushes the buffer to the file, and closes it. However, flushing the buffer and closing the file takes a little time, and I think this could be where we are having the issue.

@ Doug - Yes, that looks like an ideal way to get around the issue, and I will implement that concept in the next few days. Special thanks to you for taking the time to play with the code, and discover the errors that I was getting. That makes me feel better in that it is a provable error, and thus we now have a workaround.

I will let the forums know how I get on.
Smoke makes things work. When the smoke gets out, it stops!
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 8582
Posted: 01:45am 27 Feb 2017
Copy link to clipboard 
Print this post

I've recently moved the Micromite eXtreme to FatFs rather than the original Microchip file system. This definitely works on the Basis that writes take place to a buffer and that is flushed to disk whenever the buffer is full (512 bytes) or at a file close.
FatFs also implements a SYNC command which forces a buffer flush without closing the file. It would be easy to implement this as an additional capability, possibly as an option as part of the OPEN command.

Geoff is looking to also move to FatFs for V5.4 so it probably isn't worth doing anything on the Microchip stack.
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9063
Posted: 01:53am 27 Feb 2017
Copy link to clipboard 
Print this post

Understood.
Thanks for that information.


For now, we have a work-around, so I will implement that for now and that should get me out of the problem I was having.

I also agree with you - probably not worth adding anything to the Microchip stack, if Geoff is considering a change in the filesystem code anyway.
Smoke makes things work. When the smoke gets out, it stops!
 
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024