Skip to content

Commit

Permalink
Working nicely
Browse files Browse the repository at this point in the history
  • Loading branch information
MarreTeint authored Oct 11, 2022
1 parent f87dc39 commit 8be8d0f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions src/main/java/com/monchromateur/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,39 @@ public class Controller {

@FXML
private LineChart result;
//hide result


@FXML
protected void plotDatas() {
protected void plotDatas() throws InterruptedException {
SerialPort comPort = SerialPort.getCommPort("COM3");
comPort.setComPortParameters(2000000, 8, 1, 0);
//comPort.setComPortTimeouts(SerialPort.TIMEOUT_READ_SEMI_BLOCKING, 0, 0);
comPort.setComPortParameters(2000000, 8, SerialPort.ONE_STOP_BIT, SerialPort.NO_PARITY);
comPort.setComPortTimeouts(SerialPort.TIMEOUT_READ_SEMI_BLOCKING, 0, 0);
comPort.openPort();
//Arduino restart when openPort
Thread.sleep(2000);

//Starting message to arduino
byte[] WriteByte = new byte[1];
WriteByte[0] = 65; //send A
if (comPort.writeBytes(WriteByte, 1) == 1) {
System.out.println("Start command sent");
} else {
System.out.println("Error sending start command");
}


//TODO Send a request to the Arduino to start the acquisition
//comPort.writeBytes("r".getBytes(), 1);
NumberAxis xAxis = new NumberAxis();
xAxis.setLabel("Years");
NumberAxis yAxis = new NumberAxis();
yAxis.setLabel("No.of schools");
XYChart.Series series = new XYChart.Series();
series.setName("Absorbance en fonction de la longueur d'onde");

//read data from arduino
int Y[] = new int[400];
for (int i = 0; i < 400; i++) {

StringBuilder data = new StringBuilder();
//TODO : read bytes one by one from com3 if 10 => new variable, if 13 => ignore, if 0 => wait until new byte
byte[] readBuffer = new byte[1];
//convert buffer to int
comPort.readBytes(readBuffer, readBuffer.length);
//System.out.println(readBuffer[0]);

//System.out.println(" ");
while (readBuffer[0] != 10 && data.length() < 3 && data.length() >= 0) {
if (readBuffer[0] == 0) {
}
Expand All @@ -51,17 +54,17 @@ else if(readBuffer[0] >= 48 && readBuffer[0] <= 57){
}
comPort.readBytes(readBuffer, readBuffer.length);
}
System.out.println(data);
if (data.length() > 0) {
Y[i] = parseInt(data.toString());
System.out.println(i+" Data received: " + Y[i]);
}
}

for(int i=400; i<800; i+=1){
series.getData().add(new XYChart.Data(i, Y[i-400]));
}

//add linechar to result
//put data in chart
result.getData().clear();
result.getData().add(series);
result.setCreateSymbols(false); //hide dots
Expand Down
Binary file modified target/classes/com/monchromateur/Controller.class
Binary file not shown.

0 comments on commit 8be8d0f

Please sign in to comment.