public async Task <ActionResult <SerialPortDefinition> > ConnectJoystick(int id, int?baudRate = null) { int baudRateN0 = baudRate ?? 250000; var port = await SerialPortList.GetPortAndRescan(id); if (port == null) { return(NotFound()); } if (port.IsConnected) { if (port.Serial.BaudRate == baudRateN0) { return(Ok(GetDefinition(port))); } await port.Serial.DisconnectAsync(); } port.IsJoystick = true; port.Serial.BaudRate = baudRateN0; port.Serial.DtrIsReset = true; port.Serial.ResetOnConnect = false; port.GCodeCommandPrefix = ""; await port.Serial.ConnectAsync(port.PortName, null, null, null); await _hubContext.Clients.All.Connected(id); return(Ok(GetDefinition(port))); }
public void RefreshSerialPort() { SerialPortSearcher.Instance.Value.Refresh(); var ports = SerialPortSearcher.Instance.Value.PortNames.ToList(); ports.Sort(String.CompareOrdinal); SerialPortList.Clear(); ; foreach (var portName in ports) { SerialPortList.Add(portName); } if (SerialPortList.Count > 0) { if (SerialPortList.IndexOf(SerialPort) <= 0) { SerialPort = SerialPortList[0]; } } else { SerialPort = ""; } }
public async Task <ActionResult <SerialPortDefinition> > Connect(int id, string commandPrefix = null, int?baudRate = null, bool?dtrIsReset = true, bool?resetOnConnect = false) { bool dtrIsResetN0 = dtrIsReset ?? true; bool resetOnConnectN0 = resetOnConnect ?? false; int baudRateN0 = baudRate ?? 250000; var port = await SerialPortList.GetPortAndRescan(id); if (port == null) { return(NotFound()); } if (port.IsConnected) { if (port.Serial.BaudRate == baudRateN0 && resetOnConnectN0 == false && !port.IsJoystick) { return(Ok(GetDefinition(port))); } await port.Serial.DisconnectAsync(); } port.IsJoystick = false; port.Serial.BaudRate = baudRateN0; port.Serial.DtrIsReset = dtrIsResetN0; port.Serial.ResetOnConnect = resetOnConnectN0; port.GCodeCommandPrefix = commandPrefix ?? ""; await port.Serial.ConnectAsync(port.PortName, null, null, null); await _hubContext.Clients.All.Connected(id); return(Ok(GetDefinition(port))); }
/// <summary> /// COM一覧のドロップダウンを開いた時にCOMポートを再検索するイベント /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void comGimPort_DropDown(object sender, EventArgs e) { comGimPort.Items.Clear(); // すべてのシリアル・ポート名を取得する String[] ports = System.IO.Ports.SerialPort.GetPortNames(); // 取得したシリアル・ポート名を出力する foreach (String s in ports) { comGimPort.Items.Add(s); } //最後のを選択する // comGimPort->SelectedIndex = comGimPort->Items->Count-1; //ターゲットデバイス名取得 SerialPortList sp = new SerialPortList(); String target_name = sp.GetComFromDevName("STMicro"); //該当を選択 foreach (String s in ports) { if (s == target_name) { comGimPort.SelectedItem = s; break; } } //該当するCOMポートの名前を表示する ComPortNameLabel.Text = sp.GetDevNameFromCom((string)comGimPort.SelectedItem); }
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { if (msg == WmDevicechange) { //Debug.Print($"WmDevicechange {wParam} {lParam}"); //Debug.Print($" " + string.Join(", ", SerialPortList.GetBasicList().Select(x => x.Name))); var newPorts = SerialPortList.GetNames(); if (newPorts.Count() != ports.Count) { var added = newPorts.Except(ports); var removed = ports.Except(newPorts); ports = newPorts.ToList(); foreach (var i in added) { Debug.Print("New port " + i); Task.Run(() => Dispatcher.Invoke(() => ShowNewPortNotification(i))); } foreach (var i in removed) { Debug.Print("Removed port " + i); Task.Run(() => Dispatcher.Invoke(() => ShowRemovedPortNotification(i))); } } } return(IntPtr.Zero); }
private static void PrintOnlyNames() { var p = SerialPortList.GetBasicList(); foreach (var x in p) { Console.WriteLine(x.Name); } }
/// <summary> /// Reads the Registry for available serial ports /// </summary> /// <returns>A collection containing a list of available serial ports</returns> public static SerialPortList GetSerialPorts() { var mSerialPorts = new SerialPortList(); if (!mSerialPorts.IsInitialized) { mSerialPorts.ScanRegistry(); } return(mSerialPorts); }
public async Task <ActionResult <SerialPortDefinition> > Get(int id) { var port = await SerialPortList.GetPortAndRescan(id); if (port == null) { return(NotFound()); } return(GetDefinition(port)); }
public async Task <ActionResult> ResumeCommand(int id) { var port = await SerialPortList.GetPortAndRescan(id); if (port == null) { return(NotFound()); } port.Serial.ResumeAfterAbort(); return(Ok()); }
public async Task <IActionResult> ClearCommandHistory(int id) { var port = await SerialPortList.GetPortAndRescan(id); if (port == null) { return(NotFound()); } port.Serial.ClearCommandHistory(); return(Ok()); }
protected override void OnSourceInitialized(EventArgs e) { base.OnSourceInitialized(e); ports = SerialPortList.GetNames().ToList(); var source = PresentationSource.FromVisual(this) as HwndSource; source.AddHook(WndProc); this.Visibility = Visibility.Hidden; }
public async Task <ActionResult> SingleStepCommand(int id) { var port = await SerialPortList.GetPortAndRescan(id); if (port == null) { return(NotFound()); } port.Serial.SendNext = true; return(Ok()); }
public async Task <ActionResult> DisableSingleStepCommand(int id) { var port = await SerialPortList.GetPortAndRescan(id); if (port == null) { return(NotFound()); } port.Serial.Pause = false; return(Ok()); }
private async Task <SerialPortWrapper> GetPort(int id) { var port = SerialPortList.GetPort(id); if (port == null) { // rescan SerialPortList.Refresh(); port = SerialPortList.GetPort(id); } return(await Task.FromResult(port)); }
public async Task <ActionResult <IEnumerable <UInt32> > > EraseEeprom(int id) { var port = await SerialPortList.GetPortAndRescan(id); if (port == null) { return(NotFound()); } var eeprom = await port.Serial.EraseEeprom(); return(Ok(eeprom)); }
public async Task <ActionResult <decimal> > GetParameter(int id, int paramNo) { var port = await SerialPortList.GetPortAndRescan(id); if (port == null) { return(NotFound()); } var paramValue = await port.Serial.GetParameterValueAsync(paramNo, port.GCodeCommandPrefix); return(Ok(paramValue)); }
public async Task <ActionResult <IEnumerable <UInt32> > > GetEeprom(int id) { var port = await SerialPortList.GetPortAndRescan(id); if (port == null) { return(NotFound()); } var eeprom = await port.Serial.GetEpromValues(GCodeSerial.DefaultEpromTimeout); return(Ok(eeprom)); }
public async Task <ActionResult <IEnumerable <IEnumerable <decimal> > > > GetPosition(int id) { var port = await SerialPortList.GetPortAndRescan(id); if (port == null) { return(NotFound()); } var position = await port.Serial.GetPosition(port.GCodeCommandPrefix); return(Ok(position)); }
public async Task <ActionResult <IEnumerable <SerialCommand> > > SendCommand(int id, [FromBody] SerialCommands commands) { var port = await SerialPortList.GetPortAndRescan(id); if (port == null || commands == null || commands.Commands == null) { return(NotFound()); } var ret = await port.Serial.SendCommandsAsync(commands.Commands, commands.TimeOut); return(Ok(ret)); }
/// <summary> /// フォームが起動した時の処理 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Form1_Load(object sender, EventArgs e) { //UI操作無効化 panel1.Enabled = false; this.tabControl1.TabPages.Remove(this.tabPage2); // すべてのシリアル・ポート名を取得する String[] ports = System.IO.Ports.SerialPort.GetPortNames(); // 取得したシリアル・ポート名を出力する foreach (String s in ports) { comGimPort.Items.Add(s); } //ターゲットデバイス名取得 SerialPortList sp = new SerialPortList(); String target_name = sp.GetComFromDevName("STMicro"); //該当を選択 foreach (String s in ports) { if (s == target_name) { comGimPort.SelectedItem = s; break; } } //選択したCOMポートの名前の表示 ComPortNameLabel.Text = sp.GetDevNameFromCom((string)comGimPort.SelectedItem); //プロットコントロールの初期化 SAMPLING_CNT = int.Parse("" + sampling_val.Value) - 1; myPlotModel.DefaultColors = new List <OxyColor> { OxyColors.Red, OxyColors.Green, OxyColors.Blue, OxyColor.FromRgb(0x20, 0x4A, 0x87) }; myPlotModel.Axes.Add(new LinearAxis { Position = AxisPosition.Bottom, Minimum = 0, Maximum = SAMPLING_CNT, MajorGridlineStyle = LineStyle.Solid, MinorGridlineStyle = LineStyle.Dot }); //x軸の設定 sampling_val myPlotModel.Axes.Add(new LinearAxis { Position = AxisPosition.Left, Minimum = -double.Parse("" + yAxe_val.Value), Maximum = double.Parse("" + yAxe_val.Value), MajorGridlineStyle = LineStyle.Solid, MinorGridlineStyle = LineStyle.Dot }); // y軸の設定 plotView1.Model = myPlotModel; }
private static void PrintBasicList() { var p = SerialPortList.GetDetailList(); foreach (var x in p) { var text = $"{x.Name,-6} {x.FullName}"; if (x.IsUsbDevice) { text += $" [USB {x.GetUsbVendorName()} {x.GetUsbDeviceName()}]"; } Console.WriteLine(text); } }
public async Task <ActionResult <SerialPortDefinition> > DisConnect(int id) { var port = await SerialPortList.GetPortAndRescan(id); if (port == null) { return(NotFound()); } await port.Serial.DisconnectAsync(); port.Serial = null; await _hubContext.Clients.All.Disconnected(id); return(Ok()); }
private void ShowNewPortNotification(string i) { var port = SerialPortList.GetDetailList().FirstOrDefault(x => x.Name == i); if (port == null) { throw new Exception("Missing details!"); } var note = $"{port.Manufacturer}"; if (port.IsUsbDevice) { note += $"\n({port.GetUsbVendorName()}, {port.GetUsbDeviceName()})"; } tb.ShowBalloonTip($"{i} detected ({port.Description})", note, Hardcodet.Wpf.TaskbarNotification.BalloonIcon.Info); }
public async Task <ActionResult> Probe(int id, string axisName, decimal probeSize, decimal probeDist, decimal probeDistUp, decimal probeFeed) { var port = await SerialPortList.GetPortAndRescan(id); if (port == null) { return(NotFound()); } if (string.IsNullOrEmpty(axisName) || probeSize == 0 || probeDist == 0 || probeFeed == 0) { return(new BadRequestResult()); } await port.Serial.SendProbeCommandAsync(axisName, probeSize, probeDist, probeDistUp, probeFeed); return(Ok()); }
public async Task <ActionResult <IEnumerable <SerialCommand> > > GetCommandHistory(int id, bool?sortDesc = null) { var port = await SerialPortList.GetPortAndRescan(id); if (port == null) { return(NotFound()); } var cmdList = port.Serial.CommandHistoryCopy; if (sortDesc ?? true) { cmdList = cmdList.OrderByDescending(h => h.SeqId).ToList(); } return(Ok(cmdList)); }
public async Task <ActionResult> SaveEeprom(int id, [FromBody] UInt32[] eepromValue) { var port = await SerialPortList.GetPortAndRescan(id); if (port == null) { return(NotFound()); } var ee = new EepromV1 { Values = eepromValue }; if (ee.IsValid) { await port.Serial.WriteEepromValues(ee); } return(Ok()); }
public async Task <ActionResult <IEnumerable <SerialCommand> > > SendWhileOkCommand(int id, [FromBody] SerialCommands commands) { var port = await SerialPortList.GetPortAndRescan(id); if (port == null || commands == null || commands.Commands == null) { return(NotFound()); } var ret = new List <SerialCommand>(); foreach (var c in commands.Commands) { var result = await port.Serial.SendCommandsAsync(new[] { c }, commands.TimeOut); ret.AddRange(result); if (result.Any() && result.LastOrDefault().ReplyType != EReplyType.ReplyOk) { break; } } return(Ok(ret)); }
public async Task <IActionResult> Render(int id, [FromBody] PreviewGCode opt) { var port = await SerialPortList.GetPortAndRescan(id); if (port == null) { return(NotFound()); } var gCodeDraw = new GCodeBitmapDraw() { SizeX = opt.SizeX, SizeY = opt.SizeY, SizeZ = opt.SizeZ, RenderSize = new Size(opt.RenderSizeX, opt.RenderSizeY), OffsetX = opt.OffsetX, OffsetY = opt.OffsetY, OffsetZ = opt.OffsetZ, Zoom = opt.Zoom, CutterSize = opt.CutterSize, LaserSize = opt.LaserSize, KeepRatio = opt.KeepRatio }; if (opt.Rotate3DVect != null && opt.Rotate3DVect.Count() == 3) { gCodeDraw.Rotate = new Rotate3D(opt.Rotate3DAngle, opt.Rotate3DVect.ToArray()); } if (!string.IsNullOrEmpty(opt.MachineColor)) { gCodeDraw.MachineColor = System.Drawing.ColorTranslator.FromHtml(opt.MachineColor); } if (!string.IsNullOrEmpty(opt.LaserOnColor)) { gCodeDraw.LaserOnColor = System.Drawing.ColorTranslator.FromHtml(opt.LaserOnColor); } if (!string.IsNullOrEmpty(opt.LaserOffColor)) { gCodeDraw.LaserOffColor = System.Drawing.ColorTranslator.FromHtml(opt.LaserOffColor); } if (!string.IsNullOrEmpty(opt.CutColor)) { gCodeDraw.CutColor = System.Drawing.ColorTranslator.FromHtml(opt.CutColor); } if (!string.IsNullOrEmpty(opt.CutDotColor)) { gCodeDraw.CutDotColor = System.Drawing.ColorTranslator.FromHtml(opt.CutDotColor); } if (!string.IsNullOrEmpty(opt.CutEllipseColor)) { gCodeDraw.CutEllipseColor = System.Drawing.ColorTranslator.FromHtml(opt.CutEllipseColor); } if (!string.IsNullOrEmpty(opt.CutArcColor)) { gCodeDraw.CutArcColor = System.Drawing.ColorTranslator.FromHtml(opt.CutArcColor); } if (!string.IsNullOrEmpty(opt.FastMoveColor)) { gCodeDraw.FastMoveColor = System.Drawing.ColorTranslator.FromHtml(opt.FastMoveColor); } if (!string.IsNullOrEmpty(opt.HelpLineColor)) { gCodeDraw.HelpLineColor = System.Drawing.ColorTranslator.FromHtml(opt.HelpLineColor); } var hisCommands = port.Serial.CommandHistoryCopy.OrderBy(x => x.SeqId).Select(c => c.CommandText); var load = new LoadGCode(); load.Load(hisCommands.ToArray()); var commands = load.Commands; var bitmap = gCodeDraw.DrawToBitmap(commands); var memoryStream = new MemoryStream(); bitmap.Save(memoryStream, ImageFormat.Png); memoryStream.Position = 0; var fileName = "preview.png"; return(File(memoryStream, this.GetContentType(fileName), fileName)); }
private static void PrintDetailList(bool detectOpen, bool verbose) { var p = SerialPortList.GetDetailList(); Console.WriteLine(); foreach (var x in p) { Console.WriteLine(x.Name + ":"); Console.WriteLine(); if (detectOpen) { Console.Write($" Is Open : "); if (x.IsAvailable()) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("NO"); Console.ResetColor(); } else { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("YES"); Console.ResetColor(); } } if (verbose) { Console.WriteLine($" Name : {x.FullName}"); } Console.WriteLine($" Description : {x.Description}"); Console.WriteLine($" Manufacturer : {x.Manufacturer}"); if (verbose) { Console.WriteLine($" Service : {x.Service}"); Console.WriteLine($" Status : {x.Status}"); Console.WriteLine($" DeviceID : {x.DeviceID}"); Console.WriteLine($" ClassGuid : {x.ClassGuid}"); } Console.WriteLine($" Is USB device : {(x.IsUsbDevice ? "YES" : "NO")}"); if (x.IsUsbDevice) { Console.WriteLine(); if (verbose) { Console.WriteLine($" USB VID : {x.GetUsbVID():X}"); Console.WriteLine($" USB PID : {x.GetUsbPID():X}"); } Console.WriteLine($" USB Vendor : {x.GetUsbVendorName()}"); Console.WriteLine($" USB Device : {x.GetUsbDeviceName()}"); } Console.WriteLine(""); } if (p.Count() == 0) { Console.WriteLine("There is not serial port."); } else if (p.Count() == 1) { Console.WriteLine("There is 1 serial port."); } else { Console.WriteLine($"There are {p.Count()} serial ports."); } }
public async Task <IEnumerable <SerialPortDefinition> > Refresh() { SerialPortList.Refresh(); return(await Get()); }