终端一体化运控平台
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

RobotGripperHelper.cs 3.0 KiB

1 anno fa
1 anno fa
1 anno fa
1 anno fa
1 anno fa
1 anno fa
1 anno fa
1 anno fa
1 anno fa
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. using System.IO.Ports;
  8. using System.Threading;
  9. using System.Collections.Concurrent;
  10. using BPA.Helper;
  11. namespace BPASmartClient.SerialPort
  12. {
  13. public class RobotGripperHelper
  14. {
  15. private System.IO.Ports.SerialPort comPort = new System.IO.Ports.SerialPort();
  16. public bool IsOpen => comPort.IsOpen;
  17. public bool Open(string portName, int baudRate)
  18. {
  19. if (!System.IO.Ports.SerialPort.GetPortNames().Contains(portName))
  20. {
  21. MessageLog.GetInstance.Show($"{portName} 串口不存在");
  22. }
  23. if (!comPort.IsOpen)
  24. {
  25. comPort.PortName = portName;
  26. comPort.BaudRate = baudRate;
  27. comPort.DataBits = 8;
  28. comPort.Parity = Parity.None;
  29. comPort.StopBits = StopBits.One;
  30. /*comPort.ReadTimeout = 1000;
  31. comPort.WriteTimeout = 1000;*/
  32. //comPort.RtsEnable = true; //设置为 true后会读取不到数据
  33. comPort.DtrEnable = true;//获取或设置一个值,该值在串行通信过程中启用数据终端就绪 (DTR) 信号。
  34. //comPort.RtsEnable = true;//获取或设置一个值,该值指示在串行通信中是否启用请求发送 (RTS) 信号
  35. try
  36. {
  37. comPort.Open();
  38. }
  39. catch (Exception ex)
  40. {
  41. MessageLog.GetInstance.ShowEx(ex.ToString());
  42. }
  43. }
  44. /* ThreadManage.GetInstance().StartLong(new Action(() =>
  45. {
  46. Thread.Sleep(5000);
  47. }), $"开启电爪设备通讯", true);*/
  48. MessageLog.GetInstance.Show($"{portName} 串口打开成功");
  49. return comPort.IsOpen;
  50. }
  51. public bool Stop(string portName)
  52. {
  53. comPort.Close();
  54. MessageLog.GetInstance.Show($"{portName} 串口关闭成功");
  55. return comPort.IsOpen;
  56. }
  57. /// <summary>
  58. /// 舵机控制 #000P1300T1000!
  59. /// </summary>
  60. /// <param name="index">通道号 0 - 5</param>
  61. /// <param name="value">夹爪幅度 0500 - 2500</param>
  62. /// <param name="time">夹爪时间 0000-9999</param>
  63. public void Write(byte index, string value, string time = "1000")
  64. {
  65. byte[] PwmNumber = Encoding.ASCII.GetBytes(value);
  66. byte[] Times = Encoding.ASCII.GetBytes(time);
  67. //byte[] buffers = new byte[15] { 0x23, 0x30, 0x30, 0x30, 0x50, PwmNumber[0], PwmNumber[1], PwmNumber[2], PwmNumber[3], 0x54, Times[0], Times[1], Times[2], Times[3], 0x21 };
  68. byte[] buffers = new byte[15] { 0x23, 0x30, 0x30, 0x30, 0x50, 0x31, 0x30, 0x35, 0x30, 0x54, 0x31, 0x30, 0x30, 0x30, 0x21 };
  69. if (IsOpen) comPort.Write(buffers, 0, buffers.Length);
  70. }
  71. }
  72. }