Serial.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 Basic_Terminal
  11. {
  12. class Serial
  13. {
  14. private System.IO.Ports.SerialPort serialPort;
  15. private Debug Debug = new Debug(); // Teminal Text Wnd Open
  16. public string Serial_Name { get => serialPort.PortName; set => serialPort.PortName = value;}
  17. Data_Handler data_Handler = new Data_Handler();
  18. //FileDownload fileDownload;
  19. Update_Serial fileDownload;
  20. public void Serial_Initialize(ref ComboBox cb_port)
  21. {
  22. this.serialPort = new System.IO.Ports.SerialPort();
  23. this.serialPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(this.Serial_DataRecvFunction);
  24. cb_port.BeginUpdate();
  25. foreach (string comport in SerialPort.GetPortNames())//foreach (string comport in SerialPort_TestProgram.GetPortNames())
  26. {
  27. cb_port.Items.Add(comport);
  28. }
  29. cb_port.EndUpdate();
  30. //SerialPort 초기 설정.
  31. serialPort.Encoding = Encoding.GetEncoding("Windows-1252");
  32. cb_port.DataSource = SerialPort.GetPortNames();
  33. serialPort.PortName = Serial_Name = cb_port.SelectedItem.ToString();
  34. serialPort.BaudRate = (int)115200;
  35. serialPort.DataBits = (int)8;
  36. serialPort.Parity = System.IO.Ports.Parity.None;
  37. serialPort.StopBits = StopBits.One;
  38. }
  39. private delegate void StringSend(string Text);
  40. private delegate void ByteSend(byte[] Text);
  41. public string Str2hex(string strData,Boolean Compotable)
  42. {
  43. string resultHex = string.Empty;
  44. byte[] arr_byteStr = Encoding.Default.GetBytes(strData);
  45. foreach (byte byteStr in arr_byteStr)
  46. {
  47. if(Compotable == true)
  48. resultHex += string.Format("{0:X2}", byteStr) + " ";
  49. else
  50. resultHex += string.Format("{0:X2}", byteStr);
  51. }
  52. return resultHex;
  53. }
  54. static public byte[] Str2bytes(string byteData)
  55. {
  56. #if false
  57. System.Text.ASCIIEncoding asencoding = new System.Text.ASCIIEncoding();
  58. return Encoding.Default.GetBytes(byteData);
  59. #else
  60. byte[] arr_byteStr = Encoding.Default.GetBytes(byteData);
  61. return arr_byteStr;
  62. #endif
  63. }
  64. public void Serial_DataRecvFunction(object sender, SerialDataReceivedEventArgs e)
  65. {
  66. if (Debug.RadioButton_ascii.Checked == true)
  67. {
  68. string data = serialPort.ReadExisting();
  69. if (Debug.Created)
  70. if (Debug.RadioButton_ascii.Checked == true)
  71. Debug.Invoke(new StringSend(Debug.Data_Recv_Str), data);
  72. else
  73. Debug.Invoke(new StringSend(Debug.Data_Recv_Str), Str2hex(data, true));
  74. data_Handler.Recv_dataCheck(this.fileDownload, Str2bytes(data));
  75. }
  76. else
  77. {
  78. // 리스트 두개 사용
  79. int nLnegth = serialPort.BytesToRead;
  80. byte[] btdata = new byte[nLnegth];
  81. serialPort.Read(btdata, 0, nLnegth);
  82. if (nLnegth > 0)
  83. {
  84. string tmpSTR = Encoding.Default.GetString(btdata).TrimEnd('\0');
  85. Debug.Invoke(new StringSend(Debug.Data_Recv_Str), Str2hex(tmpSTR, true));
  86. data_Handler.Recv_dataCheck(this.fileDownload, btdata);
  87. // 이부분에서 데이타 처리하는 부분으로 전송하여 사용하면 됨
  88. }
  89. }
  90. /****
  91. *메모리 누수 방지용 코드
  92. */
  93. System.GC.Collect(0, GCCollectionMode.Forced);
  94. System.GC.WaitForFullGCComplete();
  95. }
  96. public Boolean Serial_PortOpen(ref Button Btn_Portonoff)
  97. {
  98. Boolean ret = false;
  99. try
  100. {
  101. if (serialPort.IsOpen) { //When the port is open
  102. serialPort.Close();
  103. Btn_Portonoff.Text = "Port Open";
  104. ret = true;
  105. }
  106. else//When the port is close
  107. {
  108. serialPort.Open();
  109. Btn_Portonoff.Text = "Port Close";
  110. }
  111. }
  112. catch
  113. {
  114. MessageBox.Show("already port open " + Serial_Name);
  115. }
  116. return ret;
  117. }
  118. public void Serial_TerminalOpen(object serial)
  119. {
  120. this.Debug.Serial_ClassSet(serial);
  121. try
  122. {
  123. this.Debug.Show();
  124. }
  125. catch
  126. {
  127. Debug = new Debug();
  128. this.Debug.Show();
  129. }
  130. }
  131. public void Serial_DataSend(byte[] data)
  132. {
  133. try
  134. {
  135. serialPort.Write(data,0,data.Length);
  136. }
  137. catch (System.Exception ex)
  138. {
  139. MessageBox.Show(ex.Message);
  140. }
  141. }
  142. public void Serial_DataSend(byte[] buffer, int count)
  143. {
  144. try { serialPort.Write(buffer, 0, count); }
  145. catch { MessageBox.Show("Port Open Failed!!!"); }
  146. }
  147. public void FileDownloadClass_Get(object filedownload)
  148. {
  149. //this.fileDownload = (FileDownload)filedownload;
  150. this.fileDownload = (Update_Serial)filedownload;
  151. }
  152. public void Test_Serial()
  153. {
  154. byte[] tempdata = new byte[1024];
  155. for (int i = 0; i < 255; i++)
  156. {
  157. tempdata[i] = Convert.ToByte(i);
  158. }
  159. this.serialPort.Write(tempdata, 0, 255);
  160. //this.serialPort.Write(tempdata.ToString());
  161. }
  162. }
  163. }