将多个串行端口同时记录到多个文件中

本文关键字:文件 记录 串行端口 | 更新日期: 2023-09-27 18:20:50

我正在编写一个C#程序,该程序将同时从多个串行端口读取数据,并将该数据记录到文件中。每个端口都有自己的专用文件可写入。

我能够成功地用一个端口创建一个类来实现这一点,但是当我尝试用两个端口实现这一目标时,只有第二个打开的端口有任何数据写入文件。

下面是我为处理从串行端口接收的数据事件并写入文件而编写的类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO;
using System.IO.Ports;
namespace Shell
{
    public class Shell
    {
        private string port_name;
        static SerialPort port;
        static FileStream fs;
        static StreamWriter stream;
        /*------------------------------------------------------
        Constructors can be added as needed to take
        additional inputs for more flexibility.
        ------------------------------------------------------*/
        public Shell(string name)
        {
            port = new SerialPort();
            port_name = name;
            port.PortName = name;
            port.BaudRate = 115200;
            port.Parity = Parity.None;
            port.DataBits = 8;
            port.StopBits = StopBits.One;
            port.Handshake = Handshake.None;
            port.Encoding = Encoding.UTF8;
        }
        /*------------------------------------------------------
        Close the serial port and file.
        ------------------------------------------------------*/
        public void close()
        {
            if (port.IsOpen)
            {
                port.DataReceived -= port_DataReceived;
                port.Close();
                stream.Dispose();
                fs.Close();
            }
        }
        /*------------------------------------------------------
        Add the serial data received handler, open the file to
        log to, and open the serial port.
        ------------------------------------------------------*/
        public void open(string path)
        {
            if (!port.IsOpen)
            {
                fs = File.Open(path, FileMode.Create, FileAccess.ReadWrite);
                stream = new StreamWriter(fs);
                stream.AutoFlush = true;
                stream.WriteLine("Start");
                port.DataReceived += port_DataReceived;
                port.Open();
            }
        }
        /*------------------------------------------------------
        Handler for the serial data events.
        ------------------------------------------------------*/
        void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            string data = port.ReadExisting();
            if (!string.IsNullOrEmpty(data))
            {
                    stream.Write(data);
            }
        }
    }
}

这是我用来打开和关闭连接的简单表单中的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace shell_log_test
{
    public partial class Form1 : Form
    {
        private Shell.Shell port1;
        private Shell.Shell port2;
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            port2 = new Shell.Shell("COM5");
            port2.open("port2.log");
            port1 = new Shell.Shell("COM12");
            port1.open("port1.log");
        }
        private void button2_Click(object sender, EventArgs e)
        {
            port1.close();
            port2.close();
        }
    }
}

提前感谢您的帮助。

将多个串行端口同时记录到多个文件中

更改以下内容:

static SerialPort port;
static FileStream fs;
static StreamWriter stream;

至:

private SerialPort port;
private FileStream fs;
private StreamWriter stream;