using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
namespace RS232
{
public partial class Form1 : Form
{
delegate void SetTextEv(string data);
SetTextEv SetTextInvk;
SetTextEv SetTextInvk2;
public void SetText1(string data)
{
textBox1.Text += data;
}
public void SetText2(string data)
{
textBox2.Text += data;
}
public Form1()
{
InitializeComponent();
m_ErrMsg = "";
m_Sport = new SerialPort();
m_lock = new System.Object();
comboBox1.SelectedIndex = 0;
comboBox2.SelectedIndex = 0;
SetTextInvk = new SetTextEv(SetText1);
SetTextInvk2 = new SetTextEv(SetText2);
}
private void textBox1_TabStopChanged(object sender, EventArgs e)
{
TextBox txt = (TextBox)sender;
txt.Select(txt.Text.Length, 0);
txt.ScrollToCaret();
}
byte[] rdata = new byte[1024];
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
int nLen = serialPort1.Read(rdata, 0, rdata.Length);
string str = Encoding.Default.GetString(rdata, 0, nLen);
string str2 = "";
str2 += (rdata[0].ToString("X2") + " ");
for (int i = 1; i < nLen; i++)
{
str2 += (rdata[i].ToString("X2") + " ");
if (rdata[i - 1] == 0x0D && rdata[i] == 0x0A)
{
str2 += "\r\n";
}
}
textBox1.Invoke(SetTextInvk, str);
textBox2.Invoke(SetTextInvk2, str2);
}
private void simpleButton1_Click(object sender, EventArgs e)
{
serialPort1.PortName = comboBox1.SelectedItem.ToString();
serialPort1.BaudRate = Convert.ToInt32(comboBox2.SelectedItem.ToString());
if (!serialPort1.IsOpen)
serialPort1.Open();
button2.Enabled = true;
button1.Enabled = false;
}
private void button2_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen)
serialPort1.Close();
button2.Enabled = false;
button1.Enabled = true;
}
// Primitive
string m_ErrMsg;
string m_LineBuf;
System.Object m_lock;
// .Net
System.IO.Ports.SerialPort m_Sport;
static Form1 s_Instnace = null;
static readonly object padlock = new object();
public static Form1 Instance
{
get
{
if (s_Instnace == null)
{
lock (padlock)
{
s_Instnace = new Form1();
}
}
return s_Instnace;
}
}
public string ErrMsg
{
get { return m_ErrMsg; }
}
public System.Object Lock
{
get { return m_lock; }
}
public SerialPort SPort
{
get { return m_Sport; }
}
public bool open()
{
bool result = false;
try
{
m_Sport.PortName = "COM3";
m_Sport.BaudRate = 9800;
m_Sport.DataBits = 8;
m_Sport.Parity = Parity.None;
m_Sport.StopBits = StopBits.One;
m_Sport.ReadTimeout = 1000;
m_Sport.WriteTimeout = 1000;
m_Sport.Open();
m_Sport.DataReceived += new SerialDataReceivedEventHandler(serialDataRecvHandler);
if (!m_Sport.IsOpen)
{
throw new Exception("Serial port open fail");
}
result = true;
}
catch (Exception e)
{
m_ErrMsg = e.Message;
result = false;
}
return result;
}
public bool close()
{
bool result = false;
try
{
if (m_Sport.IsOpen)
{
m_Sport.Close();
}
if (!m_Sport.IsOpen)
{
result = true;
}
else
{
throw new Exception("Serial port close fail");
}
}
catch (Exception e)
{
m_ErrMsg = e.Message;
result = false;
}
return result;
}
public void serialDataRecvHandler(Object sender, SerialDataReceivedEventArgs e)
{
try
{
lock (Form1.Instance.Lock)
{
int readlen = Form1.Instance.SPort.ReadBufferSize;
byte[] bytebuf = new byte[readlen];
Form1.Instance.SPort.Read(bytebuf, 0, readlen);
m_LineBuf = System.Text.Encoding.Default.GetString(bytebuf);
m_LineBuf = m_LineBuf.Replace("\0", "");
}
}
catch (TimeoutException ex)
{
m_ErrMsg = ex.Message;
}
}
private void simpleButton3_Click(object sender, EventArgs e)
{
open();
}
}
}
'IT > Visual Studio' 카테고리의 다른 글
[.Net] C# DataTable 객체를 Database에 저장하기 (0) | 2020.11.11 |
---|---|
원문자 키 값 표 (0) | 2020.05.21 |
[C#] 인터넷 날짜와 시간 가져오기 (0) | 2019.03.26 |
엔티티프레임워크에서 linq 형 변환 방법 (0) | 2019.02.14 |
[C#] USB 통신 샘플 소스 (0) | 2018.07.26 |
JSon 문자열을 C# DataTable로 변환 코드 (0) | 2018.05.02 |
[VisualStudio] 사용자 지정 이벤트 로그를 만들 수 있는 권한 부여 (0) | 2018.04.10 |
[Visual Studio] 강력키 생성방법 (0) | 2018.04.07 |