Skip to content

Commit

Permalink
Added support of checksum
Browse files Browse the repository at this point in the history
  • Loading branch information
opetany93 committed Dec 22, 2017
1 parent 6c048a8 commit c7e5efd
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 48 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.

37 changes: 6 additions & 31 deletions src/port/CRC16.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

public class CRC16
{
int[] crc16ccittTab = {
private static int[] crc16ccittTab = {
0x0000,0x1021,0x2042,0x3063,0x4084,0x50a5,0x60c6,0x70e7,
0x8108,0x9129,0xa14a,0xb16b,0xc18c,0xd1ad,0xe1ce,0xf1ef,
0x1231,0x0210,0x3273,0x2252,0x52b5,0x4294,0x72f7,0x62d6,
Expand Down Expand Up @@ -48,40 +48,15 @@ public CRC16()
// ============================================================================================================================
public int generateCRC16CCITT(ArrayList<Byte> buff)
{
int crc = 0x0000;
byte data;
int crc = 0;
int data;

for (byte b : buff)
for (Byte aBuff : buff)
{
data = (byte)(((crc >> 8) ^ b) & 0x00FF);
crc = (crc << 8) ^ crc16ccittTab[data];
data = 0xFF & ((((crc & 0xFFFF) >> 8) ^ (aBuff & 0xFF)) & 0x00FF);
crc = 0xFFFF & (((crc & 0xFFFF) << 8) ^ crc16ccittTab[data & 0xFF]);
}


return crc;
}

int calculate_crc(ArrayList<Byte> bytes) {
int i;
int crc_value = 0;
for (Byte aByte : bytes)
{
for (i = 0x80; i != 0; i >>= 1)
{
if ((crc_value & 0x8000) != 0)
{
crc_value = (crc_value << 1) ^ 0x8005;
}
else
{
crc_value = crc_value << 1;
}
if ((aByte & i) != 0)
{
crc_value ^= 0x8005;
}
}
}
return crc_value;
}
}
9 changes: 7 additions & 2 deletions src/port/Port.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,19 @@ 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_BLOCKING, 1, 0);
serialPort.setComPortTimeouts(SerialPort.TIMEOUT_READ_BLOCKING, 0, 0);

stopReading = false;

try
{
return (serialPort.openPort());
}
catch (Exception e){ return false; }
catch (Exception e)
{
e.printStackTrace();
return false;
}
}

return false;
Expand All @@ -88,6 +92,7 @@ public boolean close()
{
boolean isClosed = false;


if(null != serialPort)
{
isClosed = serialPort.closePort();
Expand Down
48 changes: 34 additions & 14 deletions src/port/PortReader.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package port;

import classes.Frame;
import mainwindow.Log;

import java.util.ArrayList;
import java.util.concurrent.LinkedBlockingQueue;
Expand All @@ -13,8 +14,8 @@ public class PortReader extends Port
private static final byte CHG_BYTE = (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 */
private static final byte MAX_FRAME_SIZE = 33;
so all maximum number of bytes will be 4*8 + one byte of size + all possible bytes CHG */
private static final byte MAX_FRAME_SIZE = 68;

private static volatile PortReader PortReaderINSTANCE;

Expand Down Expand Up @@ -70,20 +71,35 @@ private Frame readFrame()
{
bufferIn = decodePayload(bufferIn);

// TODO: validate data
int receivedChecksum = ((bufferIn.get(bufferIn.size()-2) & 0xFF) << 8) | bufferIn.get(bufferIn.size()-1) & 0xFF;

try
{
frame = mapToFrame(bufferIn);
bufferIn.remove(bufferIn.size()-1); // remove from buffer received checksum
bufferIn.remove(bufferIn.size()-1);

listening = false;
if ( new CRC16().generateCRC16CCITT(bufferIn) == receivedChecksum)
{
System.out.println("Checksum is right.");

try
{
frame = mapToFrame(bufferIn);

System.out.println("Buffer 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("Received frame is wrong.");
listening = false;
}
}
else
Expand Down Expand Up @@ -148,7 +164,7 @@ private Frame mapToFrame(ArrayList<Byte> bytes) throws Exception

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));
mappedValues.add( ((bytes.get(++k) & 0xFF) << 24) | ((bytes.get(++k) & 0xFF) << 16) | ((bytes.get(++k) & 0xFF) << 8) | (bytes.get(++k) & 0xFF) );
}

return new Frame(mappedValues);
Expand All @@ -169,9 +185,13 @@ public void startReading()

if ( null != frame )
{
frameBuffer.put(readFrame());
frameBuffer.put(frame);
System.out.println("New frame is added.");
}
else
{
System.out.println("Read frame was a null.");
}
}
catch (InterruptedException e)
{
Expand Down

0 comments on commit c7e5efd

Please sign in to comment.