Serial.cs 9.0 KB

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