Sfoglia il codice sorgente

Add ThunderboltSerialPort class to interface with the Thunderbolt's serial port

master
The6P4C 7 anni fa
parent
commit
628744ba65
4 ha cambiato i file con 106 aggiunte e 12 eliminazioni
  1. +1
    -1
      ThunderboltTimeSync.sln
  2. +16
    -11
      ThunderboltTimeSync/FormMain.cs
  3. +88
    -0
      ThunderboltTimeSync/ThunderboltSerialPort.cs
  4. +1
    -0
      ThunderboltTimeSync/ThunderboltTimeSync.csproj

+ 1
- 1
ThunderboltTimeSync.sln Vedi File

@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.25123.0
VisualStudioVersion = 14.0.25420.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ThunderboltTimeSync", "ThunderboltTimeSync\ThunderboltTimeSync.csproj", "{7D970120-F57C-4541-BD54-2602C59EB8BA}"
EndProject


+ 16
- 11
ThunderboltTimeSync/FormMain.cs Vedi File

@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO.Ports;
using System.Linq;
using System.Windows.Forms;

namespace ThunderboltTimeSync {
@@ -18,16 +20,19 @@ namespace ThunderboltTimeSync {

InitializeComponent();

Debug.WriteLine(SystemTimeUtils.GetSystemTime().ToString("yyyy-MM-ddTHH:mm:ssK.ffff"));
try {
SystemTimeUtils.SetSystemTime(new DateTime(
2000, 1, 1,
10, 0, 0, 0
));
} catch (SystemTimeUtils.SystemTimeException stEx) {
Debug.WriteLine(string.Format("Setting system time failed: \"{0}\"", stEx.Message));
}
Debug.WriteLine(SystemTimeUtils.GetSystemTime().ToString("yyyy-MM-ddTHH:mm:ssK.ffff"));
ThunderboltSerialPort tbsp = new ThunderboltSerialPort(new SerialPort("COM3"));

tbsp.PacketReceived += (List<byte> packetBuffer) => {
List<string> byteStrings = packetBuffer.Select(x => string.Format("{0:X2}", x)).ToList();
Debug.WriteLine(
string.Format(
"Packet received: {0}",
string.Join(" ", byteStrings)
)
);
};

tbsp.Open();
}
}
}

+ 88
- 0
ThunderboltTimeSync/ThunderboltSerialPort.cs Vedi File

@@ -0,0 +1,88 @@
using System.Collections.Generic;
using System.IO.Ports;

namespace ThunderboltTimeSync {
public class ThunderboltSerialPort {
private static readonly byte CHAR_DLE = 0x10;
private static readonly byte CHAR_ETX = 0x03;

private List<byte> packetBuffer;
private bool inPacket;

private SerialPort serialPort;

/// <summary>
/// A delegate which is called when a full packet is received over the serial port.
/// The byte buffer passed in contains the initial [DLE], and final [DLE][ETX]. The buffer is completely unparsed and remains stuffed.
/// </summary>
/// <param name="packetBuffer">A byte buffer which contains the packet.</param>
public delegate void PacketReceivedEventHandler(List<byte> packetBuffer);

/// <summary>
/// An event which is called when a full packet is received over the serial port.
/// Refer to <see cref="PacketReceivedEventHandler"/> for more information.
/// </summary>
public event PacketReceivedEventHandler PacketReceived;

/// <summary>
/// Creates an instance of the ThunderboltSerialPort class, which processes serial data from a Thunderbolt and
/// The serial port passed into the function must not be opened, or have a delegate already attached to the DataReceived event.
/// </summary>
/// <param name="serialPort">The serial port on which to communicate with the Thunderbolt.</param>
public ThunderboltSerialPort(SerialPort serialPort) {
packetBuffer = new List<byte>();
inPacket = false;

this.serialPort = serialPort;
serialPort.DataReceived += DataReceived;
}

/// <summary>
/// Begins processing serial data.
/// </summary>
public void Open() {
serialPort.Open();
}

private void DataReceived(object sender, SerialDataReceivedEventArgs e) {
int possibleCurrentByte;

while ((possibleCurrentByte = serialPort.ReadByte()) != -1) {
// Once we're sure the byte that was read wasn't -1 (which signifies the end of the read), we're safe to cast to a byte
byte currentByte = (byte) possibleCurrentByte;

if (inPacket) {
packetBuffer.Add(currentByte);

// Check buffer length to ensure we've reached a plausible end of packet.
// 5 bytes is [DLE]<id><1 byte of data>[DLE][ETX]
if (currentByte == CHAR_ETX && packetBuffer.Count >= 5) {
int numberOfPrecedingDLEs = 0;

// Count number of DLEs, excluding the first two bytes (initial DLE and id)
for (int i = 2; i < packetBuffer.Count; ++i) {
if (packetBuffer[i] == CHAR_DLE) {
++numberOfPrecedingDLEs;
}
}

// Odd number of DLEs means the ETX does in fact signify the end of the packet
if (numberOfPrecedingDLEs % 2 == 1) {
PacketReceived?.Invoke(packetBuffer);

packetBuffer.Clear();
inPacket = false;
}
}
} else {
// A DLE received when not currently in a packet signifies the beginning of a packet
if (currentByte == CHAR_DLE) {
packetBuffer.Add(currentByte);

inPacket = true;
}
}
}
}
}
}

+ 1
- 0
ThunderboltTimeSync/ThunderboltTimeSync.csproj Vedi File

@@ -55,6 +55,7 @@
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SystemTimeUtils.cs" />
<Compile Include="ThunderboltSerialPort.cs" />
<EmbeddedResource Include="FormMain.resx">
<DependentUpon>FormMain.cs</DependentUpon>
</EmbeddedResource>


Caricamento…
Annulla
Salva