HstConnection.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System.Net;
  2. using System.Text;
  3. using System.Threading;
  4. using Aitex.Core.RT.Event;
  5. using Aitex.Core.RT.Log;
  6. using Aitex.Core.RT.SCCore;
  7. using Aitex.Core.Util;
  8. using MECF.Framework.Common.Communications;
  9. using MECF.Framework.Common.Communications.Tcp.Socket.Client.APM;
  10. using MECF.Framework.Common.Communications.Tcp.Socket.Client.APM.EventArgs;
  11. using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Robot;
  12. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.OcrReaders.HST
  13. {
  14. public class HstMessage : AsciiMessage
  15. {
  16. public string Data { get; set; }
  17. }
  18. public class HstConnection:HstConnectionBase
  19. {
  20. public HstConnection(string addr) : base(addr)
  21. {
  22. }
  23. public void EnableLog(bool enable)
  24. {
  25. }
  26. protected override void OnDataReceived(string oneLineMessage)
  27. {
  28. MessageBase msg = ParseResponse(oneLineMessage);
  29. if (msg.IsFormatError)
  30. {
  31. SetCommunicationError(true, "received invalid response message.");
  32. }
  33. lock (_lockerActiveHandler)
  34. {
  35. if (_activeHandler != null)
  36. {
  37. if (msg.IsFormatError || (_activeHandler.HandleMessage(msg, out bool completed) && completed))
  38. {
  39. _activeHandler = null;
  40. }
  41. }
  42. }
  43. }
  44. protected override MessageBase ParseResponse(string rawText)
  45. {
  46. HstMessage msg = new HstMessage();
  47. msg.IsResponse = false;
  48. msg.IsAck = false;
  49. msg.IsComplete = false;
  50. string tempdata = rawText.Replace("\r", "").Replace("\n", "");
  51. msg.RawMessage = rawText;
  52. if(tempdata.Contains("Welcome")) msg.IsAck = true;
  53. if (tempdata == "1") msg.IsAck = true;
  54. else if (tempdata.Length == 1) msg.IsNak = true;
  55. else
  56. {
  57. msg.IsResponse = true;
  58. msg.Data = rawText;
  59. }
  60. return msg;
  61. }
  62. }
  63. }