use Tools + Auto Format.
The Serial.write() statement goes AFTER the }.
Definitely serial.write should be after the " } " it's a typo when editing the post. and thank you for the auto format tip I like it ![]()
I think there are a problem with the unsigned and signed bytes.
Now I tried with a very simple arduino program :
char byteRead;
int led = 13 ;
void setup() {
Serial.begin(9600);
pinMode(led, OUTPUT);
}
void loop() {
if (Serial.available()) {
byteRead = Serial.read();
Serial.write(byteRead);
}
}
and a simpler java code
public static void main(String[] args) throws IOException, PortInUseException, NoSuchPortException, InterruptedException{
SerialPortHandler serial = new SerialPortHandler();
serial.connect("COM2");
Thread.sleep(3000);
OutputStream serialOut = serial.getSerialOutputStream();
InputStream serialIn=serial.getSerialInputStream();
String s = "1";
byte[] b=new byte[] {0,0,0,0} ;
serialOut.write(s.getBytes());
serialOut.flush();
Thread.sleep(1000);
serialIn.read(b);
System.out.print("Reçu ");
for (int j = 0; j < b.length; j++) {
System.out.print(b[j]+ " ");
}
System.out.println();
b[0]=0 ;
b[1]=0;
b[2]=0;
b[3]=0;
serial.disconnect();
}
}
I keep receiving the same result -32 0 0 0 0 with any caracter that I send.
All my arduino codes work fine on the serial monitor.
serialIn.read(b);
serialIn.read(b) reads data from the SerialInputStream and put them in b which is a byte array.
I tried so many codes until now and I also having the same output : -32 which is 224 in decimal.
Can anyone help me check if my data types are correct ?