Corrected driver to work properly under Linux

This commit is contained in:
Agustín Gimenez 2023-02-07 19:09:50 +01:00
parent ab32e4f324
commit bba03c69eb
10 changed files with 210 additions and 133 deletions

View File

@ -6,6 +6,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<ApplicationIcon>window.ico</ApplicationIcon>
<Version>3.5.0.1</Version>
</PropertyGroup>
<ItemGroup>

View File

@ -186,6 +186,9 @@ async Task<int> Capture(CLCaptureOptions opts)
case CaptureError.HardwareError:
Console.WriteLine("Device reported error starting capture. Restart the device and try again.");
return -1;
case CaptureError.UnexpectedError:
Console.WriteLine("Unexpected error. Restart the device and try again.");
return -1;
}
}
@ -223,6 +226,9 @@ async Task<int> Capture(CLCaptureOptions opts)
case CaptureError.HardwareError:
Console.WriteLine("Device reported error starting capture. Restart the device and try again.");
return -1;
case CaptureError.UnexpectedError:
Console.WriteLine("Unexpected error. Restart the device and try again.");
return -1;
}
}

View File

@ -4,6 +4,7 @@
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>3.5.0.1</Version>
</PropertyGroup>
<ItemGroup>

View File

@ -8,6 +8,7 @@
<BuiltInComInteropSupport>true</BuiltInComInteropSupport>
<ApplicationIcon>Assets\window.ico</ApplicationIcon>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
<Version>3.5.0.1</Version>
</PropertyGroup>
<ItemGroup>
<None Remove=".gitignore" />

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<_LastSelectedProfileId>C:\Users\geniw\source\repos\LogicAnalyzer\LogicAnalyzer\Properties\PublishProfiles\Windows-Arm64.pubxml</_LastSelectedProfileId>
<_LastSelectedProfileId>C:\Users\geniw\source\repos\LogicAnalyzer\LogicAnalyzer\Properties\PublishProfiles\Linux-Arm.pubxml</_LastSelectedProfileId>
</PropertyGroup>
</Project>

View File

@ -561,6 +561,9 @@ namespace LogicAnalyzer
case CaptureError.HardwareError:
await ShowError("Error", "Device reported error starting capture. Restart the device and try again.");
return;
case CaptureError.UnexpectedError:
await ShowError("Error", "Unexpected error, restart the application and the device and try again.");
return;
}
}

View File

@ -4,6 +4,7 @@
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>3.5.0.1</Version>
</PropertyGroup>
<ItemGroup>

View File

@ -4,6 +4,7 @@
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>3.5.0.1</Version>
</PropertyGroup>
<ItemGroup>

View File

@ -1,4 +1,5 @@
using System.IO.Ports;
using System.Net.Http;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
@ -15,6 +16,8 @@ namespace SharedDriver
Stream baseStream;
SerialPort sp;
TcpClient tcpClient;
string devAddr;
ushort devPort;
public string? DeviceVersion { get; set; }
public event EventHandler<CaptureEventArgs>? CaptureCompleted;
@ -59,17 +62,15 @@ namespace SharedDriver
if (match == null || !match.Success)
throw new ArgumentException("Specified address/port is invalid");
string addr = match.Groups[1].Value;
devAddr = match.Groups[1].Value;
string port = match.Groups[2].Value;
ushort uport;
if(!ushort.TryParse(port, out uport))
if(!ushort.TryParse(port, out devPort))
throw new ArgumentException("Specified address/port is invalid");
tcpClient = new TcpClient();
tcpClient.Connect(addr, uport);
tcpClient.Connect(devAddr, devPort);
baseStream = tcpClient.GetStream();
readResponse = new StreamReader(baseStream);
@ -127,137 +128,144 @@ namespace SharedDriver
if (Channels == null || Channels.Length == 0 || PreSamples < 2 || PostSamples < 512 || Frequency < 3100 || Frequency > 100000000)
return CaptureError.BadParams;
switch (CaptureMode)
try
{
case 0:
switch (CaptureMode)
{
case 0:
if (PreSamples > 98303 || PostSamples > 131069 || PreSamples + PostSamples > 131071)
return CaptureError.BadParams;
break;
if (PreSamples > 98303 || PostSamples > 131069 || PreSamples + PostSamples > 131071)
return CaptureError.BadParams;
break;
case 1:
case 1:
if (PreSamples > 49151 || PostSamples > 65533 || PreSamples + PostSamples > 65535)
return CaptureError.BadParams;
break;
if (PreSamples > 49151 || PostSamples > 65533 || PreSamples + PostSamples > 65535)
return CaptureError.BadParams;
break;
case 2:
case 2:
if (PreSamples > 24576 || PostSamples > 32765 || PreSamples + PostSamples > 32767)
return CaptureError.BadParams;
break;
if (PreSamples > 24576 || PostSamples > 32765 || PreSamples + PostSamples > 32767)
return CaptureError.BadParams;
break;
}
channelCount = Channels.Length;
triggerChannel = Array.IndexOf(Channels, TriggerChannel);
preSamples = PreSamples;
currentCaptureHandler = CaptureCompletedHandler;
CaptureRequest request = new CaptureRequest
{
triggerType = 0,
trigger = (byte)TriggerChannel,
invertedOrCount = TriggerInverted ? (byte)1 : (byte)0,
channels = new byte[32],
channelCount = (byte)Channels.Length,
frequency = (uint)Frequency,
preSamples = (uint)PreSamples,
postSamples = (uint)PostSamples,
captureMode = CaptureMode
};
for (int buc = 0; buc < Channels.Length; buc++)
request.channels[buc] = (byte)Channels[buc];
OutputPacket pack = new OutputPacket();
pack.AddByte(1);
pack.AddStruct(request);
baseStream.Write(pack.Serialize());
baseStream.Flush();
baseStream.ReadTimeout = 10000;
var result = readResponse.ReadLine();
baseStream.ReadTimeout = Timeout.Infinite;
if (result == "CAPTURE_STARTED")
{
capturing = true;
Task.Run(() => ReadCapture(PreSamples + PostSamples, CaptureMode));
return CaptureError.None;
}
return CaptureError.HardwareError;
}
channelCount = Channels.Length;
triggerChannel = Array.IndexOf(Channels, TriggerChannel);
preSamples = PreSamples;
currentCaptureHandler = CaptureCompletedHandler;
CaptureRequest request = new CaptureRequest
{
triggerType = 0,
trigger = (byte)TriggerChannel,
invertedOrCount = TriggerInverted ? (byte)1 : (byte)0,
channels = new byte[32],
channelCount = (byte)Channels.Length,
frequency = (uint)Frequency,
preSamples = (uint)PreSamples,
postSamples = (uint)PostSamples,
captureMode = CaptureMode
};
for (int buc = 0; buc < Channels.Length; buc++)
request.channels[buc] = (byte)Channels[buc];
OutputPacket pack = new OutputPacket();
pack.AddByte(1);
pack.AddStruct(request);
baseStream.Write(pack.Serialize());
baseStream.Flush();
baseStream.ReadTimeout = 10000;
var result = readResponse.ReadLine();
baseStream.ReadTimeout = Timeout.Infinite;
if (result == "CAPTURE_STARTED")
{
capturing = true;
Task.Run(() => ReadCapture(PreSamples + PostSamples, CaptureMode));
return CaptureError.None;
}
return CaptureError.HardwareError;
catch { return CaptureError.UnexpectedError; }
}
public CaptureError StartPatternCapture(int Frequency, int PreSamples, int PostSamples, int[] Channels, int TriggerChannel, int TriggerBitCount, UInt16 TriggerPattern, bool Fast, byte CaptureMode, Action<CaptureEventArgs>? CaptureCompletedHandler = null)
{
if (capturing)
return CaptureError.Busy;
if (Channels == null || Channels.Length == 0 || PreSamples < 2 || PostSamples < 512 || Frequency < 3100 || Frequency > 100000000)
return CaptureError.BadParams;
switch (CaptureMode)
try
{
case 0:
if (capturing)
return CaptureError.Busy;
if (PreSamples > 98303 || PostSamples > 131069 || PreSamples + PostSamples > 131071)
return CaptureError.BadParams;
break;
if (Channels == null || Channels.Length == 0 || PreSamples < 2 || PostSamples < 512 || Frequency < 3100 || Frequency > 100000000)
return CaptureError.BadParams;
case 1:
switch (CaptureMode)
{
case 0:
if (PreSamples > 49151 || PostSamples > 65533 || PreSamples + PostSamples > 65535)
return CaptureError.BadParams;
break;
if (PreSamples > 98303 || PostSamples > 131069 || PreSamples + PostSamples > 131071)
return CaptureError.BadParams;
break;
case 2:
case 1:
if (PreSamples > 24576 || PostSamples > 32765 || PreSamples + PostSamples > 32767)
return CaptureError.BadParams;
break;
if (PreSamples > 49151 || PostSamples > 65533 || PreSamples + PostSamples > 65535)
return CaptureError.BadParams;
break;
case 2:
if (PreSamples > 24576 || PostSamples > 32765 || PreSamples + PostSamples > 32767)
return CaptureError.BadParams;
break;
}
channelCount = Channels.Length;
triggerChannel = Array.IndexOf(Channels, TriggerChannel);
preSamples = PreSamples;
currentCaptureHandler = CaptureCompletedHandler;
CaptureRequest request = new CaptureRequest
{
triggerType = (byte)(Fast ? 2 : 1),
trigger = (byte)TriggerChannel,
invertedOrCount = (byte)TriggerBitCount,
triggerValue = (UInt16)TriggerPattern,
channels = new byte[32],
channelCount = (byte)Channels.Length,
frequency = (uint)Frequency,
preSamples = (uint)PreSamples,
postSamples = (uint)PostSamples,
captureMode = CaptureMode
};
for (int buc = 0; buc < Channels.Length; buc++)
request.channels[buc] = (byte)Channels[buc];
OutputPacket pack = new OutputPacket();
pack.AddByte(1);
pack.AddStruct(request);
baseStream.Write(pack.Serialize());
baseStream.Flush();
baseStream.ReadTimeout = 10000;
var result = readResponse.ReadLine();
baseStream.ReadTimeout = Timeout.Infinite;
if (result == "CAPTURE_STARTED")
{
capturing = true;
Task.Run(() => ReadCapture(PreSamples + PostSamples, CaptureMode));
return CaptureError.None;
}
return CaptureError.HardwareError;
}
channelCount = Channels.Length;
triggerChannel = Array.IndexOf(Channels, TriggerChannel);
preSamples = PreSamples;
currentCaptureHandler = CaptureCompletedHandler;
CaptureRequest request = new CaptureRequest
{
triggerType = (byte)(Fast ? 2 : 1),
trigger = (byte)TriggerChannel,
invertedOrCount = (byte)TriggerBitCount,
triggerValue = (UInt16)TriggerPattern,
channels = new byte[32],
channelCount = (byte)Channels.Length,
frequency = (uint)Frequency,
preSamples = (uint)PreSamples,
postSamples = (uint)PostSamples,
captureMode = CaptureMode
};
for (int buc = 0; buc < Channels.Length; buc++)
request.channels[buc] = (byte)Channels[buc];
OutputPacket pack = new OutputPacket();
pack.AddByte(1);
pack.AddStruct(request);
baseStream.Write(pack.Serialize());
baseStream.Flush();
baseStream.ReadTimeout = 10000;
var result = readResponse.ReadLine();
baseStream.ReadTimeout = Timeout.Infinite;
if (result == "CAPTURE_STARTED")
{
capturing = true;
Task.Run(() => ReadCapture(PreSamples + PostSamples, CaptureMode));
return CaptureError.None;
}
return CaptureError.HardwareError;
catch { return CaptureError.UnexpectedError; }
}
public bool StopCapture()
@ -267,15 +275,32 @@ namespace SharedDriver
capturing = false;
sp.Write(new byte[] { 0xFF }, 0, 1);
sp.BaseStream.Flush();
Thread.Sleep(1);
sp.Close();
Thread.Sleep(1);
sp.Open();
baseStream = sp.BaseStream;
readResponse = new StreamReader(baseStream);
readData = new BinaryReader(baseStream);
if (IsNetwork)
{
baseStream.WriteByte(0xff);
baseStream.Flush();
Thread.Sleep(1);
tcpClient.Close();
Thread.Sleep(1);
tcpClient = new TcpClient();
tcpClient.Connect(devAddr, devPort);
baseStream = tcpClient.GetStream();
readResponse = new StreamReader(baseStream);
readData = new BinaryReader(baseStream);
}
else
{
sp.Write(new byte[] { 0xFF }, 0, 1);
sp.BaseStream.Flush();
Thread.Sleep(1);
sp.Close();
Thread.Sleep(1);
sp.Open();
baseStream = sp.BaseStream;
readResponse = new StreamReader(baseStream);
readData = new BinaryReader(baseStream);
}
return true;
}
@ -333,19 +358,39 @@ namespace SharedDriver
uint length = readData.ReadUInt32();
uint[] samples = new uint[length];
BinaryReader rdData;
if (IsNetwork)
rdData = readData;
else
{
byte[] readBuffer = new byte[Samples * (Mode == 0 ? 1 : (Mode == 1 ? 2 : 4))];
int left = readBuffer.Length;
int pos = 0;
while (left > 0 && sp.IsOpen)
{
pos += sp.Read(readBuffer, pos, left);
left = readBuffer.Length - pos;
}
MemoryStream ms = new MemoryStream(readBuffer);
rdData = new BinaryReader(ms);
}
switch(Mode)
{
case 0:
for (int buc = 0; buc < length; buc++)
samples[buc] = readData.ReadByte();
samples[buc] = rdData.ReadByte();
break;
case 1:
for (int buc = 0; buc < length; buc++)
samples[buc] = readData.ReadUInt16();
samples[buc] = rdData.ReadUInt16();
break;
case 2:
for (int buc = 0; buc < length; buc++)
samples[buc] = readData.ReadUInt32();
samples[buc] = rdData.ReadUInt32();
break;
}
@ -354,6 +399,22 @@ namespace SharedDriver
else if (CaptureCompleted != null)
CaptureCompleted(this, new CaptureEventArgs { Samples = samples, ChannelCount = channelCount, TriggerChannel = triggerChannel, PreSamples = preSamples });
if (!IsNetwork)
{
try
{
rdData.BaseStream.Close();
rdData.BaseStream.Dispose();
}
catch { }
try
{
rdData.Close();
rdData.Dispose();
}
catch { }
}
capturing = false;
}
catch(Exception ex)
@ -453,7 +514,8 @@ namespace SharedDriver
None,
Busy,
BadParams,
HardwareError
HardwareError,
UnexpectedError
}
public class CaptureEventArgs : EventArgs

View File

@ -5,6 +5,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
<Version>3.5.0.1</Version>
</PropertyGroup>
<ItemGroup>