Serial.cs 10 KB

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