Skip to content

Commit

Permalink
Merge pull request #7 from opetany93/corrections-to-reading-frame-in-…
Browse files Browse the repository at this point in the history
…separate-thread

Fixed issue with blue screen after reopen serial port
  • Loading branch information
tainfante authored Dec 7, 2017
2 parents 4ad298b + 9183f82 commit 9cb04ba
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
21 changes: 15 additions & 6 deletions src/port/Port.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

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

private int baudRate = 115200;

Expand Down Expand Up @@ -39,17 +39,26 @@ public void send(byte[] buffer)
}
}

byte readByte()
byte readByte() throws Exception
{
byte[] oneChar = new byte[1];

if(null != serialPort)
{
if ( serialPort.isOpen() )
serialPort.readBytes(oneChar, 1);
{
if ( -1 < serialPort.readBytes(oneChar, 1) )
{
return oneChar[0];
}
else
{
oneChar[0] = 1;
}
}
}

return oneChar[0];
throw new Exception("Port is closed.");
}

public boolean open(String serialPortName, int dataBits, int stopBits, int parityBits)
Expand All @@ -59,7 +68,7 @@ public boolean open(String serialPortName, int dataBits, int stopBits, int parit
serialPort = SerialPort.getCommPort(serialPortName);
serialPort.setComPortParameters(baudRate, dataBits, stopBits, parityBits);

serialPort.setComPortTimeouts(SerialPort.TIMEOUT_READ_SEMI_BLOCKING, 100, 100);
serialPort.setComPortTimeouts(SerialPort.TIMEOUT_READ_BLOCKING, 0, 0);

stopReading = false;

Expand All @@ -85,7 +94,7 @@ public boolean close()
{
stopReading = true;

serialPort = null;
//serialPort = null;
}
}

Expand Down
23 changes: 18 additions & 5 deletions src/port/PortReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static PortReader getInstance()
return PortReaderINSTANCE;
}

private int readFrame()
private int readFrame() throws Exception
{
byte element = 0; // variable for read one byte
short cntBytes = 0;
Expand All @@ -47,7 +47,7 @@ private int readFrame()

while ((START_BYTE & 0xFF) != (element & 0xFF)) // & 0xFF <- to obtain a unsigned value
{
element = readByte();
element = readByte();
}

bufferIn.clear();
Expand Down Expand Up @@ -145,15 +145,28 @@ private int decodePayload()
public void startReading()
{
// create a new thread and listen for frame, then display it in Log tab
new Thread(() ->
Thread thread = new Thread(() ->
{
System.out.println("Started reading thread.");

bufferIn = new ArrayList<>();

while(!stopReading)
{
Log.getInstance().log("Overall received and decoded number of bytes: " + readFrame());
try
{
Log.getInstance().log("Overall received and decoded number of bytes: " + readFrame());
}
catch (Exception e)
{
break;
}
}

}).start();
System.out.println("Stopped reading thread.");

});
thread.setName("Reading Thread");
thread.start();
}
}

0 comments on commit 9cb04ba

Please sign in to comment.