Serial.cs 8.4 KB

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