Hi!
I’m new to processing, so please be patient and gentle I’ve installed the latest version (3.4) and I’m running Windows 10(x64)
I’m quite advanced in programming Arduino Microcontroller and usually I use a combination of union and struct to send and receive structured binary data through the serial port.
Example structure on Arduino side:
struct Infos1 {
int32_t P1;
float intens;
int32_t S1;
float S1Max;
};
union ToInfos {
byte ar1[sizeof(Infos1)];
//uint32_t integer;
Infos1 c;
};
ToInfos MainInfos;
So later on I can do a simple
Serial.write(MainInfos.ar1, sizeof(MainInfos));
to send data from arduino to the pc.
Now I’ve learned that in Processing there’s no “STRUCT” but I can use “CLASS” to manage structured data.
But something like “UNION” doesn’t even exist so I tried to get into Serial/Deserialization of objects.
So at first I’ve searched for an tutorial for Serial/Deserialization. I’ve found a nice example to save object data into a file which I modified for Processing syntax:
https://www.codementor.io/java/tutorial/serialization-and-deserialization-in-java
import java.io.Serializable;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
class DataValueObject implements Serializable{
private static final long serialVersionUID = 1L;
private String customer;
private String business;
transient private String contractID;
transient private String passKeys;
public String getCustomer() {
return customer;
}
public void setCustomer(String customer) {
this.customer = customer;
}
public String getBusiness() {
return business;
}
public void setBusiness(String business) {
this.business = business;
}
public String getContractID() {
return contractID;
}
public void setContractID(String contractID) {
this.contractID = contractID;
}
public String getPassKeys() {
return passKeys;
}
public void setPassKeys(String passKeys) {
this.passKeys = passKeys;
}
@Override
public String toString() {
String value = "customer : " + customer + "\nbusiness : " + business + "\ncontractID : " + contractID
+ "\npassKeys : " + passKeys;
return value;
}
}
class SerializationDemo {
/**
* This method is used to read data from file for deSerialization.
*
* @param file
* @return
* @throws IOException
* @throws ClassNotFoundException
*/
public Object deSerialization(String file) throws IOException, ClassNotFoundException {
FileInputStream fileInputStream = new FileInputStream(file);
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
ObjectInputStream objectInputStream = new ObjectInputStream(bufferedInputStream);
Object object = objectInputStream.readObject();
objectInputStream.close();
return object;
}
/**
* This method is used to write data to file for Serialization.
*
* @param file
* @param object
* @throws IOException
*/
public void serialization(String file, Object object) throws IOException {
FileOutputStream fileOutputStream = new FileOutputStream(file);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(bufferedOutputStream);
objectOutputStream.writeObject(object);
objectOutputStream.close();
}
}
DataValueObject dataValueObject = new DataValueObject();
dataValueObject.setCustomer("Debbie");
dataValueObject.setBusiness("JAVA Concepts");
dataValueObject.setContractID("ZZZZZZ");
dataValueObject.setPassKeys("!@wer#$");
SerializationDemo s1 = new SerializationDemo();
try {
s1.serialization("x:/fileToSave.txt", dataValueObject);
DataValueObject object = (DataValueObject) s1.deSerialization("fileToSave.txt");
System.out.println(object.toString());
} catch (IOException exp) {
exp.printStackTrace();
} catch (ClassNotFoundException exp) {
exp.printStackTrace();
}
But when I try to run the sketch the following exception appears:
java.io.NotSerializableException: sketch_181226d
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1509)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
at sketch_181226d$1SerializationDemo.serialization(sketch_181226d.java:105)
at sketch_181226d.setup(sketch_181226d.java:117)
at processing.core.PApplet.handleDraw(PApplet.java:2404)
at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1557)
at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:313)
What am I doing wrong? Is serial/deserialization the right/only way to send/receive binary structured data?
Thank you very much!
Kind regards,
Chris