Skip to content

Commit

Permalink
improvements:
Browse files Browse the repository at this point in the history
- added frameBuffer as LinkedBlockingQueue
- added method which maps array of received bytes to Frame object
- readFrame now returns Frame object 
- a little upgraded decodePayload method
- and few less significant upgrades
  • Loading branch information
opetany93 committed Dec 13, 2017
1 parent 5e57167 commit 615c299
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 51 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

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

12 changes: 9 additions & 3 deletions src/application/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,28 @@
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import port.PortReader;

public class Main extends Application {
public class Main extends Application
{

@Override
public void start(Stage primaryStage) throws Exception{
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, 640, 480));
primaryStage.setMinHeight(450.0);
primaryStage.setMinWidth(620.0);

primaryStage.setOnCloseRequest(event -> PortReader.getInstance().setStopReading(true));

primaryStage.show();
}


public static void main(String[] args) {
public static void main(String[] args)
{
launch(args);
}
}
5 changes: 5 additions & 0 deletions src/port/Port.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ public class Port

volatile boolean stopReading = false;

public void setStopReading(boolean stopReading)
{
this.stopReading = stopReading;
}

Port()
{

Expand Down
117 changes: 70 additions & 47 deletions src/port/PortReader.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package port;

import mainwindow.Log;
import classes.Frame;

import java.util.ArrayList;
import java.util.concurrent.LinkedBlockingQueue;

public class PortReader extends Port
{
Expand All @@ -11,17 +12,19 @@ public class PortReader extends Port
private static final byte STOP_BYTE = (byte) 0xD2;
private static final byte CHG_BYTE = (byte) 0xA5;

private static final byte MAX_FRAME_SIZE = 100;
/* 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 */
private static final byte MAX_FRAME_SIZE = 33;

private static volatile PortReader PortReaderINSTANCE;

private Thread readingThread = null;

private ArrayList<Byte> bufferIn;
private LinkedBlockingQueue<Frame> frameBuffer;

private PortReader()
{
bufferIn = new ArrayList<>();
frameBuffer = new LinkedBlockingQueue<>();
}

public static PortReader getInstance()
Expand All @@ -39,16 +42,20 @@ public static PortReader getInstance()
return PortReaderINSTANCE;
}

private int readFrame()
{ // variable for read one byte
private Frame readFrame()
{
short cntBytes = 0;
int readedBytes = 0;
byte[] element = new byte[1];

boolean listening = true;
boolean wasStarted = false;

bufferIn.clear();
Frame frame = null;

if(!bufferIn.isEmpty())
{
bufferIn.clear();
}

while (listening)
{
Expand All @@ -57,31 +64,32 @@ private int readFrame()
break;
}

if ((STOP_BYTE & 0xFF) == (element[0] & 0xFF)) // if element == STOP byte then decode received frame
if ((STOP_BYTE & 0xFF) == (element[0] & 0xFF)) // if element == STOP byte then decode received frame
{
if (!bufferIn.isEmpty())
{
// ========================== for debug =====================================
StringBuilder receivedData = new StringBuilder("Received data: ");
bufferIn = decodePayload(bufferIn);

// TODO: validate data

for (Byte el : bufferIn)
try
{
Short t = (short)(el & 0xFF);
frame = mapToFrame(bufferIn);

receivedData.append(t.toString()).append(" ");
listening = false;
}
catch (Exception e)
{
e.printStackTrace();
wasStarted = false;
bufferIn.clear();
cntBytes = 0;
}

Log.getInstance().log(receivedData.toString());
// ============================================================================

readedBytes = decodePayload();

// store in linkedBlockedQueue

listening = false;
}

wasStarted = false;
else
{
wasStarted = false;
}
}
else if ((START_BYTE & 0xFF) == (element[0] & 0xFF)) //if element == START byte then start listening again
{
Expand All @@ -99,16 +107,15 @@ else if(wasStarted) //else save element i
{
bufferIn.clear();
cntBytes = 0;

listening = false;
wasStarted = false;
}
}
}

return readedBytes;
return frame;
}

private int decodePayload()
private ArrayList<Byte> decodePayload(ArrayList<Byte> bufferIn)
{
ArrayList<Byte> data = new ArrayList<>();

Expand All @@ -125,40 +132,50 @@ private int decodePayload()
}
}

bufferIn.clear();
bufferIn.addAll(data);
return data;
}

// ========================== for debug ================================
StringBuilder decodedData = new StringBuilder("Decoded data: ");
private Frame mapToFrame(ArrayList<Byte> bytes) throws Exception
{
ArrayList<Integer> mappedValues = new ArrayList<>();

for (Byte el : bufferIn)
{
Short t = (short)(el & 0xFF);
byte numberOfChannels = bytes.get(0);

decodedData.append(t.toString()).append(" ");
if ( (bytes.size() - 1) < numberOfChannels * 4 )
{
throw new Exception("Number of channels in frame was wrong.");
}

Log.getInstance().log(decodedData.toString());
// ======================================================================
for (int i = 0, k = 0; i < numberOfChannels; i++)
{
mappedValues.add( ((bytes.get(++k) & 0xFF) << 24) | ((bytes.get(++k) & 0xFF) << 16) | ((bytes.get(++k) & 0xFF) << 8) | (bytes.get(++k) & 0xFF));
}

return bufferIn.size();
return new Frame(mappedValues);
}

public void startReading()
{
// create a new thread and listen for frame, then display it in Log tab

readingThread = new Thread(() ->
// create a new thread and listen for frame, then add to frame buffer
Thread readingThread = new Thread(() ->
{
System.out.println("Started reading thread.");

while(!stopReading)
while (!stopReading)
{
int readedBytes;
try
{
Frame frame = readFrame();

if ( (readedBytes = readFrame()) > 0)
if ( null != frame )
{
frameBuffer.put(readFrame());
System.out.println("New frame is added.");
}
}
catch (InterruptedException e)
{
Log.getInstance().log("Overall received and decoded number of bytes: " + readedBytes);
e.printStackTrace();
}
}

Expand All @@ -167,4 +184,10 @@ public void startReading()
readingThread.setName("FrameReadingThread");
readingThread.start();
}

public LinkedBlockingQueue<Frame> getFrameBuffer()
{
return frameBuffer;
}

}

0 comments on commit 615c299

Please sign in to comment.