Serial.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. //Add code
  7. using System.Windows.Forms;
  8. using System.IO;
  9. using System.IO.Ports;
  10. namespace RF_TRIO_PLL_ZIG
  11. {
  12. class Serial
  13. {
  14. private System.IO.Ports.SerialPort serialPort;
  15. private Debug Debug = new Debug(); // Teminal Text Wnd Open
  16. public string Serial_Name { get => serialPort.PortName; set => serialPort.PortName = value;}
  17. Data_Handler data_Handler = new Data_Handler();
  18. //FileDownload fileDownload;
  19. Update_Serial fileDownload;
  20. public void Serial_Initialize(ref ComboBox cb_port)
  21. {
  22. this.serialPort = new System.IO.Ports.SerialPort();
  23. this.serialPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(this.Serial_DataRecvFunction);
  24. cb_port.BeginUpdate();
  25. foreach (string comport in SerialPort.GetPortNames())//foreach (string comport in SerialPort_TestProgram.GetPortNames())
  26. {
  27. cb_port.Items.Add(comport);
  28. }
  29. cb_port.EndUpdate();
  30. //SerialPort 초기 설정.
  31. // serialPort.Encoding = Encoding.GetEncoding("Windows-1252");
  32. cb_port.DataSource = SerialPort.GetPortNames();
  33. try
  34. {
  35. serialPort.PortName = Serial_Name = cb_port.SelectedItem.ToString();
  36. serialPort.BaudRate = (int)115200;
  37. serialPort.DataBits = (int)8;
  38. serialPort.Parity = System.IO.Ports.Parity.None;
  39. serialPort.StopBits = StopBits.One;
  40. }
  41. catch { }
  42. }
  43. private delegate void StringSend(string Text);
  44. private delegate void ByteSend(byte[] Text);
  45. public string Str2hex(string strData,Boolean Compotable)
  46. {
  47. string resultHex = string.Empty;
  48. byte[] arr_byteStr = Encoding.Default.GetBytes(strData);
  49. foreach (byte byteStr in arr_byteStr)
  50. {
  51. if(Compotable == true)
  52. resultHex += string.Format("{0:X2}", byteStr) + " ";
  53. else
  54. resultHex += string.Format("{0:X2}", byteStr);
  55. }
  56. return resultHex;
  57. }
  58. static public byte[] Str2bytes(string byteData)
  59. {
  60. #if false
  61. System.Text.ASCIIEncoding asencoding = new System.Text.ASCIIEncoding();
  62. return Encoding.Default.GetBytes(byteData);
  63. #else
  64. byte[] arr_byteStr = Encoding.Default.GetBytes(byteData);
  65. return arr_byteStr;
  66. #endif
  67. }
  68. public void Serial_DataRecvFunction(object sender, SerialDataReceivedEventArgs e)
  69. {
  70. if (this.Debug.Created && Debug.RadioButton_ascii.Checked == true)
  71. {
  72. string data = serialPort.ReadExisting();
  73. if (Debug.Created)
  74. if (Debug.RadioButton_ascii.Checked == true)
  75. Debug.Invoke(new StringSend(Debug.Data_Recv_Str), data);
  76. else
  77. Debug.Invoke(new StringSend(Debug.Data_Recv_Str), Str2hex(data, true));
  78. data_Handler.Recv_dataCheck(this.fileDownload, Str2bytes(data));
  79. }
  80. else
  81. {
  82. // 리스트 두개 사용
  83. int nLnegth = serialPort.BytesToRead;
  84. byte[] btdata = new byte[nLnegth];
  85. serialPort.Read(btdata, 0, nLnegth);
  86. if (nLnegth > 0)
  87. {
  88. if (this.Debug.Created)
  89. {
  90. string tmpSTR = Encoding.Default.GetString(btdata).TrimEnd('\0');
  91. Debug.Invoke(new StringSend(Debug.Data_Recv_Str), Str2hex(tmpSTR, true));
  92. }
  93. data_Handler.Recv_dataCheck(this.fileDownload, btdata);
  94. // 이부분에서 데이타 처리하는 부분으로 전송하여 사용하면 됨
  95. }
  96. }
  97. /****
  98. *메모리 누수 방지용 코드
  99. */
  100. System.GC.Collect(0, GCCollectionMode.Forced);
  101. System.GC.WaitForFullGCComplete();
  102. }
  103. public Boolean Serial_PortOpen(ref Button Btn_Portonoff,ref ComboBox cb)
  104. {
  105. Boolean ret = false;
  106. try
  107. {
  108. if (serialPort.IsOpen) { //When the port is open
  109. serialPort.Close();
  110. Btn_Portonoff.Text = "Port Open";
  111. ret = true;
  112. }
  113. else//When the port is close
  114. {
  115. if (cb.Text != "")
  116. {
  117. serialPort.Open();
  118. Btn_Portonoff.Text = "Port Close";
  119. }
  120. else
  121. {
  122. MessageBox.Show("Port is not set");
  123. ret = true;
  124. }
  125. }
  126. }
  127. catch
  128. {
  129. MessageBox.Show("already port open " + Serial_Name);
  130. }
  131. return ret;
  132. }
  133. public void Serial_TerminalOpen(object serial)
  134. {
  135. this.Debug.Serial_ClassSet(serial);
  136. try
  137. {
  138. this.Debug.Show();
  139. }
  140. catch
  141. {
  142. Debug = new Debug();
  143. this.Debug.Show();
  144. }
  145. }
  146. public void Serial_DataSend(byte[] data)
  147. {
  148. try
  149. {
  150. serialPort.Write(data,0,data.Length);
  151. }
  152. catch (System.Exception ex)
  153. {
  154. MessageBox.Show(ex.Message);
  155. }
  156. }
  157. public void Serial_DataSend(byte[] buffer, int count)
  158. {
  159. try { serialPort.Write(buffer, 0, count); }
  160. catch { MessageBox.Show("Port Open Failed!!!"); }
  161. }
  162. public void FileDownloadClass_Get(object filedownload)
  163. {
  164. //this.fileDownload = (FileDownload)filedownload;
  165. this.fileDownload = (Update_Serial)filedownload;
  166. }
  167. public void Test_Serial()
  168. {
  169. byte[] tempdata = new byte[1024];
  170. for (int i = 0; i < 255; i++)
  171. {
  172. tempdata[i] = Convert.ToByte(i);
  173. }
  174. this.serialPort.Write(tempdata, 0, 255);
  175. //this.serialPort.Write(tempdata.ToString());
  176. }
  177. public void debug_hextoasciiConvert()
  178. {
  179. if(this.Debug.Created)
  180. this.Debug.hex_to_ascii_radiobuttonConvert();
  181. }
  182. public void debug_asciitohexConvert()
  183. {
  184. if (this.Debug.Created)
  185. this.Debug.ascii_to_hex_radiobuttonConvert();
  186. }
  187. }
  188. }