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

时间:2015-11-05 21:55:11

标签: c#

我正在编写一个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();
        }
    }
}

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

更改以下内容:

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

为:

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