You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
224 lines
6.1 KiB
224 lines
6.1 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
using System.IO.Ports;
|
|
|
|
namespace RS232Test
|
|
{
|
|
public partial class Form1 : Form
|
|
{
|
|
private string portName;
|
|
|
|
private const char STX = (char)0x02;
|
|
private const char CR = (char)0x0D;
|
|
private const char LF = (char)0x0A;
|
|
|
|
private const string SEPERATOR = "\r\n";
|
|
|
|
private string serialStream = "";
|
|
|
|
private object receiveLock = new object();
|
|
|
|
public Form1()
|
|
{
|
|
InitializeComponent();
|
|
InitInstance();
|
|
}
|
|
|
|
private void InitInstance()
|
|
{
|
|
GetPortNames();
|
|
}
|
|
|
|
private void GetPortNames()
|
|
{
|
|
DataTable portTable = new DataTable();
|
|
portTable.Columns.Add("VALUE", typeof(string));
|
|
|
|
string[] portNames = SerialPort.GetPortNames();
|
|
if (portNames.Length < 1)
|
|
return;
|
|
|
|
for (int i = 0; i < portNames.Length; i++)
|
|
{
|
|
portTable.Rows.Add(portNames[i]);
|
|
}
|
|
|
|
portComboBox.DataSource = portTable.DefaultView;
|
|
portComboBox.ValueMember = "VALUE";
|
|
}
|
|
|
|
private void PortOpen()
|
|
{
|
|
if (!serialPort.IsOpen)
|
|
serialPort.Open();
|
|
|
|
Print(string.Format("\t[CMD] {0} {1}", DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss"), portName + "포트 연결"));
|
|
}
|
|
|
|
private void PortClose()
|
|
{
|
|
if (serialPort.IsOpen)
|
|
serialPort.Close();
|
|
|
|
Print(string.Format("\t[CMD] {0} {1}", DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss"), portName + "포트 해제"));
|
|
}
|
|
|
|
private void Connect_Click(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
if (string.IsNullOrEmpty(portName))
|
|
{
|
|
MessageBox.Show("포트를 선택해주십시오.");
|
|
return;
|
|
}
|
|
|
|
if (Connect.Text == "연결")
|
|
{
|
|
PortOpen();
|
|
Connect.Text = "해제";
|
|
}
|
|
else
|
|
{
|
|
PortClose();
|
|
Connect.Text = "연결";
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
}
|
|
}
|
|
|
|
private void Print(string message)
|
|
{
|
|
printText.AppendText(message + Environment.NewLine);
|
|
}
|
|
|
|
private void serialPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
|
|
{
|
|
try
|
|
{
|
|
lock(receiveLock)
|
|
{
|
|
this.Invoke(new EventHandler(DataReceived));
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(ex.Message);
|
|
}
|
|
}
|
|
|
|
private void DataReceived(object sender, EventArgs e)
|
|
{
|
|
string data = serialPort.ReadExisting();
|
|
if (data.Length < 1)
|
|
return;
|
|
|
|
serialStream = String.Concat(serialStream, data);
|
|
string packetString = pullPacketString();
|
|
Console.WriteLine();
|
|
EqTocPrint(packetString);
|
|
}
|
|
|
|
private string pullPacketString()
|
|
{
|
|
int etxIndex = serialStream.IndexOf(SEPERATOR);
|
|
if (etxIndex < 0)
|
|
return null;
|
|
|
|
string packetString = serialStream.Substring(0, etxIndex);
|
|
serialStream = serialStream.Remove(0, etxIndex + SEPERATOR.Length);
|
|
|
|
return packetString;
|
|
}
|
|
|
|
private void EqTocPrint(string message)
|
|
{
|
|
if (string.IsNullOrEmpty(message))
|
|
return;
|
|
|
|
if (commandText.Text.ToUpper() != "RD")
|
|
{
|
|
Print(message);
|
|
return;
|
|
}
|
|
|
|
string[] tmp = message.Split(' ');
|
|
if (tmp.Length < 4)
|
|
{
|
|
Print(message);
|
|
return;
|
|
}
|
|
|
|
List<string> values = new List<string>();
|
|
for (int i = 0; i < tmp.Length; i++)
|
|
{
|
|
if (string.IsNullOrEmpty(tmp[i]))
|
|
continue;
|
|
|
|
values.Add(tmp[i]);
|
|
}
|
|
|
|
// 시간 "yyyy-MM-dd HH:mm:ss"
|
|
DateTime now = DateTime.Now;
|
|
string timeString = string.Format("{0}.{1}.{2} ", now.Year, now.Month.ToString("D2"), now.Day.ToString("D2")) + values[0];
|
|
DateTime timeStamp = DateTime.ParseExact(timeString, "yyyy.MM.dd HH:mm:ss", null);
|
|
|
|
// TOC
|
|
double toc = double.Parse(values[1]);
|
|
|
|
// 전기저항값
|
|
double res = double.Parse(values[2]);
|
|
|
|
// 온도
|
|
double temp = double.Parse(values[3]);
|
|
|
|
string formatted = string.Format(@"시간: {0}
|
|
TOC: {1} ppb
|
|
저항: {2} ㏁·cm
|
|
온도: {3} ℃", timeStamp.ToString(), toc, res, temp);
|
|
|
|
Print(formatted);
|
|
}
|
|
|
|
private void portComboBox_SelectedIndexChanged(object sender, EventArgs e)
|
|
{
|
|
DataRowView rowView = portComboBox.SelectedItem as DataRowView;
|
|
if (rowView == null)
|
|
return;
|
|
|
|
portName = rowView.Row["VALUE"].ToString();
|
|
serialPort.PortName = portName;
|
|
}
|
|
|
|
private void Send_Click(object sender, EventArgs e)
|
|
{
|
|
string command = commandText.Text + "\r\n";
|
|
if (!serialPort.IsOpen)
|
|
{
|
|
MessageBox.Show("포트가 열려있지 않습니다.");
|
|
return;
|
|
}
|
|
|
|
Print(string.Format("\t[CMD] {0} {1}", DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss"), commandText.Text));
|
|
|
|
serialPort.Write(command);
|
|
}
|
|
|
|
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
|
|
{
|
|
if (serialPort.IsOpen)
|
|
serialPort.Close();
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
|