Serial.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. string tmpSTR = "";
  73. //this.main_form.RX_Light_ON();
  74. main_form.Invoke(new BoolSet(main_form.RX_Light_ON));
  75. if (this.Debug.Created && Debug.RadioButton_ascii.Checked == true)
  76. {
  77. string data = serialPort.ReadExisting();
  78. if (Debug.Created)
  79. if (Debug.RadioButton_ascii.Checked == true)
  80. Debug.Invoke(new StringSend(Debug.Data_Recv_Str), data);
  81. else
  82. Debug.Invoke(new StringSend(Debug.Data_Recv_Str), Str2hex(data, true));
  83. data_Handler.Recv_dataCheck(this,this.main_form,this.fileDownload, Str2bytes(data));
  84. }
  85. else
  86. {
  87. // 리스트 두개 사용
  88. int nLnegth = serialPort.BytesToRead;
  89. byte[] btdata = new byte[nLnegth];
  90. serialPort.Read(btdata, 0, nLnegth);
  91. if (nLnegth > 0)
  92. {
  93. if (this.Debug.Created)
  94. {
  95. if (this.Debug.radioButton_ascii.Checked == true)
  96. {
  97. tmpSTR = Encoding.Default.GetString(btdata).TrimEnd('\0');
  98. Debug.Invoke(new StringSend(Debug.Data_Recv_Str), Str2hex(tmpSTR, true));
  99. }
  100. else
  101. {
  102. Debug.Invoke(new ByteSend(Debug.Data_Recv_Hex), btdata);
  103. }
  104. }
  105. else
  106. {
  107. if (this.Debug.Created)
  108. Debug.Invoke(new ByteSend(Debug.Data_Recv_Hex), btdata);
  109. }
  110. data_Handler.Recv_dataCheck(this, this.main_form,this.fileDownload, btdata);
  111. // 이부분에서 데이타 처리하는 부분으로 전송하여 사용하면 됨
  112. }
  113. }
  114. /****
  115. *메모리 누수 방지용 코드
  116. */
  117. System.GC.Collect(0, GCCollectionMode.Forced);
  118. System.GC.WaitForFullGCComplete();
  119. }
  120. public Boolean Serial_PortOpen(ref Button Btn_Portonoff,ref ComboBox cb)
  121. {
  122. Boolean ret = false;
  123. try
  124. {
  125. if (serialPort.IsOpen) { //When the port is open
  126. serialPort.Close();
  127. Btn_Portonoff.Text = "Port Open";
  128. ret = true;
  129. }
  130. else//When the port is close
  131. {
  132. if (cb.Text != "")
  133. {
  134. serialPort.Open();
  135. Btn_Portonoff.Text = "Port Close";
  136. Debug.Debug_Main_Form_Get(this.main_form);
  137. }
  138. else
  139. {
  140. MessageBox.Show("Port is not set");
  141. ret = true;
  142. }
  143. }
  144. }
  145. catch
  146. {
  147. MessageBox.Show("already port open " + Serial_Name);
  148. }
  149. return ret;
  150. }
  151. public void Serial_TerminalOpen(object serial)
  152. {
  153. this.Debug.Serial_ClassSet(serial);
  154. try
  155. {
  156. this.Debug.Show();
  157. }
  158. catch
  159. {
  160. Debug = new Debug();
  161. this.Debug.Show();
  162. }
  163. }
  164. public void Serial_DataSend(byte[] data)
  165. {
  166. try
  167. {
  168. serialPort.Write(data,0,data.Length);
  169. main_form.pictureBox_R_TX.Visible = false;
  170. main_form.pictureBox_G_TX.Visible = true;
  171. }
  172. catch (System.Exception ex)
  173. {
  174. MessageBox.Show(ex.Message);
  175. }
  176. }
  177. public void Serial_DataSend(byte[] buffer, int count)
  178. {
  179. try {
  180. serialPort.Write(buffer, 0, count);
  181. main_form.Invoke(new BoolSet(main_form.TX_Light_ON));
  182. }
  183. catch { MessageBox.Show("Port Open Failed!!!"); }
  184. }
  185. public void FileDownloadClass_Get(object filedownload)
  186. {
  187. //this.fileDownload = (FileDownload)filedownload;
  188. this.fileDownload = (Update_Serial)filedownload;
  189. }
  190. public void Test_Serial()
  191. {
  192. byte[] tempdata = new byte[1024];
  193. for (int i = 0; i < 255; i++)
  194. {
  195. tempdata[i] = Convert.ToByte(i);
  196. }
  197. this.serialPort.Write(tempdata, 0, 255);
  198. //this.serialPort.Write(tempdata.ToString());
  199. }
  200. public void debug_hextoasciiConvert()
  201. {
  202. if(this.Debug.Created)
  203. this.Debug.hex_to_ascii_radiobuttonConvert();
  204. }
  205. public void debug_asciitohexConvert()
  206. {
  207. if (this.Debug.Created)
  208. this.Debug.ascii_to_hex_radiobuttonConvert();
  209. }
  210. public void Serial_Main_Form_Get(object frm)
  211. {
  212. this.main_form = (Main_Form)frm;
  213. }
  214. }
  215. }