Serial.cs 7.4 KB

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