Skip to content

Commit

Permalink
some new improvements:
Browse files Browse the repository at this point in the history
- corrected and connected layout to the port controller 
- opening port working
- corrected .gitignore
  • Loading branch information
opetany93 committed Nov 28, 2017
1 parent 2898d1f commit 37dd7de
Show file tree
Hide file tree
Showing 12 changed files with 215 additions and 139 deletions.
49 changes: 44 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,49 @@
# Compiled class file
*.class
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# Output files
# User-specific stuff:
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/dictionaries

# Sensitive or high-churn files:
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.xml
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml

# Gradle:
.idea/**/gradle.xml
.idea/**/libraries

# CMake
cmake-build-debug/

# Mongo Explorer plugin:
.idea/**/mongoSettings.xml

## File-based project format:
*.iws

## Plugin-specific files:

# IntelliJ
out/

\.idea/misc\.xml
# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

\.idea/workspace\.xml
# Cursive Clojure plugin
.idea/replstate.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
3 changes: 3 additions & 0 deletions .idea/dictionaries/opetany.xml

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

10 changes: 10 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

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

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

6 changes: 6 additions & 0 deletions .idea/misc.xml

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

2 changes: 1 addition & 1 deletion RealTimeSerialPlotter.iml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="jssc-2.8.0" level="project" />
<orderEntry type="library" name="jSerialComm-1.3.11" level="project" />
</component>
</module>
Binary file added Resources/jSerialComm-1.3.11.jar
Binary file not shown.
Binary file removed Resources/jssc-2.8.0.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion src/application/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class Main extends Application {
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("/mainwindow/mainWindowLayout.fxml"));
primaryStage.setTitle("Real Time Serial Plotter");
primaryStage.setScene(new Scene(root, 600, 400));
primaryStage.setScene(new Scene(root, 640, 480));
primaryStage.setMinHeight(450.0);
primaryStage.setMinWidth(620.0);

Expand Down
191 changes: 98 additions & 93 deletions src/mainwindow/COMPort.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
package mainwindow;

import javafx.scene.control.Toggle;
import jssc.SerialPort;
import jssc.SerialPortException;
import jssc.SerialPortTimeoutException;
import com.fazecast.jSerialComm.SerialPort;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;

public class COMPort
import java.net.URL;
import java.util.ResourceBundle;

public class COMPort implements Initializable
{
@FXML
Button connectButton;

@FXML
ComboBox<String> selectPortComboBox, selectBaudrateComboBox;

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;

Expand All @@ -38,82 +45,71 @@ public static COMPort getInstance()
return PortINSTANCE;
}

public void setBaudRate(int baudRate)
@Override
public void initialize(URL location, ResourceBundle resources)
{
if( null != serialPort)
for (SerialPort port : SerialPort.getCommPorts())
{
this.baudRate = baudRate;

updateParams();
selectPortComboBox.getItems().add(port.getDescriptivePortName());
}
}

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

updateParams();
selectPortComboBox.getSelectionModel().select(0);
}
}

public void setNumDataBits(Toggle newValue)
{
if( null != serialPort )
{
this.dataBits = (int)newValue.getUserData();
// ============= Baud Rate ComboBox initial values ===========================
selectBaudrateComboBox.getItems().add("9600");
selectBaudrateComboBox.getItems().add("14400");
selectBaudrateComboBox.getItems().add("19200");
selectBaudrateComboBox.getItems().add("38400");
selectBaudrateComboBox.getItems().add("56000");
selectBaudrateComboBox.getItems().add("57600");
selectBaudrateComboBox.getItems().add("115200");
selectBaudrateComboBox.getItems().add("128000");
selectBaudrateComboBox.getItems().add("230400");
selectBaudrateComboBox.getItems().add("256000");
selectBaudrateComboBox.getItems().add("460800");
selectBaudrateComboBox.getItems().add("921600");
selectBaudrateComboBox.getItems().add("1000000");
selectBaudrateComboBox.getItems().add("2000000");
selectBaudrateComboBox.getItems().add("3000000");
selectBaudrateComboBox.getSelectionModel().select(String.valueOf(getBaudRate()));
// ===========================================================================

updateParams();
}
}

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

updateParams();
}
if( null != serialPort)
serialPort.setBaudRate(baudRate);
}


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();
}
serialPort = SerialPort.getCommPort(serialPortName);
serialPort.setComPortParameters(baudRate, dataBits, stopBits, parityBits);

// 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;
serialPort.setComPortTimeouts(SerialPort.TIMEOUT_READ_SEMI_BLOCKING, 100, 100);

while(!stopReading)
{
try
{
oneChar = serialPort.readBytes(1, 1);
//dataTabInterface.displayByte(oneChar[0]);
}
catch (SerialPortException | SerialPortTimeoutException e)
{
e.printStackTrace();
}
}
}).start();
// 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 = new byte[1];
//
// while(!stopReading)
// {
// if ( 0 < serialPort.readBytes(oneChar, 1) )
// {
// //dataTabInterface.displayByte(oneChar[0]);
// }
// }
// }).start();

try
{
Expand All @@ -127,44 +123,30 @@ public boolean open(String serialPortName, int dataBits, int stopBits, int parit

public boolean close()
{
boolean isOpened = false;
boolean isClosed = false;

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

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

serialPort = null;
}
}

return isOpened;
return isClosed;
}

public void send(byte[] buffer)
{
if(null != serialPort)
{
if ( serialPort.isOpened() )
if ( serialPort.isOpen() )
{
try
{
serialPort.writeBytes(buffer);
}
catch (SerialPortException e)
{
e.printStackTrace();
}
serialPort.writeBytes(buffer, buffer.length);
}
}
}
Expand All @@ -173,27 +155,50 @@ public String getSystemPortName()
{
if ( null != serialPort )
{
return serialPort.isOpened() ? serialPort.getPortName() : "";
return serialPort.isOpen() ? serialPort.getSystemPortName() : "";
}
else
{
return "";
}
}

private boolean updateParams()
public void onActionSelectPort()
{

}

public void onActionSelectBaudrate()
{
try

}

public void onActionConnectButton()
{
if ( connectButton.getText().equals("Connect"))
{
serialPort.setParams(baudRate, dataBits, stopBits, parityBits);
if ( null != selectPortComboBox.getSelectionModel().getSelectedItem() )
{
String systemPortName = selectPortComboBox.getSelectionModel().getSelectedItem();
systemPortName = systemPortName.substring(systemPortName.indexOf("(")+1, systemPortName.indexOf(")"));

if (open(systemPortName, 8, SerialPort.ONE_STOP_BIT, SerialPort.NO_PARITY))
{
connectButton.setText("Disconnect");
//Logger.getInstance().log("Port is opened.");
//StatusBar.getInstance().setOpenedPortStatus(true);
}
}
}
catch (SerialPortException e)
else
{
e.printStackTrace();

return false;
if (close())
{
connectButton.setText("Connect");
//Logger.getInstance().log("Port is opened.");
//StatusBar.getInstance().setOpenedPortStatus(true);
}
}

return true;
}
}
Loading

0 comments on commit 37dd7de

Please sign in to comment.