界面主要是点击按钮实现控制器输入变化的监听
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;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace RS232Demo
{
public partial class Form1 : Form
{
SerialPort port = new SerialPort();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Thread commTh = new Thread(RunCommon);
commTh.Start();
}
private void RunCommon() {
RS232Control rs232 = new RS232Control();
rs232.Open();
while (true)
{
rs232.WriteData();
Thread.Sleep(50);
}
}
}
}
RS232工具类
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace RS232Demo
{
class RS232Control
{
SerialPort _serialPort = new SerialPort();
public void Open() {
if (_serial