Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changing target to .netstandard #5

Open
wants to merge 1 commit into
base: rickardraysearch/csharp_tcp_client
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions niryo_one_tcp_server/clients/csharp/Examples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Program
{
public static async Task Main(string[] args)
{
string server = "10.10.10.10";
string server = "niryo-desktop";

if (args.Length == 1 || args.Length == 7)
{
Expand Down Expand Up @@ -64,10 +64,10 @@ public static async Task Main(string[] args)
await niryo.MovePose(initialPose);
}

var digitalIOPins = await niryo.GetDigitalIOState();
//var digitalIOPins = await niryo.GetDigitalIOState();

foreach (var pin in digitalIOPins)
Console.WriteLine($"Pin: {pin.PinId}, name: {pin.Name}, mode: {pin.Mode}, state: {pin.State}");
//foreach (var pin in digitalIOPins)
// Console.WriteLine($"Pin: {pin.PinId}, name: {pin.Name}, mode: {pin.Mode}, state: {pin.State}");

await niryo.SetLearningMode(true);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<PropertyGroup>
<PackageId>NiryoOneClient</PackageId>
<Version>0.1.0</Version>
<Version>0.1.1</Version>
<Authors>rickardraysearch</Authors>
<Company>Niryo</Company>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
*/

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -67,9 +68,9 @@ internal async Task WriteLineAsync(string s)
internal async Task<string> ReadAsync()
{
const int blockSize = 512;
Memory<char> memory = new Memory<char>(new char[blockSize]);
var count = await _textReader.ReadAsync(memory);
return new string(memory.Span.Slice(0, count).ToArray());
var memory = new char[blockSize];
var count = await _textReader.ReadAsync(memory, 0, memory.Length);
return new string(memory.Take(count).ToArray());
}

/// <summary>
Expand Down Expand Up @@ -111,11 +112,11 @@ internal async Task<string> ReceiveAnswerAsync(string commandType, string regex
var result = match.Value.Trim();
_stringBuf = s.Substring(match.Index + match.Length).TrimStart();

var colonSplit = result.Split(':', 2);
var colonSplit = result.Split(new []{':'}, 2).ToArray();
var cmd = colonSplit[0];
if (cmd != commandType)
throw new NiryoOneException("Wrong command response received.");
var commaSplit2 = colonSplit[1].Split(',', 2);
var commaSplit2 = colonSplit[1].Split(new []{','}, 2).ToArray();
var status = commaSplit2[0];
if (status != "OK")
throw new NiryoOneException(commaSplit2[1]);
Expand Down Expand Up @@ -152,7 +153,8 @@ public async Task SetLearningMode(bool mode)
/// </summary>
public async Task MoveJoints(RobotJoints joints)
{
await SendCommandAsync("MOVE_JOINTS", string.Join(',', joints.Select(x => x.ToString(CultureInfo.InvariantCulture))));

await SendCommandAsync("MOVE_JOINTS", string.Join(",", joints.Select(x => x.ToString(CultureInfo.InvariantCulture))));
await ReceiveAnswerAsync("MOVE_JOINTS");
}

Expand All @@ -162,7 +164,7 @@ public async Task MoveJoints(RobotJoints joints)
/// </summary>
public async Task MovePose(PoseObject pose)
{
await SendCommandAsync("MOVE_POSE", string.Join(',', pose.Select(x => x.ToString(CultureInfo.InvariantCulture))));
await SendCommandAsync("MOVE_POSE", string.Join(",", pose.Select(x => x.ToString(CultureInfo.InvariantCulture))));
await ReceiveAnswerAsync("MOVE_POSE");
}

Expand Down Expand Up @@ -358,8 +360,12 @@ public async Task<DigitalPinObject[]> GetDigitalIOState()
var state = await ReceiveAnswerAsync("GET_DIGITAL_IO_STATE", @"(, *\[[^]]*\]){8}");

var regex = new Regex("\\[[0-9]+, '[^']*', [0-9]+, [0-9+]\\]");
var matches = regex.Matches(state);

var ma = regex.Matches(state);
var matches = new List<Match>();
foreach (Match match in ma)
{
matches.Add(match);
}
return matches.Select(m => ParserUtils.ParseDigitalPinObject(m.Value)).ToArray();
}
}
Expand Down
39 changes: 27 additions & 12 deletions niryo_one_tcp_server/clients/csharp/NiryoOneClient/ParserUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
*/

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
Expand Down Expand Up @@ -87,7 +88,7 @@ public static HardwareStatus ParseHardwareStatus(string data)
/// <returns>A parsed object</returns>
public static PoseObject ParsePoseObject(string s)
{
return new PoseObject(s.Split(",").Select(x => float.Parse(x, CultureInfo.InvariantCulture)).ToArray());
return new PoseObject(s.Split(',').Select(x => float.Parse(x, CultureInfo.InvariantCulture)).ToArray());
}


Expand All @@ -98,7 +99,7 @@ public static PoseObject ParsePoseObject(string s)
/// <returns>A parsed object</returns>
public static RobotJoints ParseRobotJoints(string s)
{
return new RobotJoints(s.Split(",").Select(x => float.Parse(x, CultureInfo.InvariantCulture)).ToArray());
return new RobotJoints(s.Split(',').Select(x => float.Parse(x, CultureInfo.InvariantCulture)).ToArray());
}


Expand All @@ -109,39 +110,53 @@ public static RobotJoints ParseRobotJoints(string s)
/// <returns>A parsed object</returns>
public static DigitalPinObject ParseDigitalPinObject(string s)
{
if (!s.StartsWith('[') || !s.EndsWith(']'))
if (!s.StartsWith("[") || !s.EndsWith("]"))
throw new ArgumentException();

var ss = s.Substring(1, s.Length - 2).Split(", ");
var ss = s.Substring(1, s.Length - 2).Split(',').ToArray();

return new DigitalPinObject
{
PinId = int.Parse(ss[0]),
PinId = int.Parse(ss[0].Trim()),
Name = ss[1].Trim().Substring(1, ss[1].Length - 2),
Mode = (PinMode)int.Parse(ss[2]),
State = (DigitalState)int.Parse(ss[3])
Mode = (PinMode)int.Parse(ss[2].Trim()),
State = (DigitalState)int.Parse(ss[3].Trim())
};
}

private static string Strip_(string s, char prefix, char suffix)
{
if (!s.StartsWith(prefix))
if (!s.StartsWith(prefix.ToString()))
throw new ArgumentException();
if (!s.EndsWith(suffix))
if (!s.EndsWith(suffix.ToString()))
throw new ArgumentException();
return s.Substring(1, s.Length - 2);
}

private static string[] ParseStrings_(string s)
{
var regex = new Regex(@"'[^']*'");
return regex.Matches(s).Select(m => Strip_(m.Value, '\'', '\'')).ToArray();
var ma = regex.Matches(s);
var matches = new List<Match>();
foreach (Match match in ma)
{
matches.Add(match);
}
return matches.Select(m => Strip_(m.Value, '\'', '\'')).ToArray();
}

private static T[] ParseNumbers_<T>(string s, Func<string, T> parser)


private static T[] ParseNumbers_<T>(string s, Func<string, T> parser)
{
var regex = new Regex(@"[0-9]+(\.[0-9]*)?");
return regex.Matches(s).Select(m => parser(m.Value)).ToArray();
var ma = regex.Matches(s);
var matches = new List<Match>();
foreach (Match match in ma)
{
matches.Add(match);
}
return matches.Select(m => parser(m.Value)).ToArray();
}
}
}