Serial.cs 8.2 KB

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