Skip to content

Commit

Permalink
different data receiving concept
Browse files Browse the repository at this point in the history
  • Loading branch information
tainfante committed Jan 21, 2018
1 parent efb5124 commit b2cc0f1
Show file tree
Hide file tree
Showing 7 changed files with 277 additions and 123 deletions.
213 changes: 175 additions & 38 deletions .idea/workspace.xml

Large diffs are not rendered by default.

Binary file modified out/production/KCC_App/sample/Controller.class
Binary file not shown.
Binary file modified out/production/KCC_App/sample/Port.class
Binary file not shown.
10 changes: 5 additions & 5 deletions out/production/KCC_App/sample/sample.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,30 @@
<bottom>
<HBox prefHeight="4.0" prefWidth="600.0" BorderPane.alignment="CENTER">
<children>
<Button fx:id="startBtn" mnemonicParsing="false" onAction="#onStartAction" text="START">
<Button fx:id="startBtn" mnemonicParsing="false" onAction="#onStartAction" prefHeight="27.0" prefWidth="60.0" text="START">
<HBox.margin>
<Insets bottom="10.0" left="15.0" right="10.0" top="10.0" />
</HBox.margin>
<padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding>
</Button>
<Button fx:id="stopBtn" mnemonicParsing="false" onAction="#onStopAction" text="STOP">
<Button fx:id="stopBtn" mnemonicParsing="false" onAction="#onStopAction" prefHeight="27.0" prefWidth="69.0" text="CLEAR">
<HBox.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
<Insets bottom="10.0" right="10.0" top="10.0" />
</HBox.margin>
<padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding>
</Button>
<VBox prefHeight="47.0" prefWidth="173.0">
<VBox prefHeight="47.0" prefWidth="139.0">
<children>
<Label alignment="CENTER" prefHeight="17.0" prefWidth="150.0" text="COM port:" textAlignment="CENTER">
<VBox.margin>
<Insets left="5.0" right="5.0" top="5.0" />
</VBox.margin>
</Label>
<ComboBox fx:id="com" onAction="#onSelectCom" onMouseClicked="#onListPorts" prefHeight="25.0" prefWidth="152.0">
<ComboBox fx:id="com" onAction="#onSelectCom" onMouseClicked="#onListPorts" prefHeight="25.0" prefWidth="121.0">
<VBox.margin>
<Insets bottom="5.0" left="5.0" right="5.0" />
</VBox.margin>
Expand Down
44 changes: 31 additions & 13 deletions src/sample/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public class Controller {

private String chosenCom;

private String units;

float frequency=200000000;

private static volatile Controller ControllerInstance;

public Controller(){
Expand All @@ -49,11 +53,12 @@ public void onStartAction(ActionEvent actionEvent) {
if(null!=com.getSelectionModel().getSelectedItem()){
String comPort = com.getSelectionModel().getSelectedItem();
Port.getInstance().open(comPort);
Port.getInstance().startReceiving();
startBtn.setDisable(true);
stopBtn.setDisable(false);
com.setDisable(true);
label.setText("Click the STOP button to close the port and stop receiving data");
Port.getInstance().sendStart();
long impulses=Port.getInstance().readTime();
float time=calculateTime(impulses);
log("Estimated time is: "+time+" " +units +"\n");
Port.getInstance().sendStop();
Port.getInstance().close();
}
else{
Alert alert = new Alert(Alert.AlertType.ERROR);
Expand All @@ -64,15 +69,28 @@ public void onStartAction(ActionEvent actionEvent) {
}
}

private float calculateTime(long impulses) {
float time=impulses/(16*frequency);
if (time>1000){
time=time/1000;
units="s";
}
else if(time<1){
time=time*1000;
units="us";
}
else if(time<0.001){
time=time*1000000;
units="ns";
}
else{
units="ms";
}
return time;
}

public void onStopAction(ActionEvent actionEvent) {
Port.getInstance().close();
Port.getInstance().Stop=true;
log.appendText("Port has been closed...\n");
stopBtn.setDisable(true);
startBtn.setDisable(false);
com.setDisable(false);
Port.getInstance().StopAll=true;
label.setText("Click the START button to open the port and start receiving data");
log.clear();
}

public void log(String text) {
Expand Down
123 changes: 61 additions & 62 deletions src/sample/Port.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ public class Port {

boolean Stop = false;

boolean StopAll = false;

private static volatile Port PortInstance;

private ArrayList<Byte> inputData;
Expand Down Expand Up @@ -44,7 +42,6 @@ public boolean open(String portName) {
CommPortIdentifier portNumber = CommPortIdentifier.getPortIdentifier(portName);
serialPort = (SerialPort) portNumber.open("Time to Digital Converter", 1000);
serialPort.setSerialPortParams(baudRate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
Controller.getInstance().log("Port has been opened \n");
Stop = false;

return true;
Expand Down Expand Up @@ -74,23 +71,13 @@ public boolean close() {
}
}

public void startReceiving(){
Thread receivingThread = new Thread(() ->
{
Controller.getInstance().log("Waiting to receive data \n");

while (!StopAll){
try{
long time=readTime();
Controller.getInstance().log("Estimated time is: "+time+ "\n");
}
catch (Exception e){
e.printStackTrace();
}
}
});
receivingThread.setName("TimeThread");
receivingThread.start();
public void sendStart(){
try{
serialPort.getOutputStream().write(66);
}
catch (IOException e) {
e.printStackTrace();
}
}

private byte readOneByte(){
Expand All @@ -105,57 +92,69 @@ private byte readOneByte(){
}
return oneByte;
}
private long readTime() {
Stop=false;
public long readTime() {
long time=0;
long unsignedData=0;
int numberOfBytes=0;
byte data;
while (!Stop)
{
data = readOneByte();

if ( 0 > data )
while(time==0){
Stop=false;
while (!Stop)
{
break;
}
if (data==1){
numberOfBytes++;
while(numberOfBytes!=0){
switch(numberOfBytes){
case 1:
data=readOneByte();
unsignedData=(data&0xFF);
time=time|(unsignedData<<32);
numberOfBytes++;
break;
case 2:
data=readOneByte();
unsignedData=(data&0xFF);
time=time|(unsignedData<<24);
numberOfBytes++;
break;
case 3:
data=readOneByte();
unsignedData=(data&0xFF);
time=time|(unsignedData<<16); numberOfBytes++;
break;
case 4:
data=readOneByte();
unsignedData=(data&0xFF);
time=time|(unsignedData<<8); numberOfBytes++;
break;
case 5:
data=readOneByte();
unsignedData=(data&0xFF);
time=time|(unsignedData);
numberOfBytes=0;
Stop=true;
break;
data = readOneByte();

if ( 0 > data )
{
break;
}
if (data==1){
numberOfBytes++;
while(numberOfBytes!=0){
switch(numberOfBytes){
case 1:
data=readOneByte();
unsignedData=(data&0xFF);
time=time|(unsignedData<<32);
numberOfBytes++;
break;
case 2:
data=readOneByte();
unsignedData=(data&0xFF);
time=time|(unsignedData<<24);
numberOfBytes++;
break;
case 3:
data=readOneByte();
unsignedData=(data&0xFF);
time=time|(unsignedData<<16); numberOfBytes++;
break;
case 4:
data=readOneByte();
unsignedData=(data&0xFF);
time=time|(unsignedData<<8); numberOfBytes++;
break;
case 5:
data=readOneByte();
unsignedData=(data&0xFF);
time=time|(unsignedData);
numberOfBytes=0;
Stop=true;
break;
}
}
}
}
}

return time;
}

public void sendStop() {
try{
serialPort.getOutputStream().write(0);
}
catch (IOException e) {
e.printStackTrace();
}
}
}
10 changes: 5 additions & 5 deletions src/sample/sample.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,30 @@
<bottom>
<HBox prefHeight="4.0" prefWidth="600.0" BorderPane.alignment="CENTER">
<children>
<Button fx:id="startBtn" mnemonicParsing="false" onAction="#onStartAction" text="START">
<Button fx:id="startBtn" mnemonicParsing="false" onAction="#onStartAction" prefHeight="27.0" prefWidth="60.0" text="START">
<HBox.margin>
<Insets bottom="10.0" left="15.0" right="10.0" top="10.0" />
</HBox.margin>
<padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding>
</Button>
<Button fx:id="stopBtn" mnemonicParsing="false" onAction="#onStopAction" text="STOP">
<Button fx:id="stopBtn" mnemonicParsing="false" onAction="#onStopAction" prefHeight="27.0" prefWidth="69.0" text="CLEAR">
<HBox.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
<Insets bottom="10.0" right="10.0" top="10.0" />
</HBox.margin>
<padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding>
</Button>
<VBox prefHeight="47.0" prefWidth="173.0">
<VBox prefHeight="47.0" prefWidth="139.0">
<children>
<Label alignment="CENTER" prefHeight="17.0" prefWidth="150.0" text="COM port:" textAlignment="CENTER">
<VBox.margin>
<Insets left="5.0" right="5.0" top="5.0" />
</VBox.margin>
</Label>
<ComboBox fx:id="com" onAction="#onSelectCom" onMouseClicked="#onListPorts" prefHeight="25.0" prefWidth="152.0">
<ComboBox fx:id="com" onAction="#onSelectCom" onMouseClicked="#onListPorts" prefHeight="25.0" prefWidth="121.0">
<VBox.margin>
<Insets bottom="5.0" left="5.0" right="5.0" />
</VBox.margin>
Expand Down

0 comments on commit b2cc0f1

Please sign in to comment.