123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- //Add code
- using System.Windows.Forms;
- using System.IO;
- using System.IO.Ports;
- namespace RF_TRIO_PLL_ZIG
- {
- class Serial
- {
- private System.IO.Ports.SerialPort serialPort;
- private Debug Debug = new Debug(); // Teminal Text Wnd Open
- public string Serial_Name { get => serialPort.PortName; set => serialPort.PortName = value;}
- Data_Handler data_Handler = new Data_Handler();
- //FileDownload fileDownload;
- Update_Serial fileDownload;
- public void Serial_Initialize(ref ComboBox cb_port)
- {
-
- this.serialPort = new System.IO.Ports.SerialPort();
- this.serialPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(this.Serial_DataRecvFunction);
- cb_port.BeginUpdate();
- foreach (string comport in SerialPort.GetPortNames())//foreach (string comport in SerialPort_TestProgram.GetPortNames())
- {
- cb_port.Items.Add(comport);
- }
- cb_port.EndUpdate();
- //SerialPort 초기 설정.
- // serialPort.Encoding = Encoding.GetEncoding("Windows-1252");
- cb_port.DataSource = SerialPort.GetPortNames();
- try
- {
- serialPort.PortName = Serial_Name = cb_port.SelectedItem.ToString();
- serialPort.BaudRate = (int)115200;
- serialPort.DataBits = (int)8;
- serialPort.Parity = System.IO.Ports.Parity.None;
- serialPort.StopBits = StopBits.One;
- }
- catch { }
- }
- private delegate void StringSend(string Text);
- private delegate void ByteSend(byte[] Text);
- public string Str2hex(string strData,Boolean Compotable)
- {
- string resultHex = string.Empty;
- byte[] arr_byteStr = Encoding.Default.GetBytes(strData);
- foreach (byte byteStr in arr_byteStr)
- {
- if(Compotable == true)
- resultHex += string.Format("{0:X2}", byteStr) + " ";
- else
- resultHex += string.Format("{0:X2}", byteStr);
- }
- return resultHex;
- }
- static public byte[] Str2bytes(string byteData)
- {
- #if false
- System.Text.ASCIIEncoding asencoding = new System.Text.ASCIIEncoding();
- return Encoding.Default.GetBytes(byteData);
- #else
- byte[] arr_byteStr = Encoding.Default.GetBytes(byteData);
- return arr_byteStr;
- #endif
- }
-
- public void Serial_DataRecvFunction(object sender, SerialDataReceivedEventArgs e)
- {
- if (this.Debug.Created && Debug.RadioButton_ascii.Checked == true)
- {
- string data = serialPort.ReadExisting();
- if (Debug.Created)
- if (Debug.RadioButton_ascii.Checked == true)
- Debug.Invoke(new StringSend(Debug.Data_Recv_Str), data);
- else
- Debug.Invoke(new StringSend(Debug.Data_Recv_Str), Str2hex(data, true));
- data_Handler.Recv_dataCheck(this.fileDownload, Str2bytes(data));
- }
- else
- {
- // 리스트 두개 사용
- int nLnegth = serialPort.BytesToRead;
- byte[] btdata = new byte[nLnegth];
- serialPort.Read(btdata, 0, nLnegth);
- if (nLnegth > 0)
- {
- if (this.Debug.Created)
- {
- string tmpSTR = Encoding.Default.GetString(btdata).TrimEnd('\0');
- Debug.Invoke(new StringSend(Debug.Data_Recv_Str), Str2hex(tmpSTR, true));
- }
- data_Handler.Recv_dataCheck(this.fileDownload, btdata);
-
- // 이부분에서 데이타 처리하는 부분으로 전송하여 사용하면 됨
- }
- }
- /****
- *메모리 누수 방지용 코드
- */
- System.GC.Collect(0, GCCollectionMode.Forced);
- System.GC.WaitForFullGCComplete();
- }
- public Boolean Serial_PortOpen(ref Button Btn_Portonoff,ref ComboBox cb)
- {
- Boolean ret = false;
- try
- {
- if (serialPort.IsOpen) { //When the port is open
- serialPort.Close();
- Btn_Portonoff.Text = "Port Open";
- ret = true;
- }
- else//When the port is close
- {
- if (cb.Text != "")
- {
- serialPort.Open();
- Btn_Portonoff.Text = "Port Close";
- }
- else
- {
- MessageBox.Show("Port is not set");
- ret = true;
- }
- }
- }
- catch
- {
- MessageBox.Show("already port open " + Serial_Name);
-
- }
- return ret;
- }
- public void Serial_TerminalOpen(object serial)
- {
- this.Debug.Serial_ClassSet(serial);
- try
- {
- this.Debug.Show();
- }
- catch
- {
- Debug = new Debug();
- this.Debug.Show();
- }
- }
- public void Serial_DataSend(byte[] data)
- {
- try
- {
- serialPort.Write(data,0,data.Length);
- }
- catch (System.Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- public void Serial_DataSend(byte[] buffer, int count)
- {
- try { serialPort.Write(buffer, 0, count); }
- catch { MessageBox.Show("Port Open Failed!!!"); }
- }
- public void FileDownloadClass_Get(object filedownload)
- {
- //this.fileDownload = (FileDownload)filedownload;
- this.fileDownload = (Update_Serial)filedownload;
- }
- public void Test_Serial()
- {
- byte[] tempdata = new byte[1024];
- for (int i = 0; i < 255; i++)
- {
- tempdata[i] = Convert.ToByte(i);
-
-
- }
- this.serialPort.Write(tempdata, 0, 255);
- //this.serialPort.Write(tempdata.ToString());
- }
- public void debug_hextoasciiConvert()
- {
- if(this.Debug.Created)
- this.Debug.hex_to_ascii_radiobuttonConvert();
- }
- public void debug_asciitohexConvert()
- {
- if (this.Debug.Created)
- this.Debug.ascii_to_hex_radiobuttonConvert();
- }
- }
- }
|