HstOcrReaderSimulator.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using MECF.Framework.Simulator.Core.Driver;
  8. namespace MECF.Framework.Simulator.Core.Aligners
  9. {
  10. public class HstOcrReaderSimulator : SocketDeviceSimulator
  11. {
  12. Random _rd = new Random();
  13. public HstOcrReaderSimulator( )
  14. : base(23, -1, "\r\n", ' ')
  15. {
  16. }
  17. public HstOcrReaderSimulator(int port)
  18. : base(port, -1, "\r\n", ' ')
  19. {
  20. }
  21. int GetSleepTime()
  22. {
  23. return 1000;
  24. }
  25. protected override void ProcessUnsplitMessage(string msg)
  26. {
  27. string result = "1";
  28. OnWriteMessage(result);
  29. if (msg.StartsWith("SM"))
  30. {
  31. result = string.Format("{0},{1:F2},{2:F2}", GenerateRandomNumber(12), _rd.Next(80, 100), 1);
  32. Task.Run(() =>
  33. {
  34. Thread.Sleep(GetSleepTime());
  35. OnWriteMessage(result);
  36. });
  37. }
  38. }
  39. private static char[] constant =
  40. {
  41. '0','1','2','3','4','5','6','7','8','9',
  42. 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
  43. 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
  44. };
  45. public static string GenerateRandomNumber(int Length)
  46. {
  47. System.Text.StringBuilder newRandom = new System.Text.StringBuilder(62);
  48. Random rd = new Random();
  49. for (int i = 0; i < Length; i++)
  50. {
  51. newRandom.Append(constant[rd.Next(62)]);
  52. }
  53. return newRandom.ToString();
  54. }
  55. }
  56. }