[英]C# Com port close
I need to read data from COM
in Form1
and then In Form2
again too. 我需要从
Form1
COM
读取数据,然后再次在Form2
读取数据。
What will be the best way? 什么是最好的方式?
In Form1
read the COM
data, then mySerialPort.Close();
在
Form1
读取COM
数据,然后mySerialPort.Close();
, and in Form2
open new connection? ,并在
Form2
打开新连接?
If like this, where in my code above should I close
it? 如果像这样,我上面的代码应该
close
它吗?
Or don't close the COM
? 或者不要关闭
COM
? If don't close, how can I read the data in Form2
? 如果不关闭,我如何读取
Form2
的数据?
namespace portreader
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
SerialPort mySerialPort = new SerialPort();
mySerialPort.PortName = "COM3";
mySerialPort.BaudRate = 9600;
mySerialPort.Parity = Parity.None;
mySerialPort.StopBits = StopBits.One;
mySerialPort.DataBits = 8;
mySerialPort.Handshake = Handshake.None;
mySerialPort.DataReceived += new SerialDataReceivedEventHandler(mySerialPort_DataReceived);
mySerialPort.Open();
}
string _buffer;
private void mySerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
if (this.InvokeRequired)
{
// Using this.Invoke causes deadlock when closing serial port, and BeginInvoke is good practice anyway.
this.BeginInvoke(new EventHandler<SerialDataReceivedEventArgs>(mySerialPort_DataReceived), new object[] { sender, e });
return;
}
SerialPort sp = (SerialPort)sender;
string data = sp.ReadExisting();
_buffer += data;
if (_buffer.Length >= 8)
{
int chipnumber = Int32.Parse(_buffer, System.Globalization.NumberStyles.HexNumber);
Form2 form2 = new Form2(chipnumber);
form2.ShowDialog(this);
_buffer = null;
}
}
}
}
you need to make a class that both form1 and form2 can use, then give form1 and form2 a reference to that class. 你需要创建一个form1和form2都可以使用的类,然后给form1和form2一个对该类的引用。
Ideally, the forms don't have any idea there is a serial port at all. 理想情况下,表单根本不知道有串口。 They just use your class/service to ask questions and get answers, and encapsulated in that class is the logic for going to the serial port and doing the queries.
他们只是使用您的类/服务来提问并获得答案,并且封装在该类中是进入串口并进行查询的逻辑。
This is one of the basic design principles of software, separating your UI from your application logic. 这是软件的基本设计原则之一,将UI与应用程序逻辑分离。
see my answer on another related post 在另一篇相关文章中看到我的答案
You could simply wrap the ComPort
in a class like 您可以简单地将
ComPort
包装在类中
public class DataInput
{
public event Action<string> DataReceived;
private readonly SerialPort mySerialPort;
public DataInput(string portName)
{
mySerialPort = new SerialPort();
mySerialPort.PortName = portName;
mySerialPort.BaudRate = 9600;
mySerialPort.Parity = Parity.None;
mySerialPort.StopBits = StopBits.One;
mySerialPort.DataBits = 8;
mySerialPort.Handshake = Handshake.None;
mySerialPort.DataReceived += new SerialDataReceivedEventHandler(mySerialPort_DataReceived);
mySerialPort.Open();
}
private void mySerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
string received = mySerialPort.ReadExisting();
DataReceived?.Invoke(received);
}
}
Now every form (and also other components of your software) can suscribe the event DataReceived
of an instance of DataInput
. 现在,每个表单(以及您的软件的其他组件)都可以使用
DataInput
实例的事件DataReceived
。 So every component can handle the data the way it likes. 因此,每个组件都可以按照自己喜欢的方式处理数据。 Therefore you simply have to create an instance of
DataReceived
and use it in your forms like: 因此,您只需创建
DataReceived
的实例并在表单中使用它,如:
public partial class Form1 : Form
{
private readonly DataInput dataInput;
public Form1()
{
InitializeComponent();
dataInput = new DataInput("COM3");
dataInput.dataReceived += OnDataReceived;
}
private void OnDataReceived(string Data)
{
//Do whatever you want with the data
}
}
Now you can pass dataInput
to your second form and you are able to use the dataReceived
event again. 现在,您可以将
dataInput
传递给第二个表单,并且可以再次使用dataReceived
事件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.