终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

78 regels
3.0 KiB

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