public static async Task <IEnumerable <IEnumerable <decimal> > > GetPosition(this ISerial serial, string commandPrefix) { string message = await serial.SendCommandAndReadOKReplyAsync($"{commandPrefix}?", 10 * 1000); var ret = new List <IEnumerable <decimal> >(); if (!string.IsNullOrEmpty(message)) { if (message.Contains("MPos:")) { // new or grbl format message = message.Replace("ok", "").Replace("<", "").Replace(">", "").Trim(); string[] tags = message.Split('|'); var mPos = TryConvert(tags, "MPos:"); if (mPos != null) { ret.Add(mPos.ToArray()); var wco = TryConvert(tags, "WCO:"); if (wco != null) { for (int i = 0; i < wco.Length; i++) { mPos[i] -= wco[i]; } } ret.Add(mPos); } } else { decimal[] mPos = Convert(message, "dummy"); ret.Add(mPos); message = await serial.SendCommandAndReadOKReplyAsync($"{commandPrefix}m114 s1", 10 * 1000); if (!string.IsNullOrEmpty(message)) { decimal[] rPos = Convert(message, "dummy"); ret.Add(rPos); } } } return(ret); }
public static async Task <decimal?> GetParameterValueAsync(this ISerial serial, int parameter, string commandPrefix) { string message = await serial.SendCommandAndReadOKReplyAsync($"{commandPrefix}(print, #{parameter})", 10 * 1000); if (!string.IsNullOrEmpty(message)) { // expected response : 0\nok string pos = message.Split('\n').FirstOrDefault(); if (decimal.TryParse(pos, NumberStyles.Any, CultureInfo.InvariantCulture, out decimal val)) { return(val); } } return(null); }