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> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<ApplicationIcon>window.ico</ApplicationIcon> <ApplicationIcon>window.ico</ApplicationIcon>
<Version>3.5.0.1</Version>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

@ -186,6 +186,9 @@ async Task<int> Capture(CLCaptureOptions opts)
case CaptureError.HardwareError: case CaptureError.HardwareError:
Console.WriteLine("Device reported error starting capture. Restart the device and try again."); Console.WriteLine("Device reported error starting capture. Restart the device and try again.");
return -1; 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: case CaptureError.HardwareError:
Console.WriteLine("Device reported error starting capture. Restart the device and try again."); Console.WriteLine("Device reported error starting capture. Restart the device and try again.");
return -1; 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> <TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<Version>3.5.0.1</Version>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

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

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup> <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> </PropertyGroup>
</Project> </Project>

View File

@ -561,6 +561,9 @@ namespace LogicAnalyzer
case CaptureError.HardwareError: case CaptureError.HardwareError:
await ShowError("Error", "Device reported error starting capture. Restart the device and try again."); await ShowError("Error", "Device reported error starting capture. Restart the device and try again.");
return; 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> <TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<Version>3.5.0.1</Version>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

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

View File

@ -1,4 +1,5 @@
using System.IO.Ports; using System.IO.Ports;
using System.Net.Http;
using System.Net.Sockets; using System.Net.Sockets;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
@ -15,6 +16,8 @@ namespace SharedDriver
Stream baseStream; Stream baseStream;
SerialPort sp; SerialPort sp;
TcpClient tcpClient; TcpClient tcpClient;
string devAddr;
ushort devPort;
public string? DeviceVersion { get; set; } public string? DeviceVersion { get; set; }
public event EventHandler<CaptureEventArgs>? CaptureCompleted; public event EventHandler<CaptureEventArgs>? CaptureCompleted;
@ -59,17 +62,15 @@ namespace SharedDriver
if (match == null || !match.Success) if (match == null || !match.Success)
throw new ArgumentException("Specified address/port is invalid"); 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; string port = match.Groups[2].Value;
ushort uport; if(!ushort.TryParse(port, out devPort))
if(!ushort.TryParse(port, out uport))
throw new ArgumentException("Specified address/port is invalid"); throw new ArgumentException("Specified address/port is invalid");
tcpClient = new TcpClient(); tcpClient = new TcpClient();
tcpClient.Connect(addr, uport); tcpClient.Connect(devAddr, devPort);
baseStream = tcpClient.GetStream(); baseStream = tcpClient.GetStream();
readResponse = new StreamReader(baseStream); readResponse = new StreamReader(baseStream);
@ -127,6 +128,8 @@ namespace SharedDriver
if (Channels == null || Channels.Length == 0 || PreSamples < 2 || PostSamples < 512 || Frequency < 3100 || Frequency > 100000000) if (Channels == null || Channels.Length == 0 || PreSamples < 2 || PostSamples < 512 || Frequency < 3100 || Frequency > 100000000)
return CaptureError.BadParams; return CaptureError.BadParams;
try
{
switch (CaptureMode) switch (CaptureMode)
{ {
case 0: case 0:
@ -188,9 +191,12 @@ namespace SharedDriver
} }
return CaptureError.HardwareError; 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) 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) if (capturing)
return CaptureError.Busy; return CaptureError.Busy;
@ -259,6 +265,8 @@ namespace SharedDriver
} }
return CaptureError.HardwareError; return CaptureError.HardwareError;
} }
catch { return CaptureError.UnexpectedError; }
}
public bool StopCapture() public bool StopCapture()
{ {
@ -267,6 +275,22 @@ namespace SharedDriver
capturing = false; 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.Write(new byte[] { 0xFF }, 0, 1);
sp.BaseStream.Flush(); sp.BaseStream.Flush();
Thread.Sleep(1); Thread.Sleep(1);
@ -276,6 +300,7 @@ namespace SharedDriver
baseStream = sp.BaseStream; baseStream = sp.BaseStream;
readResponse = new StreamReader(baseStream); readResponse = new StreamReader(baseStream);
readData = new BinaryReader(baseStream); readData = new BinaryReader(baseStream);
}
return true; return true;
} }
@ -333,19 +358,39 @@ namespace SharedDriver
uint length = readData.ReadUInt32(); uint length = readData.ReadUInt32();
uint[] samples = new uint[length]; 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) switch(Mode)
{ {
case 0: case 0:
for (int buc = 0; buc < length; buc++) for (int buc = 0; buc < length; buc++)
samples[buc] = readData.ReadByte(); samples[buc] = rdData.ReadByte();
break; break;
case 1: case 1:
for (int buc = 0; buc < length; buc++) for (int buc = 0; buc < length; buc++)
samples[buc] = readData.ReadUInt16(); samples[buc] = rdData.ReadUInt16();
break; break;
case 2: case 2:
for (int buc = 0; buc < length; buc++) for (int buc = 0; buc < length; buc++)
samples[buc] = readData.ReadUInt32(); samples[buc] = rdData.ReadUInt32();
break; break;
} }
@ -354,6 +399,22 @@ namespace SharedDriver
else if (CaptureCompleted != null) else if (CaptureCompleted != null)
CaptureCompleted(this, new CaptureEventArgs { Samples = samples, ChannelCount = channelCount, TriggerChannel = triggerChannel, PreSamples = preSamples }); 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; capturing = false;
} }
catch(Exception ex) catch(Exception ex)
@ -453,7 +514,8 @@ namespace SharedDriver
None, None,
Busy, Busy,
BadParams, BadParams,
HardwareError HardwareError,
UnexpectedError
} }
public class CaptureEventArgs : EventArgs public class CaptureEventArgs : EventArgs

View File

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