EurothermP116Connection.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using MECF.Framework.Common.Communications;
  4. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Temperatures.Eurotherms
  5. {
  6. public class P116PIDTCMessage : BinaryMessage
  7. {
  8. public byte DeviceAddress { get; set; }
  9. public byte FunctionCode { get; set; }
  10. public byte Length { get; set; }
  11. public byte[] Data { get; set; }
  12. }
  13. public class EurothermP116Connection:SerialPortConnectionBase
  14. {
  15. private List<byte> _lstCacheBuffer = new List<byte>();
  16. public EurothermP116Connection(string portName) : base(portName, 9600, 8, System.IO.Ports.Parity.None, System.IO.Ports.StopBits.One, "\n", false)
  17. {
  18. }
  19. public override bool SendMessage(byte[] message)
  20. {
  21. _lstCacheBuffer.Clear();
  22. return base.SendMessage(message);
  23. }
  24. protected override MessageBase ParseResponse(byte[] rawMessage)
  25. {
  26. _lstCacheBuffer.AddRange(rawMessage);
  27. byte[] temps = _lstCacheBuffer.ToArray();
  28. P116PIDTCMessage msg = new P116PIDTCMessage();
  29. msg.IsResponse = false;
  30. msg.IsAck = false;
  31. msg.IsComplete = false;
  32. msg.RawMessage = _lstCacheBuffer.ToArray();
  33. if (temps.Length < 4) return msg;
  34. msg.DeviceAddress = temps[0];
  35. msg.FunctionCode = temps[1];
  36. if (msg.FunctionCode !=0x03)
  37. {
  38. msg.Data = _lstCacheBuffer.ToArray();
  39. }
  40. else
  41. {
  42. msg.Length = temps[2];
  43. if (temps.Length < msg.Length + 5)
  44. {
  45. //msg.IsFormatError = true;
  46. return msg;
  47. }
  48. msg.Data = _lstCacheBuffer.Skip(3).Take(msg.Length).ToArray();
  49. }
  50. msg.IsResponse = true;
  51. msg.IsAck = true;
  52. msg.IsComplete = true;
  53. return msg;
  54. }
  55. private static byte ModRTU_CRC(byte[] buffer)
  56. {
  57. //ushort crc = 0xFFFF;
  58. // var buf = System.Text.Encoding.UTF8.GetBytes(String.Join(Environment.NewLine, buffer));
  59. var buf = buffer;
  60. var len = buffer.Length;
  61. byte temp = buffer[0];
  62. for (int i = 1; i < buffer.Length; i++)
  63. {
  64. temp = (byte)(temp ^ buffer[i]);
  65. }
  66. return (byte)~temp;
  67. }
  68. }
  69. }