저는 (주)디에이피에서 drill일만 해 보았어요..
그런데 유심히 보니..PC에 허브를 연결하여 설비쪽으로 시리얼 통신을 하는듯 싶더라구요..
그 전송시키는 프로그램을 현장에서는 DNC라고 부르더군요....
이걸 구현한 겁니다...
DNC고장으로 긴급상황 발생시를 대비해서 필요할수도 있겠군요....
using System;
using System.IO;
using System.IO.Ports;
using System.Threading;
using System.Windows.Forms;
namespace SerialFileSend
{
public partial class Form1 : Form
{
SerialPort serialPort = new SerialPort();
public Form1()
{
InitializeComponent();
// 시리얼 설정
serialPort.PortName = "COM5";
serialPort.BaudRate = 9600;
serialPort.DataBits = 8;
serialPort.Parity = Parity.None;
serialPort.StopBits = StopBits.One;
try
{
serialPort.Open();
MessageBox.Show("시리얼 포트 연결 완료");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btnSendFile_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Text File|*.txt";
if (ofd.ShowDialog() == DialogResult.OK)
{
try
{
// 메모장 파일 읽기
string[] lines = File.ReadAllLines(ofd.FileName);
foreach (string line in lines)
{
if (serialPort.IsOpen)
{
// 설비로 전송
serialPort.WriteLine(line);
// 화면 출력
textBox1.AppendText(
"[전송] " + line + "\r\n"
);
// 설비 처리 시간
Thread.Sleep(300);
}
}
MessageBox.Show("전송 완료");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (serialPort.IsOpen)
{
serialPort.Close();
}
}
}
}