An unhandled exception of type 'System.InvalidOperationException' occurred in GHIElectronics.TinyCLR.IO.dll occurred

I guess you are right. Maybe the output text gives some clarification:

PT30.1835164835165
PT30.2583516483516
PT30.2583516483516
Arbitration ID: 0x00000001
Is extended ID: True
Is remote transmission request: False
Arbitration ID: 0x00000001
Is extended ID: True
Is remote transmission request: False
#### Exception System.InvalidOperationException - CLR_E_INVALID_OPERATION (3) ####
#### Message:
#### GHIElectronics.TinyCLR.IO.FileSystem+NativeFileStream::Write [IP: 0000] ####
#### System.IO.FileStream::Write [IP: 0035] ####
#### ReglerNextGen.Controller.SDstorage::WriteFile [IP: 002a] ####
#### ReglerNextGen.Controller.Program::OnMessageReceived [IP: 0369] ####
#### ReglerNextGen.Controller.Communication::RaiseMessageReceivedEvent [IP: 0013] ####
#### ReglerNextGen.Controller.Communication::Can_MessageReceived [IP: 0074] ####
#### GHIElectronics.TinyCLR.Devices.Can.CanController::<.ctor>b__3_0 [IP: 000e] ####
#### GHIElectronics.TinyCLR.Devices.Can.Provider.CanControllerApiWrapper::<.ctor>b__6_0 [IP: 0021] ####
Exception thrown: ‘System.InvalidOperationException’ in GHIElectronics.TinyCLR.IO.dll
SETPRESSURE 10.900 20 50 50 10.9 Settings 1 Could not write to SD card
#### Exception System.IO.IOException - CLR_E_UNAUTHORIZED_ACCESS (3) ####
#### Message:
#### System.IO.FileSystemManager::AddToOpenList [IP: 0082] ####
#### System.IO.FileStream::.ctor [IP: 005b] ####
#### System.IO.FileStream::.ctor [IP: 0009] ####
#### ReglerNextGen.Controller.SDstorage::ReadFile [IP: 0039] ####
#### ReglerNextGen.Controller.Program::OnMessageReceived [IP: 0377] ####
#### ReglerNextGen.Controller.Communication::RaiseMessageReceivedEvent [IP: 0013] ####
#### ReglerNextGen.Controller.Communication::Can_MessageReceived [IP: 0074] ####
#### GHIElectronics.TinyCLR.Devices.Can.CanController::<.ctor>b__3_0 [IP: 000e] ####
#### GHIElectronics.TinyCLR.Devices.Can.Provider.CanControllerApiWrapper::<.ctor>b__6_0 [IP: 0021] ####
Exception thrown: ‘System.IO.IOException’ in GHIElectronics.TinyCLR.IO.dll
An unhandled exception of type ‘System.IO.IOException’ occurred in GHIElectronics.TinyCLR.IO.dll

likely writting to sd and CAN msg come?

Yes, that is true, but how can I work around this problem?
Here is another example when writing does not succeed.

Step into: Stepping over non-user code ‘System.IO.DriveInfo.GetForPath’
Step into: Stepping over non-user code ‘System.Collections.Hashtable.GetEnumerator’
Step into: Stepping over non-user code ‘System.Collections.Hashtable.HashtableEnumerator.HashtableEnumerator’
Step into: Stepping over non-user code ‘System.Collections.Hashtable.HashtableEnumerator.HashtableEnumerator’
Step into: Stepping over non-user code ‘System.Collections.Hashtable.GetEnumerator’
Step into: Stepping over non-user code ‘System.IO.DriveInfo.GetForPath’
Step into: Stepping over non-user code ‘System.Collections.Hashtable.HashtableEnumerator.MoveNext’
Step into: Stepping over non-user code ‘System.IO.DriveInfo.GetForPath’
Step into: Stepping over non-user code ‘System.Collections.Hashtable.HashtableEnumerator.Current.get’
Step into: Stepping over non-user code ‘System.Collections.DictionaryEntry.DictionaryEntry’
Step into: Stepping over non-user code ‘System.Collections.DictionaryEntry.DictionaryEntry’
Step into: Stepping over non-user code ‘System.Collections.Hashtable.HashtableEnumerator.Current.get’
Step into: Stepping over non-user code ‘System.IO.DriveInfo.GetForPath’
Step into: Stepping over non-user code ‘System.IO.DriveInfo.GetForPath’
Step into: Stepping over non-user code ‘System.IO.DriveInfo.GetForPath’
Step into: Stepping over non-user code ‘System.IO.FileStream.FileStream’
Step into: Stepping over non-user code ‘System.IO.FileStream.FileStream’
Step into: Stepping over non-user code ‘GHIElectronics.TinyCLR.IO.FileSystem.NativeDriveProvider.OpenFile’
Step into: Stepping over non-user code ‘GHIElectronics.TinyCLR.IO.FileSystem.NativeDriveProvider.OpenFile’
Step into: Stepping over non-user code ‘System.IO.FileStream.FileStream’
Step into: Stepping over non-user code ‘System.IO.FileStream.FileStream’
Step into: Stepping over non-user code ‘System.IO.FileStream.CanRead.get’
Step into: Stepping over non-user code ‘System.IO.FileStream.CanRead.get’
Step into: Stepping over non-user code ‘System.IO.FileStream.FileStream’
Step into: Stepping over non-user code ‘System.IO.FileStream.CanWrite.get’
Step into: Stepping over non-user code ‘System.IO.FileStream.CanWrite.get’
Step into: Stepping over non-user code ‘System.IO.FileStream.FileStream’
Step into: Stepping over non-user code ‘System.IO.FileStream.FileStream’
Step into: Stepping over non-user code ‘System.IO.FileStream.Write’
Step into: Stepping over non-user code ‘System.IO.FileStream.CanWrite.get’
Step into: Stepping over non-user code ‘System.IO.FileStream.CanWrite.get’
Step into: Stepping over non-user code ‘System.IO.FileStream.Write’
Step into: Stepping over non-user code ‘System.IO.FileStream.Write’
Step into: Stepping over non-user code ‘System.TimeSpan.FromMilliseconds’
Step into: Stepping over non-user code ‘System.TimeSpan.TimeSpan’
Step into: Stepping over non-user code ‘System.TimeSpan.FromMilliseconds’
Step into: Stepping over non-user code ‘System.IO.FileStream.Write’
#### Exception System.InvalidOperationException - CLR_E_INVALID_OPERATION (1) ####
#### Message:
#### GHIElectronics.TinyCLR.IO.FileSystem+NativeFileStream::Write [IP: 0000] ####
#### System.IO.FileStream::Write [IP: 0035] ####
#### ReglerNextGen.Controller.SDstorage::WriteFile [IP: 002a] ####
#### ReglerNextGen.Controller.Program::Main [IP: 05f6] ####
Exception thrown: ‘System.InvalidOperationException’ in GHIElectronics.TinyCLR.IO.dll
Step into: Stepping over non-user code ‘System.IO.FileStream.Write’
Step into: Stepping over non-user code ‘System.IO.FileStream.Write’
Step into: Stepping over non-user code ‘int.ToString’
Step into: Stepping over non-user code ‘System.Globalization.NumberFormatInfo.CurrentInfo.get’
Step into: Stepping over non-user code ‘System.Globalization.CultureInfo.CurrentUICulture.get’
Step into: Stepping over non-user code ‘System.Globalization.CultureInfo.CurrentUICulture.get’
Step into: Stepping over non-user code ‘System.Globalization.NumberFormatInfo.CurrentInfo.get’
Step into: Stepping over non-user code ‘System.Globalization.CultureInfo.NumberFormat.get’
Step into: Stepping over non-user code ‘System.Globalization.NumberFormatInfo.CurrentInfo.get’

First of all, don’t use CAN for sure it is only happened when CAN msg come.

When you sure only happened when CAN message come, try to make CAN receiving event simple. You may try just add a variable like missMsgCnt, and increase +1 for each message in the event. No Debug.WriteLine……, no for loop.

Create another thread, pull those CAN messages by missMsgCnt. Default FiFo can hold 128 CAN messages, but you can change this variable to higher since G120 has large memory.

I don’t see why writing to SD failed because CAN message even, but maybe the SD driver check timeout, so just try somehow, make CAN event simple as much as you can.

Or, even no need CAN receving message event.

Hello Tran,
Thank you very much for your reply.
I will follow up on your suggestions and hope this will solve the problem.
Although 128 seems enough to me, how can I change the FIFO variable for CAN messages? I cannot find it in the tutorial or in the CanController definition.

is it TinyCLR 1.0?

canController.ReadBufferSize = 128 * 2 * 2;