Skip to content

Commit

Permalink
added some java code in COMPort
Browse files Browse the repository at this point in the history
  • Loading branch information
opetany93 committed Nov 12, 2017
1 parent 2e89cd7 commit ce2ed98
Show file tree
Hide file tree
Showing 7 changed files with 295 additions and 1 deletion.
9 changes: 9 additions & 0 deletions .idea/libraries/jssc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion RealTimeSerialPlotter.iml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/Resources" isTestSource="false" />
</content>
<orderEntry type="jdk" jdkName="9" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="RXTXcomm" level="project" />
<orderEntry type="library" name="jssc" level="project" />
</component>
</module>
Binary file added out/production/RealTimeSerialPlotter/jssc.jar
Binary file not shown.
Binary file modified out/production/RealTimeSerialPlotter/mainwindow/COMPort.class
Binary file not shown.
19 changes: 19 additions & 0 deletions out/production/RealTimeSerialPlotter/mainwindow/comPortLayout.fxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<HBox layoutY="27.0" prefHeight="25.0" prefWidth="600.0" xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="mainwindow.COMPort">
<children>
<Label prefHeight="25.0" prefWidth="65.0" text="COM Port" />
<ChoiceBox prefWidth="150.0" />
<Label prefHeight="29.0" prefWidth="55.0" text="Baudrate" />
<ChoiceBox prefWidth="150.0" />
<Button mnemonicParsing="false" text="Connect" />
</children>
</HBox>
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>

<?import javafx.scene.layout.VBox?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="400.0" minWidth="600.0" prefHeight="403.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1">

<center>
<TabPane layoutY="52.0" prefHeight="314.0" prefWidth="600.0" tabClosingPolicy="UNAVAILABLE">
<tabs>
<Tab text="Chart">
<content>
<fx:include source="chart.fxml" />
</content>
</Tab>
<Tab text="Channel settings">
<content>
<fx:include source="channel.fxml" />
</content>
</Tab>
<Tab text="Export">
<content>
<fx:include source="export.fxml" />
</content>
</Tab>
<Tab text="Log">
<content>
<fx:include source="log.fxml" />
</content>
</Tab>
</tabs>
</TabPane>
</center>

<top>
<VBox prefHeight="50.0" prefWidth="600.0">
<MenuBar layoutY="2.0" prefHeight="25.0" prefWidth="600.0">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="Exit" />
<MenuItem mnemonicParsing="false" text="" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Edit">
<items>
<MenuItem mnemonicParsing="false" text="Delete" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="About" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Unspecified Menu">
<items>
<MenuItem mnemonicParsing="false" text="Action 1" />
</items>
</Menu>
</menus>
</MenuBar>
<HBox layoutY="27.0" prefHeight="25.0" prefWidth="600.0">
<fx:include source="comPortLayout.fxml" />
</HBox>
</VBox>
</top>
</BorderPane>
192 changes: 192 additions & 0 deletions src/mainwindow/COMPort.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,199 @@
package mainwindow;

import javafx.scene.control.Toggle;
import jssc.SerialPort;
import jssc.SerialPortException;
import jssc.SerialPortTimeoutException;

public class COMPort
{
private static volatile COMPort PortINSTANCE;

private SerialPort serialPort;

private int baudRate = 115200;
private int dataBits;
private int stopBits;
private int parityBits;

private boolean stopReading = false;

public int getBaudRate()
{
return baudRate;
}

public static COMPort getInstance()
{
if (null == PortINSTANCE)
{
synchronized (COMPort.class)
{
if (null == PortINSTANCE)
{
PortINSTANCE = new COMPort();
}
}
}
return PortINSTANCE;
}

public void setBaudRate(int baudRate)
{
if( null != serialPort)
{
this.baudRate = baudRate;

updateParams();
}
}

public void setParity(Toggle newValue)
{
if( null != serialPort )
{
this.parityBits = (int) newValue.getUserData();

updateParams();
}
}

public void setNumDataBits(Toggle newValue)
{
if( null != serialPort )
{
this.dataBits = (int)newValue.getUserData();

updateParams();
}
}

public void setNumStopBits(Toggle newValue)
{
if( null != serialPort )
{
this.stopBits = (int) newValue.getUserData();

updateParams();
}
}

public boolean open(String serialPortName, int dataBits, int stopBits, int parityBits)
{
this.dataBits = dataBits;
this.stopBits = stopBits;
this.parityBits = parityBits;

if(null != serialPortName)
{
serialPort = new SerialPort(serialPortName);
try
{
serialPort.setParams(baudRate, dataBits, stopBits, parityBits);
}
catch (SerialPortException e)
{
e.printStackTrace();
}

// create new thread and listen for every byte and after that send him to display on Received Text Area in DataTabController
new Thread(() ->
{
byte[] oneChar;

while(!stopReading)
{
try
{
oneChar = serialPort.readBytes(1, 1);
//dataTabInterface.displayByte(oneChar[0]);
}
catch (SerialPortException | SerialPortTimeoutException e)
{
e.printStackTrace();
}
}
}).start();

try
{
return (serialPort.openPort());
}
catch (Exception e){ return false; }
}

return false;
}

public boolean close()
{
boolean isOpened = false;

if(null != serialPort)
{
try
{
isOpened = serialPort.closePort();
}
catch (SerialPortException e)
{
e.printStackTrace();
}

if (!serialPort.isOpened())
{
stopReading = true;

serialPort = null;
}
}

return isOpened;
}

public void send(byte[] buffer)
{
if(null != serialPort)
{
if ( serialPort.isOpened() )
{
try
{
serialPort.writeBytes(buffer);
}
catch (SerialPortException e)
{
e.printStackTrace();
}
}
}
}

public String getSystemPortName()
{
if ( null != serialPort )
{
return serialPort.isOpened() ? serialPort.getPortName() : "";
}
else
{
return "";
}
}

private boolean updateParams()
{
try
{
serialPort.setParams(baudRate, dataBits, stopBits, parityBits);
}
catch (SerialPortException e)
{
e.printStackTrace();

return false;
}

return true;
}
}

0 comments on commit ce2ed98

Please sign in to comment.