소스파일입니다. 한번 더 좋은 Code를 연구해 보세요
//
// 제작자 : 신 옥 진
//
// 재배포는 금지, 그냥 링크를 사용 하세요. //
//
// 영리목적( 유료 강의 자료, 판매 시스템에 포함 등 )의 사용은 사전 허락을 받아야 합니다.
//
using System;
using System.Drawing;
using System.IO.Ports;
using System.Text;
using System.Windows.Forms; /// 연구중 ///
namespace SeriaPort
{
public partial class Form1 : Form
{
static string buff = "";
// Form 관련
public Form1() { InitializeComponent(); }//func
private void Form1_Load(object sender, EventArgs e)
{
comboSpeed.SelectedIndex = 0;
serialPort.BaudRate = 9600;
timer1.Interval = 300; // 초기 타이머 간격
txtInput.Focus();
}//func
private void Form1_Resize(object sender, EventArgs e) // 폼크기조절 Event
{
if (Width < 650) Width = 650; // 최소사이즈 유지
if (Height < 200) Height = 200;
Invalidate();
}//func
// Button 관련
private void btConnect_Click(object sender, EventArgs e) //연결버튼
{
if (comboPort.Text == "") return;
try
{
if (serialPort.IsOpen)
{
serialPort.Close();
}
else
{ // 그대로 사용하시면 될듯
serialPort.PortName = comboPort.SelectedItem.ToString();
serialPort.DataBits = 8;
serialPort.StopBits = StopBits.One;
serialPort.Parity = Parity.None;
serialPort.Encoding = Encoding.UTF8; //한글코드방식
serialPort.Open();
}
}
catch (Exception)
{
MessageBox.Show("연결안됨", "ComPort", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
btConnect.Text = serialPort.IsOpen ? "연결해제" : "연결하기"; // 연결버튼 Text 변경
comboPort.Enabled = !serialPort.IsOpen;
comboSpeed.Enabled = !serialPort.IsOpen;
menuATtop.Enabled = serialPort.IsOpen;
btSend.Enabled = serialPort.IsOpen;
txtInput.Enabled = serialPort.IsOpen;
txtInput.Text = (serialPort.IsOpen) ? "" : "포트와 전송속도 선택후 <연결하기>버튼을 눌러 주세요.";
}//func
private void btSend_Click(object sender, EventArgs e) // 전송버튼
{
if (menuEndLine.Text == "Both CR & NL")
serialPort.WriteLine(txtInput.Text);
else serialPort.Write(txtInput.Text);
txtOut.SelectionColor = Color.Yellow;
txtOut.AppendText(txtInput.Text + "\r\n");
txtInput.Text = "";
txtOut.SelectionColor = Color.White;
txtOut.ScrollToCaret();
}//func
private void btClear_Click(object sender, EventArgs e) // Clear버튼
{
txtOut.Clear();
}//func
/// 메뉴 관련
private void menuExit_Click(object sender, EventArgs e)
{
Application.Exit();
}//func
private void menuAbout_Click(object sender, EventArgs e)
{
Form frm = new AboutBox1();
frm.ShowDialog();
}//func
private void menuEndNo_Click(object sender, EventArgs e)
{
menuEndLine.Text = "No Line End";
}//func
private void menuEndCrNl_Click(object sender, EventArgs e)
{
menuEndLine.Text = "Both CR & NL";
}//func
private void menuAT_Click(object sender, EventArgs e)
{
txtInput.Text = "AT";
if (menuEndLine.Text == "Both CR & NL")
serialPort.WriteLine(txtInput.Text);
else serialPort.Write(txtInput.Text);
txtOut.AppendText(txtInput.Text + "\r\n");
txtInput.Text = "";
}//func
private void menuAT_Version_Click(object sender, EventArgs e)
{
txtInput.Text = "AT+VERSION";
if (menuEndLine.Text == "Both CR & NL")
serialPort.WriteLine(txtInput.Text);
else serialPort.Write(txtInput.Text);
txtOut.AppendText(txtInput.Text + "\r\n");
txtInput.Text = "";
}//func
private void menuAT_Name_Click(object sender, EventArgs e)
{
txtInput.Text = "AT+NAME";
}//func
private void menuAT_Pin_Click(object sender, EventArgs e)
{
txtInput.Text = "AT+PIN";
}//func
private void menuAtNameQ_Click(object sender, EventArgs e)
{
txtInput.Text = "AT+NAME?";
if (menuEndLine.Text == "Both CR & NL")
serialPort.WriteLine(txtInput.Text);
else serialPort.Write(txtInput.Text);
txtOut.AppendText(txtInput.Text + "\r\n");
txtInput.Text = "";
}//func
private void menuAtBaud_Click(object sender, EventArgs e)
{
txtInput.Text = "AT+BAUD";
}//func
private void menuAtPinQ_Click(object sender, EventArgs e)
{
txtInput.Text = "AT+PIN?";
if (menuEndLine.Text == "Both CR & NL")
serialPort.WriteLine(txtInput.Text);
else serialPort.Write(txtInput.Text);
txtOut.AppendText(txtInput.Text + "\r\n");
txtInput.Text = "";
}//func
private void menuAtBaudQ_Click(object sender, EventArgs e)
{
txtInput.Text = "AT+BAUD?";
if (menuEndLine.Text == "Both CR & NL")
serialPort.WriteLine(txtInput.Text);
else serialPort.Write(txtInput.Text);
txtOut.AppendText(txtInput.Text + "\r\n");
txtInput.Text = "";
}//func
private void menuMsec1000_Click(object sender, EventArgs e)
{
timer1.Interval = 1000;
menuSpeed.Text = "Delay설정 : 1초";
}//func
private void menuMsec500_Click(object sender, EventArgs e)
{
timer1.Interval = 500;
menuSpeed.Text = "Delay설정 : 0.5초";
}//func
private void menuMsec300_Click(object sender, EventArgs e)
{
timer1.Interval = 300;
menuSpeed.Text = "Delay설정 : 0.3초";
}//func
private void menuMsec100_Click(object sender, EventArgs e)
{
timer1.Interval = 100;
menuSpeed.Text = "Delay설정 : 0.1초";
}//func
private void menuNoUse_Click(object sender, EventArgs e)
{
timer1.Interval = Int32.MaxValue; // timer1.stop()을 사용하면 timer1.start()가 작동 안함
// (이유 아직 모름)
// 그래서 타이머간격 최대치로 설정 ???( 혹시 이유 아는 분 )
menuSpeed.Text = "Delay설정 : 안됨 ( 수신문장이 조각 날수도.. )";
}//func
// ComboBox관련
private void comboPort_Click(object sender, EventArgs e)
{
comboPort.Items.Clear();
foreach (var items in SerialPort.GetPortNames())
comboPort.Items.Add(items);
}//func
private void comboSpeed_SelectedIndexChanged(object sender, EventArgs e)
{
serialPort.BaudRate = int.Parse(comboSpeed.SelectedItem.ToString());
}//func
// TextBox 관련
private void txtInput_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Return) // Enter키시 전송으로
{
if (menuEndLine.Text == "Both CR & NL")
serialPort.WriteLine(txtInput.Text);
else serialPort.Write(txtInput.Text);
txtOut.AppendText(txtInput.Text + "\r\n");
txtInput.Text = "";
txtOut.ScrollToCaret();
}
}//func
// ETC....
private void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
if (timer1.Interval != Int32.MaxValue) buff += serialPort.ReadExisting();
// buff에 한문장이 한번에 잘 안들어옴
// 그래서 타이머를 사용하여 일정시간 buff에 저장한 후
// 타이머 Event에서 한번에 출력
else
{ // 타이머 사용 안하고 바로 출력
txtOut.SelectionColor = Color.Yellow;
txtOut.AppendText(comboPort.Text + "> " + serialPort.ReadExisting() + "\r\n");
txtOut.SelectionColor = Color.White;
txtOut.ScrollToCaret();
}
}//func
private void timer1_Tick(object sender, EventArgs e)
{
btSend.Text = btSend.Text == " 전송 " ? btSend.Text = " 전송●" : btSend.Text = " 전송 ";
if (buff == "" || timer1.Interval == Int32.MaxValue) return; // 빈버퍼 또는 지연시간 사용 안하면 return
// 타이머 사용시 출력
txtOut.SelectionColor = Color.Yellow;
txtOut.AppendText(comboPort.Text + "> " + buff + "\r\n");
buff = "";
txtOut.SelectionColor = Color.White;
txtOut.ScrollToCaret(); // 현재위치로 스크롤
}//func
}//Form1
}// namespace
'프로젝트 > 시리얼 통신용 터미널 프로그램' 카테고리의 다른 글
시리얼 터미널 프로그램 ver 1.0 (0) | 2022.07.28 |
---|