0

I need to send a struct(consisting of booleans, ints and strings) over a TCP socket. The sender(a PLC) is implemented in CoDeSys and the receiver is implemented in Java.
One solution would be to create a big string with the data, send it over the socket and interpet it in Java. But this seems to be very unefficient(since the struct has 50+ variables).

Are there any other ways to send the struct(e.g.: in Java there is Serialization, but it's not supported in CoDeSys) ?

If not, what would be a good way to format the data? (e.g.: "variableName1:value1;variableName2:value2;...")

Are there any "tricks" to automatically assign the values from the string to the object on the Java side?

M0zart
  • 3
  • 2
  • 1
    Why does that seem inefficient exactly? That is exactly what you should do. TCP is a byte-stream protocol, so you should define the stream of bytes you want to exchange and write code to send and receive that stream of bytes. – David Schwartz Mar 10 '15 at 10:56
  • Well, converting a struct to a string and convert it back seems to be a big effort to implement, when there are so many variables. For example, Serialization in Java is way easier to implement. – M0zart Mar 10 '15 at 11:02
  • How about using a XML or JSON to put your data, may be more structured? – Parasu Mar 10 '15 at 11:03
  • 1
    @M0zart What do you think "serialization" means if not turning a structure to and from a string of bits? – David Schwartz Mar 10 '15 at 11:14
  • @Parasu Thanks, thats a good idea. I will probably go with this: http://www.journaldev.com/1234/jaxb-tutorial-example-to-convert-object-to-xml-and-xml-to-object on the Java and will have to implement the XML string by myself on CoDeSys side. – M0zart Mar 10 '15 at 11:29
  • @DavidSchwartz I was refering to something like this: http://www.tutorialspoint.com/java/java_serialization.htm but afaik it is only possible when both sides run Java – M0zart Mar 10 '15 at 11:29
  • Don't use structs as network protocols. Use network protocols as network protocols. – user207421 Mar 10 '15 at 11:47
  • @M0zart That creates a big string with the data which you would have to send over the socket to then interpret in Java -- exactly what you said seemed inefficient. – David Schwartz Mar 10 '15 at 18:42
  • @DavidSchwartz Sorry if I didn't express myself clearly. By "inefficient" I meant in terms of implementation (not data transfer). Java Serialization allows a fast implementation on both sides(Serialize/Deserialize). – M0zart Mar 10 '15 at 21:13

1 Answers1

0

I use XML personally. Every decent language on earth can parse it, and its not ridiculous to create in Codesys.

You are also forgetting that serialized data may contain platform specific oddities such as the Java double vs a Codesys REAL. These may not always transfer peacefully in a serialized fashion without consideration. Also byte-swapping issues depending on platform.

There are Base64 encode/decode libraries for Codesys, although that would not be much more efficient than XML I think.

Also, if you really worry about efficiency, in regards to XML... who said it had to be well formed. Cheat a bit as long as you know your language can glue it together properly.

user2097818
  • 1,821
  • 3
  • 16
  • 34