PageRenderTime 102ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/SSPComm/SSPTransport/SSPChannel.cs

http://sspcomm.googlecode.com/
C# | 191 lines | 166 code | 24 blank | 1 comment | 19 complexity | c5e69b9010f7677d843f2f9ee6bea97f MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO.Ports;
  5. using SSPComm.SSPCommands;
  6. using System.Diagnostics;
  7. using System.Collections;
  8. namespace SSPComm.SSPTransport
  9. {
  10. internal class SSPChannel
  11. {
  12. private static Byte SYNC_BYTE = 0x7F;
  13. private static Int32 SSP_TIMEOUT_READ = 1000;
  14. private static Int32 SSP_TIMEOUT_WRITE = 1000;
  15. private readonly TraceListener _logger;
  16. SerialPort _port;
  17. public SSPChannel(String inComPort)
  18. {
  19. _port = new SerialPort(inComPort, 9600, Parity.None, 8, StopBits.Two);
  20. _port.ReadTimeout = SSP_TIMEOUT_READ;
  21. _port.WriteTimeout = SSP_TIMEOUT_WRITE;
  22. _port.RtsEnable = false;
  23. _port.Handshake = Handshake.None;
  24. _port.DiscardNull = false;
  25. _port.DtrEnable = false;
  26. _logger = new TextWriterTraceListener(String.Concat(ToString(),".txt"));
  27. }
  28. internal Boolean IsOpen
  29. {
  30. get
  31. {
  32. return _port.IsOpen;
  33. }
  34. }
  35. public SSPCommand SendAndReceive(SSPCommand inCmd)
  36. {
  37. SendCommand(inCmd);
  38. return ReceiveCommand();
  39. }
  40. private void SendCommand(SSPCommand inCmd)
  41. {
  42. Byte[] cmdStuffed = SSPTransportStuff.InsertByteStuff(inCmd.RawData);
  43. Byte[] data2send = new Byte[cmdStuffed.Length + 1];
  44. data2send[0] = SYNC_BYTE;
  45. cmdStuffed.CopyTo(data2send, 1);
  46. _port.Write(data2send, 0, data2send.Length);
  47. _logger.WriteLine(String.Format("{0}\t>\t{1}",GetTime(), GetByteArray(data2send)), this.ToString());
  48. }
  49. #region stuff
  50. private static String GetByteArray(Byte[] inData)
  51. {
  52. StringBuilder builder = new StringBuilder(inData.Length * 3);
  53. IEnumerator en = inData.GetEnumerator();
  54. while (en.MoveNext())
  55. {
  56. builder.Append(String.Format("0x{0} ", ((Byte)en.Current).ToString("X2")));
  57. }
  58. return builder.ToString();
  59. }
  60. private static String GetTime()
  61. {
  62. return DateTime.Now.ToString("ss.fff");
  63. }
  64. #endregion
  65. private SSPCommand ReceiveCommand()
  66. {
  67. int readedByte = 0;
  68. bool sync = false;
  69. Boolean packetReceived = false;
  70. List<Byte> result = new List<Byte>(10);
  71. while (!packetReceived)
  72. {
  73. readedByte = _port.ReadByte();
  74. if (readedByte == -1)
  75. {
  76. break;
  77. }
  78. _logger.WriteLine(String.Format("{0}\t<\t{1}", GetTime(), GetByteArray(new Byte[] { (Byte)readedByte })), this.ToString());
  79. if (readedByte == SYNC_BYTE)
  80. {
  81. sync = true;
  82. }
  83. else if (sync)
  84. {
  85. result.Add((Byte)readedByte);
  86. readedByte = _port.ReadByte();
  87. if (readedByte == -1)
  88. {
  89. break;
  90. }
  91. result.Add((Byte)readedByte);
  92. //CRC LEN
  93. Int32 waitCounter = result[1] + 2;
  94. if (readedByte == SYNC_BYTE)
  95. {
  96. waitCounter++;
  97. }
  98. Boolean syncPacket = false;
  99. packetReceived = true;
  100. while (waitCounter > 0)
  101. {
  102. readedByte = _port.ReadByte();
  103. if (readedByte == -1)
  104. {
  105. break;
  106. }
  107. result.Add((Byte)readedByte);
  108. if (readedByte == SYNC_BYTE)
  109. {
  110. if (!syncPacket)
  111. {
  112. syncPacket = true;
  113. waitCounter++;
  114. }
  115. else
  116. {
  117. syncPacket = false;
  118. }
  119. }
  120. waitCounter--;
  121. }
  122. }
  123. }
  124. _logger.WriteLine(String.Format("{0}\t<\t{1}", GetTime(), GetByteArray(result.ToArray())), this.ToString());
  125. Byte[] resultwoStuff = SSPTransportStuff.DeleteByteStuff(result.ToArray());
  126. if (!SSPTransportStuff.IsCRCValid(resultwoStuff))
  127. {
  128. throw new Exception("crc");
  129. }
  130. _port.ReadExisting();
  131. return new SSPCommand(resultwoStuff);
  132. }
  133. public void Open()
  134. {
  135. _logger.WriteLine(String.Format("{0}\to",GetTime()), this.ToString());
  136. try
  137. {
  138. _port.Open();
  139. }
  140. catch (Exception exc)
  141. {
  142. _logger.WriteLine(String.Format("{0}\tFo", GetTime()), this.ToString());
  143. throw exc;
  144. }
  145. _logger.WriteLine(String.Format("{0}\tO",GetTime()), this.ToString());
  146. }
  147. public void Close()
  148. {
  149. _logger.WriteLine(String.Format("{0}\tc",GetTime()), this.ToString());
  150. try
  151. {
  152. _port.Close();
  153. }
  154. catch (Exception exc)
  155. {
  156. _logger.WriteLine(String.Format("{0}\tFc",GetTime()), this.ToString());
  157. throw exc;
  158. }
  159. _logger.WriteLine(String.Format("{0}\tC",GetTime()), this.ToString());
  160. _logger.Flush();
  161. }
  162. public override string ToString()
  163. {
  164. return String.Format("SSPChannel-{0}", _port.PortName);
  165. }
  166. }
  167. }