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,6 +128,8 @@ namespace SharedDriver
if (Channels == null || Channels.Length == 0 || PreSamples < 2 || PostSamples < 512 || Frequency < 3100 || Frequency > 100000000)
return CaptureError.BadParams;
try
{
switch (CaptureMode)
{
case 0:
@ -188,9 +191,12 @@ namespace SharedDriver
}
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)
{
try
{
if (capturing)
return CaptureError.Busy;
@ -259,6 +265,8 @@ namespace SharedDriver
}
return CaptureError.HardwareError;
}
catch { return CaptureError.UnexpectedError; }
}
public bool StopCapture()
{
@ -267,6 +275,22 @@ namespace SharedDriver
capturing = false;
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);
@ -276,6 +300,7 @@ namespace SharedDriver
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>