Skip to content

Commit

Permalink
fix comm w some arduinos; remove unnecessary ui
Browse files Browse the repository at this point in the history
  • Loading branch information
Nat Moore committed Dec 4, 2021
1 parent c02fbab commit 7b2d3e7
Show file tree
Hide file tree
Showing 17 changed files with 104 additions and 1,322 deletions.
59 changes: 41 additions & 18 deletions fw/ino/kfd-avr/SerialProtocol.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
#include "SerialProtocol.h"
#include "hal.h"
#include <Arduino.h>

#define SOM_EOM 0x61
#define SOM_EOM_PLACEHOLDER 0x62
#define ESC 0x63
#define ESC_PLACEHOLDER 0x64

#define SOM 0x61
#define SOM_PLACEHOLDER 0x62
#define EOM 0x63
#define EOM_PLACEHOLDER 0x64
#define ESC 0x70
#define ESC_PLACEHOLDER 0x71

uint16_t inDataCount = 0;
uint8_t inData[128];
bool messageStarted = false;

void spConnect(void)
{
Expand All @@ -24,13 +27,22 @@ void spDisconnect(void)

uint16_t spRxData(uint8_t* outData)
{
uint8_t inByte;
while (Serial.available() > 0) {
inData[inDataCount] = Serial.read();
inByte = Serial.read();

// reset the buffer if we have a start of message flag coming in
if (inByte == SOM)
{
inDataCount = 0;
}

inData[inDataCount] = inByte;
inDataCount++;
}

// don't process partial frames
if (inDataCount < 3 || inData[0] != SOM_EOM || inData[inDataCount - 1] != SOM_EOM)
// don't process until we receive EOM
if (inData[inDataCount - 1] != EOM)
{
return 0;
}
Expand All @@ -45,14 +57,19 @@ uint16_t spRxData(uint8_t* outData)
{
inIndex++;

if (inData[inIndex] == SOM_EOM_PLACEHOLDER)
if (inData[inIndex] == SOM_PLACEHOLDER)
{
outData[outIndex] = SOM;
}
else if (inData[inIndex] == EOM_PLACEHOLDER)
{
outData[outIndex] = SOM_EOM;
outData[outIndex] = EOM;
}
else if (inData[inIndex] == ESC_PLACEHOLDER)
{
outData[outIndex] = ESC;
}

}
else
{
Expand All @@ -64,8 +81,7 @@ uint16_t spRxData(uint8_t* outData)

// we've already processed the message and set the pointer
// reset the count (mark the buffer as free)
inDataCount = 0;

inDataCount = 0;

return outIndex;
}
Expand All @@ -79,26 +95,33 @@ uint16_t spFrameData(const uint8_t* inData,

for (i = 0; i < inLength; i++)
{
if ((inData[i] == SOM_EOM) || (inData[i] == ESC))
if ((inData[i] == SOM) || (inData[i] == EOM) || (inData[i] == ESC))
{
escCharsNeeded++;
}
}

uint16_t totalCharsNeeded = 1 + inLength + escCharsNeeded + 1;

*(outData + 0) = SOM_EOM;
*(outData + 0) = SOM;

uint16_t j;
uint16_t k = 1;

for (j = 0; j < inLength; j++)
{
if (inData[j] == SOM_EOM)
if (inData[j] == SOM)
{
*(outData + k) = ESC;
k++;
*(outData + k) = SOM_EOM_PLACEHOLDER;
*(outData + k) = SOM_PLACEHOLDER;
k++;
}
else if (inData[j] == EOM)
{
*(outData + k) = ESC;
k++;
*(outData + k) = EOM_PLACEHOLDER;
k++;
}
else if (inData[j] == ESC)
Expand All @@ -115,7 +138,7 @@ uint16_t spFrameData(const uint8_t* inData,
}
}

*(outData + (totalCharsNeeded - 1)) = SOM_EOM;
*(outData + (totalCharsNeeded - 1)) = EOM;

return totalCharsNeeded;
}
Expand All @@ -140,4 +163,4 @@ void spTxDataWait(const uint8_t* inData,
outLength = spFrameData(inData, inLength, outData);

Serial.write(outData, outLength);
}
}
4 changes: 2 additions & 2 deletions fw/ino/kfd-avr/Versions.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

/* FIRMWARE VERSION */
#define VER_FW_MAJOR 0x01
#define VER_FW_MINOR 0x03
#define VER_FW_MINOR 0x04
#define VER_FW_PATCH 0x00

/* ADAPTER PROTOCOL VERSION */
#define VER_AP_MAJOR 0x01
#define VER_AP_MAJOR 0x02
#define VER_AP_MINOR 0x00
#define VER_AP_PATCH 0x00

Expand Down
99 changes: 59 additions & 40 deletions sw/control/KFDtool.Adapter/Protocol/Serial/SerialProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ public class SerialProtocol
{
private static NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();

const byte SOM_EOM = 0x61;
const byte SOM_EOM_PLACEHOLDER = 0x62;
const byte ESC = 0x63;
const byte ESC_PLACEHOLDER = 0x64;
const byte SOM = 0x61;
const byte SOM_PLACEHOLDER = 0x62;
const byte EOM = 0x63;
const byte EOM_PLACEHOLDER = 0x64;
const byte ESC = 0x70;
const byte ESC_PLACEHOLDER = 0x71;

private static AutoResetEvent CancelRead = new AutoResetEvent(false);

Expand Down Expand Up @@ -50,7 +52,15 @@ public SerialProtocol(string portName)

public void Open()
{
Port.Open();
// don't open the port if it is open already
if (!Port.IsOpen)
{
Port.Open();
}
else
{
Logger.Debug("Not opening already open serial port");
}
}

public void Close()
Expand All @@ -75,7 +85,7 @@ public void Send(List<byte> data)
{
List<byte> frameData = new List<byte>();

frameData.Add(SOM_EOM);
frameData.Add(SOM);

foreach (byte b in data)
{
Expand All @@ -84,18 +94,23 @@ public void Send(List<byte> data)
frameData.Add(ESC);
frameData.Add(ESC_PLACEHOLDER);
}
else if (b == SOM_EOM)
else if (b == SOM)
{
frameData.Add(ESC);
frameData.Add(SOM_EOM_PLACEHOLDER);
frameData.Add(SOM_PLACEHOLDER);
}
else if (b == EOM)
{
frameData.Add(ESC);
frameData.Add(EOM_PLACEHOLDER);
}
else
{
frameData.Add(b);
}
}

frameData.Add(SOM_EOM);
frameData.Add(EOM);

byte[] outData = frameData.ToArray();

Expand Down Expand Up @@ -192,50 +207,54 @@ private void OnDataReceived(object sender, SerialDataReceivedEventArgs e)
{
Logger.Trace("new byte: 0x{0:X2}", b);

if (b == SOM_EOM)
if (b == SOM)
{
FoundStart = true;

if (FrameBuffer.Count > 0)
Logger.Trace("found start byte");
}
else if (b == EOM)
{
for (int i = 0; i < FrameBuffer.Count; i++)
{
for (int i = 0; i < FrameBuffer.Count; i++)
if (FrameBuffer[i] == ESC)
{
if (FrameBuffer[i] == ESC)
FrameBuffer.RemoveAt(i);

if (i == FrameBuffer.Count)
{
FrameBuffer.RemoveAt(i);

if (i == FrameBuffer.Count)
{
throw new Exception("escape character at end");
}

if (FrameBuffer[i] == ESC_PLACEHOLDER)
{
FrameBuffer[i] = ESC;
}
else if (FrameBuffer[i] == SOM_EOM_PLACEHOLDER)
{
FrameBuffer[i] = SOM_EOM;
}
else
{
throw new Exception("invalid character after escape character");
}
throw new Exception("escape character at end");
}

if (FrameBuffer[i] == ESC_PLACEHOLDER)
{
FrameBuffer[i] = ESC;
}
else if (FrameBuffer[i] == SOM_PLACEHOLDER)
{
FrameBuffer[i] = SOM;
}
else if (FrameBuffer[i] == EOM_PLACEHOLDER)
{
FrameBuffer[i] = EOM;
}
else
{
throw new Exception("invalid character after escape character");
}
}
}

List<byte> packet = new List<byte>();
List<byte> packet = new List<byte>();

packet.AddRange(FrameBuffer);
packet.AddRange(FrameBuffer);

PacketBuffer.Add(packet);
PacketBuffer.Add(packet);

Logger.Debug("packet contents: {0}", BitConverter.ToString(packet.ToArray()));
Logger.Debug("packet contents: {0}", BitConverter.ToString(packet.ToArray()));

FrameBuffer.Clear();
FrameBuffer.Clear();

Logger.Debug("packet buffer length: {0}", PacketBuffer.Count);
}
Logger.Debug("packet buffer length: {0}", PacketBuffer.Count);
}
else
{
Expand Down
16 changes: 0 additions & 16 deletions sw/control/KFDtool.Gui/Control/UtilInitAdapter.xaml

This file was deleted.

Loading

0 comments on commit 7b2d3e7

Please sign in to comment.