Terminal.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Runtime.InteropServices;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. //Add code
  12. using System.IO;
  13. using System.IO.Ports;
  14. using Jdas_Mbic.Function;
  15. namespace Jdas_Mbic
  16. {
  17. public partial class Terminal : Form
  18. {
  19. public Terminal()
  20. {
  21. InitializeComponent();
  22. }
  23. private void Terminal_FormClosing(object sender, FormClosingEventArgs e)
  24. {
  25. this.Dispose();
  26. }
  27. JdasMbic main_form;
  28. int LineLimit = 500;
  29. [DllImport("user32.dll")]
  30. public static extern int SendMessage(IntPtr hWnd, Int32 wMsg, bool wParam, Int32 lParam);
  31. private const int WM_SETREDRAW = 11;
  32. public RadioButton RadioButton_ascii { get => radioButton_ascii; set => radioButton_ascii = value; }
  33. public RadioButton RadioButton_hex { get => radioButton_hex; set => radioButton_hex = value; }
  34. #if true
  35. public void Data_Recv_Str(string text)
  36. {
  37. int nLimitLines = Convert.ToInt32(LineLimit); //제한 라인 수
  38. try
  39. {
  40. try
  41. {
  42. SendMessage(this.Handle, WM_SETREDRAW, false, 0);
  43. }
  44. catch (Exception e) { MessageBox.Show(e.StackTrace); }
  45. if (tbReceived.Lines.Length > nLimitLines)
  46. {
  47. LinkedList<string> tempLines = new LinkedList<string>(tbReceived.Lines);
  48. while ((tempLines.Count - nLimitLines) > 0)
  49. {
  50. tempLines.RemoveFirst();
  51. }
  52. tbReceived.Lines = tempLines.ToArray();
  53. }
  54. try
  55. {
  56. SendMessage(this.Handle, WM_SETREDRAW, true, 0);
  57. }
  58. catch { return; }
  59. tbReceived.AppendText(text);
  60. tbReceived.SelectionStart = tbReceived.Text.Length;//맨 마지막 선택...
  61. tbReceived.ScrollToCaret();
  62. }
  63. catch { try { SendMessage(this.Handle, WM_SETREDRAW, true, 0); } catch { return; } }
  64. }
  65. #endif
  66. #if false
  67. public void Data_Recv_Hex(byte[] text)
  68. {
  69. int nLimitLines = Convert.ToInt32(LineLimit); //제한 라인 수
  70. try
  71. {
  72. try
  73. {
  74. SendMessage(this.Handle, WM_SETREDRAW, false, 0);
  75. }
  76. catch (Exception e) { MessageBox.Show(e.StackTrace); }
  77. if (tbReceived.Lines.Length > nLimitLines)
  78. {
  79. LinkedList<string> tempLines = new LinkedList<string>(tbReceived.Lines);
  80. while ((tempLines.Count - nLimitLines) > 0)
  81. {
  82. tempLines.RemoveFirst();
  83. }
  84. tbReceived.Lines = tempLines.ToArray();
  85. }
  86. try
  87. {
  88. SendMessage(this.Handle, WM_SETREDRAW, true, 0);
  89. }
  90. catch { return; }
  91. tbReceived.AppendText(text.ToString());
  92. tbReceived.SelectionStart = tbReceived.Text.Length;//맨 마지막 선택...
  93. tbReceived.ScrollToCaret();
  94. }
  95. catch { try { SendMessage(this.Handle, WM_SETREDRAW, true, 0); } catch { return; } }
  96. }
  97. #endif
  98. public void Data_Recv_Hex(byte[] text)
  99. {
  100. string AppendMessage = "";
  101. int nLimitLines = Convert.ToInt32(LineLimit); //제한 라인 수
  102. try
  103. {/*
  104. try
  105. {
  106. SendMessage(this.Handle, WM_SETREDRAW, false, 0);
  107. }
  108. catch (Exception e) { MessageBox.Show(e.StackTrace); }*/
  109. for (int i = 0; i < text.Length; i++)
  110. {
  111. AppendMessage += Convert.ToString(text[i], 16);
  112. AppendMessage += " ";
  113. }
  114. AppendMessage += "\r\n";
  115. tbReceived.AppendText(AppendMessage);
  116. if (tbReceived.Lines.Length > nLimitLines)
  117. {
  118. LinkedList<string> tempLines = new LinkedList<string>(tbReceived.Lines);
  119. while ((tempLines.Count - nLimitLines) > 0)
  120. {
  121. tempLines.RemoveFirst();
  122. }
  123. tbReceived.Lines = tempLines.ToArray();
  124. }
  125. try
  126. {
  127. SendMessage(this.Handle, WM_SETREDRAW, true, 0);
  128. }
  129. catch { return; }
  130. tbReceived.Select(tbReceived.Text.Length, 0);
  131. tbReceived.ScrollToCaret();
  132. }
  133. catch { try { SendMessage(this.Handle, WM_SETREDRAW, true, 0); } catch { return; } }
  134. }
  135. public void Data_Send(byte[] text)
  136. {
  137. string AppendMessage = "\n[TX]";
  138. int nLimitLines = Convert.ToInt32(LineLimit); //제한 라인 수
  139. try
  140. {
  141. for (int i = 0; i < text.Length; i++)
  142. {
  143. AppendMessage += Convert.ToString(text[i], 16);
  144. }
  145. tbReceived.AppendText(AppendMessage);
  146. try
  147. {
  148. SendMessage(this.Handle, WM_SETREDRAW, false, 0);
  149. }
  150. catch { return; }
  151. if (tbReceived.Lines.Length > nLimitLines)
  152. {
  153. LinkedList<string> tempLines = new LinkedList<string>(tbReceived.Lines);
  154. while ((tempLines.Count - nLimitLines) > 0)
  155. {
  156. tempLines.RemoveFirst();
  157. }
  158. tbReceived.Lines = tempLines.ToArray();
  159. }
  160. try
  161. {
  162. SendMessage(this.Handle, WM_SETREDRAW, true, 0);
  163. }
  164. catch { return; }
  165. tbReceived.Select(tbReceived.Text.Length, 0);
  166. tbReceived.ScrollToCaret();
  167. }
  168. finally { }
  169. }
  170. private void Button_Send_Click(object sender, EventArgs e)
  171. {
  172. }
  173. private void Button_Clear_Click(object sender, EventArgs e)
  174. {
  175. }
  176. public void hex_to_ascii_radiobuttonConvert()
  177. {
  178. radioButton_hex.Checked = true;
  179. radioButton_ascii.Checked = false;
  180. }
  181. public void ascii_to_hex_radiobuttonConvert()
  182. {
  183. radioButton_ascii.Checked = true;
  184. radioButton_hex.Checked = false;
  185. }
  186. public void Debug_Main_Form_Get(object frm)
  187. {
  188. this.main_form = (JdasMbic)frm;
  189. }
  190. //public string Serial_Name { get => serialPort.PortName; set => serialPort.PortName = value;}
  191. Data_Handler data_Handler = new Data_Handler();
  192. //FileDownload fileDownload;
  193. Update_Serial fileDownload = null;
  194. public void SetPortNameValues(object obj)
  195. {
  196. string[] ports = SerialPort.GetPortNames(); // load all name of com ports to string
  197. try
  198. {
  199. ((ComboBox)obj).Items.Clear(); //delete previous names in combobox items
  200. }
  201. catch { }
  202. ((ComboBox)obj).DataSource = null;
  203. foreach (string port in ports) //add this names to comboboxPort items
  204. {
  205. ((ComboBox)obj).Items.Add(port); //if there are some com ports ,select first
  206. }
  207. if (((ComboBox)obj).Items.Count > 0)
  208. {
  209. ((ComboBox)obj).SelectedIndex = 0;
  210. }
  211. else
  212. {
  213. ((ComboBox)obj).Text = " "; //if there are no com ports ,write Empty
  214. }
  215. }
  216. public void Serial_Initialize(ref ComboBox cb_port)
  217. {
  218. this.serialPort = new System.IO.Ports.SerialPort();
  219. this.serialPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(this.Serial_DataRecvFunction);
  220. cb_port.BeginUpdate();
  221. foreach (string comport in SerialPort.GetPortNames())//foreach (string comport in SerialPort_TestProgram.GetPortNames())
  222. {
  223. cb_port.Items.Add(comport);
  224. }
  225. cb_port.EndUpdate();
  226. //SerialPort 초기 설정.
  227. // serialPort.Encoding = Encoding.GetEncoding("Windows-1252");
  228. cb_port.DataSource = SerialPort.GetPortNames();
  229. try
  230. {
  231. serialPort_JdasMbic.PortName = cb_port.SelectedItem.ToString();
  232. serialPort_JdasMbic.BaudRate = (int)921600;
  233. serialPort_JdasMbic.DataBits = (int)8;
  234. serialPort_JdasMbic.Parity = System.IO.Ports.Parity.None;
  235. serialPort_JdasMbic.StopBits = StopBits.One;
  236. }
  237. catch { }
  238. }
  239. private delegate void StringSend(string Text);
  240. private delegate void ByteSend(byte[] Text);
  241. public string Str2hex(string strData,Boolean Compotable)
  242. {
  243. string resultHex = string.Empty;
  244. byte[] arr_byteStr = Encoding.Default.GetBytes(strData);
  245. foreach (byte byteStr in arr_byteStr)
  246. {
  247. if(Compotable == true)
  248. resultHex += string.Format("{0:X2}", byteStr) + " ";
  249. else
  250. resultHex += string.Format("{0:X2}", byteStr);
  251. }
  252. return resultHex;
  253. }
  254. static public byte[] Str2bytes(string byteData)
  255. {
  256. #if false
  257. System.Text.ASCIIEncoding asencoding = new System.Text.ASCIIEncoding();
  258. return Encoding.Default.GetBytes(byteData);
  259. #else
  260. byte[] arr_byteStr = Encoding.Default.GetBytes(byteData);
  261. return arr_byteStr;
  262. #endif
  263. }
  264. private delegate void BoolSet();
  265. public void Serial_DataRecvFunction(object sender, SerialDataReceivedEventArgs e)
  266. {
  267. string tmpSTR = "";
  268. Data_Handler data_Handler = new Data_Handler();
  269. int nLnegth = serialPort.BytesToRead;
  270. byte[] btdata = new byte[nLnegth];
  271. serialPort.Read(btdata, 0, nLnegth);
  272. main_form.Invoke(new BoolSet(main_form.RX_Light_ON));
  273. main_form.TX_RX_Light = true;
  274. if (this.Debug.Created && Debug.RadioButton_ascii.Checked == true)
  275. {
  276. string data = Encoding.Default.GetString(btdata);
  277. // string data = serialPort.ReadExisting();
  278. if (Debug.RadioButton_ascii.Checked == true)
  279. Debug.Invoke(new StringSend(Debug.Data_Recv_Str), data);
  280. else
  281. Debug.Invoke(new StringSend(Debug.Data_Recv_Str), Str2hex(data, true));
  282. data_Handler.Recv_dataCheck(this,this.main_form,this.fileDownload, btdata);
  283. }
  284. else
  285. {
  286. // 리스트 두개 사용
  287. if (nLnegth > 0)
  288. {
  289. if (this.Debug.Created)
  290. {
  291. if (this.Debug.radioButton_ascii.Checked == true)
  292. {
  293. tmpSTR = Encoding.Default.GetString(btdata).TrimEnd('\0');
  294. Debug.Invoke(new StringSend(Debug.Data_Recv_Str), Str2hex(tmpSTR, true));
  295. }
  296. else
  297. {
  298. Debug.Invoke(new ByteSend(Debug.Data_Recv_Hex), btdata);
  299. }
  300. }
  301. else
  302. {
  303. if (this.Debug.Created)
  304. Debug.Invoke(new ByteSend(Debug.Data_Recv_Hex), btdata);
  305. }
  306. // 이부분에서 데이타 처리하는 부분으로 전송하여 사용하면 됨
  307. }
  308. data_Handler.Recv_dataCheck(this, this.main_form,this.fileDownload, btdata);
  309. }
  310. /****
  311. *메모리 누수 방지용 코드
  312. */
  313. System.GC.Collect(0, GCCollectionMode.Forced);
  314. System.GC.WaitForFullGCComplete();
  315. }
  316. public Boolean Serial_PortOpen(ref Button Btn_Portonoff,ref ComboBox cb)
  317. {
  318. Boolean ret = false;
  319. try
  320. {
  321. if (serialPort.IsOpen) { //When the port is open
  322. serialPort.Close();
  323. Btn_Portonoff.Text = "Port Open";
  324. ret = true;
  325. }
  326. else//When the port is close
  327. {
  328. if (cb.Text != "")
  329. {
  330. serialPort.Open();
  331. Btn_Portonoff.Text = "Port Close";
  332. Debug.Debug_Main_Form_Get(this.main_form);
  333. }
  334. else
  335. {
  336. MessageBox.Show("Port is not set");
  337. ret = true;
  338. }
  339. }
  340. }
  341. catch
  342. {
  343. MessageBox.Show("already port open " + Serial_Name);
  344. }
  345. return ret;
  346. }
  347. public void Serial_TerminalOpen(object serial)
  348. {
  349. this.Debug.Serial_ClassSet(serial);
  350. try
  351. {
  352. this.Debug.Show();
  353. }
  354. catch
  355. {
  356. Debug = new Debug();
  357. this.Debug.Show();
  358. }
  359. }
  360. public void Serial_DataSend(byte[] data)
  361. {
  362. try
  363. {
  364. serialPort.Write(data,0,data.Length);
  365. main_form.pictureBox_R_TX.Visible = false;
  366. main_form.pictureBox_G_TX.Visible = true;
  367. }
  368. catch (System.Exception ex)
  369. {
  370. MessageBox.Show(ex.Message);
  371. }
  372. }
  373. public void Serial_DataSend(byte[] buffer, int count)
  374. {
  375. try {
  376. serialPort.Write(buffer, 0, count);
  377. main_form.Invoke(new BoolSet(main_form.TX_Light_ON));
  378. main_form.TX_RX_Light = true;
  379. }
  380. catch
  381. {
  382. System.Diagnostics.Process.GetCurrentProcess().Kill();
  383. MessageBox.Show("Port Open Failed!!!");
  384. }
  385. }
  386. public void FileDownloadClass_Set(object filedownload)
  387. {
  388. this.fileDownload = (Update_Serial)filedownload;
  389. }
  390. public object FileDownloadClass_Get()
  391. {
  392. return this.fileDownload;
  393. }
  394. public void Test_Serial()
  395. {
  396. byte[] tempdata = new byte[1024];
  397. for (int i = 0; i < 255; i++)
  398. {
  399. tempdata[i] = Convert.ToByte(i);
  400. }
  401. this.serialPort.Write(tempdata, 0, 255);
  402. //this.serialPort.Write(tempdata.ToString());
  403. }
  404. public void debug_hextoasciiConvert()
  405. {
  406. if(this.Debug.Created)
  407. this.Debug.hex_to_ascii_radiobuttonConvert();
  408. }
  409. public void debug_asciitohexConvert()
  410. {
  411. if (this.Debug.Created)
  412. this.Debug.ascii_to_hex_radiobuttonConvert();
  413. }
  414. public void Serial_Main_Form_Get(object frm)
  415. {
  416. this.main_form = (Main_Form)frm;
  417. }
  418. }
  419. }