|
|
|
|
using SerialCommApp.Dialogs;
|
|
|
|
|
using SerialCommApp.SeriallLibs;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Data;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.IO.Ports;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
|
|
|
|
namespace SerialCommApp
|
|
|
|
|
{
|
|
|
|
|
public enum MessageType
|
|
|
|
|
{
|
|
|
|
|
SYS,
|
|
|
|
|
SND,
|
|
|
|
|
RCV,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public partial class SerialCommApp : Form
|
|
|
|
|
{
|
|
|
|
|
private PSerialComm _serial;
|
|
|
|
|
private PSerialCommSetting _serialSetting;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public SerialCommApp()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
InitInstance();
|
|
|
|
|
|
|
|
|
|
UIEnable(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void InitInstance()
|
|
|
|
|
{
|
|
|
|
|
_serial = new PSerialComm();
|
|
|
|
|
_serialSetting = new PSerialCommSetting();
|
|
|
|
|
|
|
|
|
|
_serial.OnDataReceived += _serial_OnDataReceived;
|
|
|
|
|
|
|
|
|
|
GetPortNames();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void GetPortNames()
|
|
|
|
|
{
|
|
|
|
|
DataTable portTable = new DataTable();
|
|
|
|
|
portTable.Columns.Add("VALUE", typeof(string));
|
|
|
|
|
|
|
|
|
|
string[] portNames = PSerialComm.GetPortNames();
|
|
|
|
|
if (portNames == null || portNames.Length < 1)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
foreach (string port in portNames)
|
|
|
|
|
{
|
|
|
|
|
portTable.Rows.Add(port);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
portComboBox.DataSource = portTable.DefaultView;
|
|
|
|
|
portComboBox.ValueMember = "VALUE";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UIEnable(bool isConnect)
|
|
|
|
|
{
|
|
|
|
|
portComboBox.Enabled = !isConnect;
|
|
|
|
|
portRefreshButton.Enabled = !isConnect;
|
|
|
|
|
settingButton.Enabled = !isConnect;
|
|
|
|
|
|
|
|
|
|
sendButton.Enabled = isConnect;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void PortOpen()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
DataRowView rowView = portComboBox.SelectedItem as DataRowView;
|
|
|
|
|
if (rowView == null)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Please check the selected port.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_serial.PortName = rowView.Row["VALUE"].ToString();
|
|
|
|
|
_serial.Open();
|
|
|
|
|
|
|
|
|
|
UIEnable(true);
|
|
|
|
|
connectButton.Text = "Disconnect";
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show(ex.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void PortClose()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (_serial.IsOpen)
|
|
|
|
|
_serial.Close();
|
|
|
|
|
|
|
|
|
|
UIEnable(false);
|
|
|
|
|
connectButton.Text = "Connect";
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show(ex.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void portRefreshButton_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
GetPortNames();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void settingButton_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
PSerialCommSetting setting = (PSerialCommSetting)_serialSetting.Clone();
|
|
|
|
|
CommSettingDialog dlg = new CommSettingDialog(setting);
|
|
|
|
|
if (dlg.ShowDialog() != DialogResult.OK)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
_serialSetting = dlg.Setting;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void WriteLine(string message, MessageType type)
|
|
|
|
|
{
|
|
|
|
|
string text = $"[{DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss")} : {type.ToString()}] {message}";
|
|
|
|
|
printText.AppendText(text + Environment.NewLine);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void _serial_OnDataReceived(object sender, string e)
|
|
|
|
|
{
|
|
|
|
|
WriteLine(e, MessageType.RCV);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void connectButton_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (portComboBox.SelectedIndex < 0)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Please select port.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (connectButton.Text == "Connect")
|
|
|
|
|
PortOpen();
|
|
|
|
|
else
|
|
|
|
|
PortClose();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show(ex.Message);
|
|
|
|
|
UIEnable(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void sendButton_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
string command = commandText.Text + "\r\n";
|
|
|
|
|
|
|
|
|
|
if (!_serial.IsOpen)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("포트가 열려있지 않습니다.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
WriteLine(commandText.Text, MessageType.SND);
|
|
|
|
|
|
|
|
|
|
_serial.Write(command);
|
|
|
|
|
|
|
|
|
|
commandText.Clear();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|