fbarresi / BeckhoffJsonReadWriter

Read and write json file from a Beckhoff PLC
MIT License
23 stars 4 forks source link

Write Possible but Read not #9

Closed Fabi00815 closed 3 months ago

Fabi00815 commented 3 months ago

Hello,

i am trying to get your parser run.

If im trying to write it works perfekt. The Value in Twincat is written perfekt to the json File.

But if i want to read the same json File back to the same Variable in Twincat it doenst work.

Any Ideas?

There is no Error ID or something present?

Thanks!

fbarresi commented 3 months ago

Hi!

Yes, if there were any ads errors you get them into the twincat runtime. All other errors can be debugged by activating the logging.

You can put the log.config near to the exe in the c:/twincat/functions/jsonreadwriter folder and look for what get written there when you call the method.

Did you used the attribute defined in the Readme for your structures?

Best regards, FB

Fabi00815 commented 3 months ago

Hey,

thanks for your fast response!

Here the Log Output. (First Step i tryed to Write and it works successfully, Second Step Read and it didnt work)

The Variable is defined like this:

{attribute 'json' := 'Variable'}
Variable    :   INT;

`[23.05. 08:31:19.757] DEBUG - Program+d25.MoveNext - Starting Json Read Writer [23.05. 08:31:19.815] DEBUG - Program+d25.MoveNext - Connecting to Beckhoff Port: 851 - AdsNet: '' [23.05. 08:31:20.125] DEBUG - Program+d25.MoveNext - Method: WriteFile [23.05. 08:31:20.126] DEBUG - Program+d25.MoveNext - File: C:\Temp\test.json [23.05. 08:31:20.126] DEBUG - Program+d25.MoveNext - Field: onlyTest.Variable [23.05. 08:31:20.127] DEBUG - Program+d25.MoveNext - Executing...

[23.05. 08:32:14.360] DEBUG - Program+d25.MoveNext - Starting Json Read Writer [23.05. 08:32:14.433] DEBUG - Program+d25.MoveNext - Connecting to Beckhoff Port: 851 - AdsNet: '' [23.05. 08:32:14.729] DEBUG - Program+d25.MoveNext - Method: ReadFile [23.05. 08:32:14.729] DEBUG - Program+d25.MoveNext - File: C:\Temp\test.json [23.05. 08:32:14.730] DEBUG - Program+d25.MoveNext - Field: onlyTest.Variable [23.05. 08:32:14.730] DEBUG - Program+d25.MoveNext - Executing... [23.05. 08:32:14.731] DEBUG - Program+d25.MoveNext - Writing json into onlyTest.Variable... [23.05. 08:32:15.895] ERROR - Program+d25.MoveNext - Error while calling Json Parser: System.ArgumentException: Can not convert Object to Int16. bei Newtonsoft.Json.Linq.JToken.op_Explicit(JToken value) bei Newtonsoft.Json.Linq.JToken.ToObject(Type objectType) bei TwinCAT.JsonExtension.AdsClientExtensions.<>cDisplayClass1_0`1.b0() bei System.Threading.Tasks.Task.Execute() --- Ende der Stapelüberwachung vom vorhergehenden Ort, an dem die Ausnahme ausgelöst wurde --- bei System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() bei System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) bei TwinCAT.JsonExtension.AdsClientExtensions.d13.MoveNext() --- Ende der Stapelüberwachung vom vorhergehenden Ort, an dem die Ausnahme ausgelöst wurde --- bei System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() bei System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) bei TFU003.Program.d__25.MoveNext() System.ArgumentException: Can not convert Object to Int16. bei Newtonsoft.Json.Linq.JToken.op_Explicit(JToken value) bei Newtonsoft.Json.Linq.JToken.ToObject(Type objectType) bei TwinCAT.JsonExtension.AdsClientExtensions.<>cDisplayClass1_01.<WriteAsync>b__0() bei System.Threading.Tasks.Task.Execute() --- Ende der Stapelüberwachung vom vorhergehenden Ort, an dem die Ausnahme ausgelöst wurde --- bei System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() bei System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) bei TwinCAT.JsonExtension.AdsClientExtensions.<WriteRecursive>d__13.MoveNext() --- Ende der Stapelüberwachung vom vorhergehenden Ort, an dem die Ausnahme ausgelöst wurde --- bei System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() bei System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) bei TFU003.Program.<OnExecute>d__25.MoveNext() [23.05. 08:32:15.906] ERROR - Program+<OnExecute>d__25.MoveNext - bei Newtonsoft.Json.Linq.JToken.op_Explicit(JToken value) bei Newtonsoft.Json.Linq.JToken.ToObject(Type objectType) bei TwinCAT.JsonExtension.AdsClientExtensions.<>c__DisplayClass1_01.b0() bei System.Threading.Tasks.Task.Execute() --- Ende der Stapelüberwachung vom vorhergehenden Ort, an dem die Ausnahme ausgelöst wurde --- bei System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() bei System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) bei TwinCAT.JsonExtension.AdsClientExtensions.d13.MoveNext() --- Ende der Stapelüberwachung vom vorhergehenden Ort, an dem die Ausnahme ausgelöst wurde --- bei System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() bei System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) bei TFU003.Program.d__25.MoveNext() `

fbarresi commented 3 months ago

Hi!

Ok, I got it. The error is: Error while calling Json Parser: System.ArgumentException: Can not convert Object to Int16 .

Here the reason why this happens:

As you write to file you transmit a single value => the library wrap it up to an object (in order to create a valid json file) containing one Property.

When you read back the file you get an object, that of course cannot be parsed to a sigle field in the plc.

Solution: create a DUT with some fields inside. Use the json attribute for each field inside the DUT as you already did. Then serialize to file the variable containig the whole dut => this will be a json object. When you read it back from json it shouldn't throw any further exception because the software is able to match the object correctly.

Let me know if it works.

Best regards, FB

Fabi00815 commented 3 months ago

That worked perfekt.

Thank you for the projekt and for the fast support! Nice!

fbarresi commented 3 months ago

Welcome