Skip to content

Commit

Permalink
Merge pull request #10 from opetany93/master
Browse files Browse the repository at this point in the history
The library responsible for serial communication has been changed to PureJavaComm
  • Loading branch information
tainfante authored Dec 22, 2017
2 parents a02e5d5 + 86c0d47 commit 114fa50
Show file tree
Hide file tree
Showing 8 changed files with 1,398 additions and 71 deletions.
9 changes: 0 additions & 9 deletions .idea/libraries/jSerialComm_1_3_11.xml

This file was deleted.

1,317 changes: 1,317 additions & 0 deletions .idea/workspace.xml___jb_tmp___

Large diffs are not rendered by default.

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="jSerialComm-1.3.11" level="project" />
<orderEntry type="library" name="com.github.purejavacomm:purejavacomm:1.0.2.RELEASE" level="project" />
</component>
</module>
Binary file removed Resources/jSerialComm-1.3.11.jar
Binary file not shown.
27 changes: 15 additions & 12 deletions src/mainwindow/PortController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import plot.Plot;
import port.PortReader;
import com.fazecast.jSerialComm.SerialPort;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import purejavacomm.CommPortIdentifier;
import purejavacomm.SerialPort;

import java.net.URL;
import java.util.Enumeration;
import java.util.ResourceBundle;

public class PortController implements Initializable
Expand All @@ -24,10 +26,7 @@ public class PortController implements Initializable
@Override
public void initialize(URL location, ResourceBundle resources)
{
for (SerialPort port : SerialPort.getCommPorts())
{
selectPortComboBox.getItems().add(port.getDescriptivePortName());
}
getAndSetAvailablePortsInComboBox();

if(!selectPortComboBox.getItems().isEmpty())
{
Expand Down Expand Up @@ -76,11 +75,7 @@ public void onActionPortComboBox()
public void onMouseClickedSelectPort()
{
selectPortComboBox.getItems().clear();

for (SerialPort port : SerialPort.getCommPorts())
{
selectPortComboBox.getItems().add(port.getDescriptivePortName());
}
getAndSetAvailablePortsInComboBox();
}

public void onActionSelectBaudrate()
Expand Down Expand Up @@ -109,9 +104,8 @@ public void onActionConnectButton()
if ( null != selectPortComboBox.getSelectionModel().getSelectedItem() )
{
String systemPortName = selectPortComboBox.getSelectionModel().getSelectedItem();
systemPortName = systemPortName.substring(systemPortName.indexOf("(")+1, systemPortName.indexOf(")"));

if (PortReader.getInstance().open(systemPortName, 8, SerialPort.ONE_STOP_BIT, SerialPort.NO_PARITY))
if (PortReader.getInstance().open(systemPortName, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE))
{
connectButton.setText("Disconnect");
selectPortComboBox.setDisable(true);
Expand All @@ -134,6 +128,15 @@ public void onActionConnectButton()
Log.getInstance().log("Port is closed.");
}
}
}

private void getAndSetAvailablePortsInComboBox()
{
Enumeration<CommPortIdentifier> ports = CommPortIdentifier.getPortIdentifiers();

while(ports.hasMoreElements())
{
selectPortComboBox.getItems().add(ports.nextElement().getName());
}
}
}
2 changes: 1 addition & 1 deletion src/port/CRC16.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class CRC16
};

// ============================================================================================================================
public CRC16()
CRC16()
{

}
Expand Down
84 changes: 49 additions & 35 deletions src/port/Port.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package port;

import com.fazecast.jSerialComm.SerialPort;
import mainwindow.Log;
import purejavacomm.CommPortIdentifier;
import purejavacomm.SerialPort;
import purejavacomm.UnsupportedCommOperationException;

import java.io.IOException;

public class Port
{
private volatile SerialPort serialPort = null;
private volatile SerialPort port = null;

private int baudRate = 115200;

Expand All @@ -29,31 +34,46 @@ public void setBaudRate(int baudRate)
{
this.baudRate = baudRate;

if( null != serialPort)
serialPort.setBaudRate(baudRate);
if( null != port)
{
try
{
port.setSerialPortParams(baudRate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
}
catch (UnsupportedCommOperationException e)
{
e.printStackTrace();
}
}
}

public void send(byte[] buffer)
{
if(null != serialPort)
if(null != port)
{
if ( serialPort.isOpen() )
try
{
serialPort.writeBytes(buffer, buffer.length);
port.getOutputStream().write(buffer);
}
catch (IOException e)
{
e.printStackTrace();
}

}
}

int readByte(byte[] oneChar)
int readByte()
{
if(null != serialPort)
if(null != port)
{
if ( serialPort.isOpen() )
try
{
return serialPort.readBytes(oneChar, 1);
return port.getInputStream().read();
}
else
catch (IOException e)
{
System.out.println("The reading attempt failed due to the port being closed.");
return -1;
}
}
Expand All @@ -65,44 +85,38 @@ int readByte(byte[] oneChar)

public boolean open(String serialPortName, int dataBits, int stopBits, int parityBits)
{
if(null != serialPortName)
try
{
serialPort = SerialPort.getCommPort(serialPortName);
serialPort.setComPortParameters(baudRate, dataBits, stopBits, parityBits);

serialPort.setComPortTimeouts(SerialPort.TIMEOUT_READ_BLOCKING, 0, 0);
CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier(serialPortName);
port = (SerialPort) portId.open("RealTimeSerialPlotter", 1000);
port.setSerialPortParams(baudRate, dataBits, stopBits, parityBits);

stopReading = false;

try
{
return (serialPort.openPort());
}
catch (Exception e)
{
e.printStackTrace();
return false;
}
return true;
}
catch (Throwable thwble)
{
Log.getInstance().log("An attempt to open the port was unsuccessful.");
thwble.printStackTrace();

return false;
return false;
}
}

public boolean close()
{
boolean isClosed = false;


if(null != serialPort)
if(null != port)
{
isClosed = serialPort.closePort();
port.close();

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

serialPort = null;
}
port = null;

isClosed = true;
}

return isClosed;
Expand Down
28 changes: 15 additions & 13 deletions src/port/PortReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
public class PortReader extends Port
{
//constants
private static final byte START_BYTE = (byte) 0xB4;
private static final byte STOP_BYTE = (byte) 0xD2;
private static final byte CHG_BYTE = (byte) 0xA5;
private static final short START_BYTE = 0xB4;
private static final short STOP_BYTE = 0xD2;
private static final short CHG_BYTE = 0xA5;

/* maximum is 8 channels where every channel has 1 int value which value it is contained in 4 bytes,
so all maximum number of bytes will be 4*8 + one byte of size + all possible bytes CHG */
Expand Down Expand Up @@ -46,7 +46,7 @@ public static PortReader getInstance()
private Frame readFrame()
{
short cntBytes = 0;
byte[] element = new byte[1];
int data;

boolean listening = true;
boolean wasStarted = false;
Expand All @@ -60,31 +60,33 @@ private Frame readFrame()

while (listening)
{
if ( 0 > readByte(element) ) // read one byte
data = readByte();

if ( 0 > data ) // read one byte
{
break;
}

if ((STOP_BYTE & 0xFF) == (element[0] & 0xFF)) // if element == STOP byte then decode received frame
if (STOP_BYTE == data) // if element == STOP byte then decode received frame
{
if (!bufferIn.isEmpty())
{
bufferIn = decodePayload(bufferIn);

int receivedChecksum = ((bufferIn.get(bufferIn.size()-2) & 0xFF) << 8) | bufferIn.get(bufferIn.size()-1) & 0xFF;

bufferIn.remove(bufferIn.size()-1); // remove from buffer received checksum
bufferIn.remove(bufferIn.size()-1); // remove received checksum from buffer
bufferIn.remove(bufferIn.size()-1);

if ( new CRC16().generateCRC16CCITT(bufferIn) == receivedChecksum)
{
System.out.println("Checksum is right.");
System.out.println("The checksums agrees.");

try
{
frame = mapToFrame(bufferIn);

System.out.println("Buffer mapped to object of Frame type.");
System.out.println("Buffer is mapped to object of Frame type.");

listening = false;
}
Expand All @@ -107,7 +109,7 @@ private Frame readFrame()
wasStarted = false;
}
}
else if ((START_BYTE & 0xFF) == (element[0] & 0xFF)) //if element == START byte then start listening again
else if (START_BYTE == data) //if element == START byte then start listening again
{
wasStarted = true;
bufferIn.clear();
Expand All @@ -116,7 +118,7 @@ else if(wasStarted) //else save element i
{
if (cntBytes < MAX_FRAME_SIZE)
{
bufferIn.add(element[0]);
bufferIn.add((byte)data);
cntBytes++;
}
else
Expand All @@ -137,7 +139,7 @@ private ArrayList<Byte> decodePayload(ArrayList<Byte> bufferIn)

for (int i = 0; i < bufferIn.size(); i++)
{
if ((CHG_BYTE & 0xFF) == (bufferIn.get(i) & 0xFF))
if (CHG_BYTE == (bufferIn.get(i) & 0xFF))
{
data.add((byte)(~bufferIn.get(i + 1) & 0xFF));
i++;
Expand Down Expand Up @@ -190,7 +192,7 @@ public void startReading()
}
else
{
System.out.println("Read frame was a null.");
System.out.println("Read frame is a null.");
}
}
catch (InterruptedException e)
Expand Down

0 comments on commit 114fa50

Please sign in to comment.