Skip to content

Commit

Permalink
Merge pull request #11 from opetany93/master
Browse files Browse the repository at this point in the history
Cleaner code responsible for communication.
  • Loading branch information
tainfante authored Dec 27, 2017
2 parents 7a0a6db + e056ad7 commit ccd0a69
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 57 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.

6 changes: 3 additions & 3 deletions src/mainwindow/PortController.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class PortController implements Initializable
@Override
public void initialize(URL location, ResourceBundle resources)
{
getAndSetAvailablePortsInComboBox();
setAvailablePortsInComboBox();

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

public void onActionSelectBaudrate()
Expand Down Expand Up @@ -130,7 +130,7 @@ public void onActionConnectButton()
}
}

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

Expand Down
19 changes: 18 additions & 1 deletion src/port/CRC16.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class CRC16
}

// ============================================================================================================================
public int generateCRC16CCITT(ArrayList<Byte> buff)
private static int generateCRC16CCITT(ArrayList<Byte> buff)
{
int crc = 0;
int data;
Expand All @@ -59,4 +59,21 @@ public int generateCRC16CCITT(ArrayList<Byte> buff)

return crc;
}

public static boolean checksumIsAgree(ArrayList<Byte> buffer)
{
int receivedChecksum = getChecksumFromBuffer(buffer);

return generateCRC16CCITT(buffer) == receivedChecksum;
}

private static int getChecksumFromBuffer(ArrayList<Byte> buffer)
{
int receivedChecksum = ((buffer.get(buffer.size()-2) & 0xFF) << 8) | buffer.get(buffer.size()-1) & 0xFF;

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

return receivedChecksum;
}
}
103 changes: 51 additions & 52 deletions src/port/PortReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,6 @@ private Frame readFrame()

Frame frame = null;

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

while (listening)
{
data = readByte();
Expand All @@ -67,65 +62,70 @@ private Frame readFrame()
break;
}

if (STOP_BYTE == data) // if element == STOP byte then decode received frame
switch(data)
{
if (!bufferIn.isEmpty())
case START_BYTE: //if element == START byte then start listening again
{
bufferIn = decodePayload(bufferIn);

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

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

if ( new CRC16().generateCRC16CCITT(bufferIn) == receivedChecksum)
case STOP_BYTE: // if element == STOP byte then decode received frame
{
if (!bufferIn.isEmpty())
{
System.out.println("The checksums agrees.");
bufferIn = decodePayload(bufferIn);

try
if ( CRC16.checksumIsAgree(bufferIn) )
{
frame = mapToFrame(bufferIn);

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

listening = false;
System.out.println("The checksums agrees.");

try
{
frame = mapBufferToFrame(bufferIn);
System.out.println("Buffer is mapped to object of Frame type.");
listening = false;
}
catch (Exception e)
{
e.printStackTrace();
wasStarted = false;
bufferIn.clear();
cntBytes = 0;
}
}
catch (Exception e)
else
{
e.printStackTrace();
wasStarted = false;
bufferIn.clear();
cntBytes = 0;
Log.getInstance().log("The checksums doesn't agrees.");
listening = false;
}
}
else
{
Log.getInstance().log("Received frame is wrong.");
listening = false;
wasStarted = false;
}

break;
}
else
{
wasStarted = false;
}
}
else if (START_BYTE == data) //if element == START byte then start listening again
{
wasStarted = true;
bufferIn.clear();
}
else if(wasStarted) //else save element in buffer
{
if (cntBytes < MAX_FRAME_SIZE)
{
bufferIn.add((byte)data);
cntBytes++;
}
else

default:
{
bufferIn.clear();
cntBytes = 0;
wasStarted = false;
if(wasStarted) //else save element in buffer
{
if (cntBytes < MAX_FRAME_SIZE)
{
bufferIn.add((byte)data);
cntBytes++;
}
else
{
bufferIn.clear();
cntBytes = 0;
wasStarted = false;
}
}
}
}
}
Expand Down Expand Up @@ -153,15 +153,15 @@ private ArrayList<Byte> decodePayload(ArrayList<Byte> bufferIn)
return data;
}

private Frame mapToFrame(ArrayList<Byte> bytes) throws Exception
private Frame mapBufferToFrame(ArrayList<Byte> bytes) throws Exception
{
ArrayList<Integer> mappedValues = new ArrayList<>();

byte numberOfChannels = bytes.get(0);

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

for (int i = 0, k = 0; i < numberOfChannels; i++)
Expand All @@ -188,7 +188,7 @@ public void startReading()
if ( null != frame )
{
frameBuffer.put(frame);
System.out.println("New frame is added.");
System.out.println("New frame is added to the queue.");
}
else
{
Expand All @@ -211,5 +211,4 @@ public LinkedBlockingQueue<Frame> getFrameBuffer()
{
return frameBuffer;
}

}

0 comments on commit ccd0a69

Please sign in to comment.