可以使用C#自动检测和连接串口

时间:2015-10-01 08:17:09

标签: c# console-application

这是一个带有C#的示例控制台应用程序,可在网站上找到,但我无法自动检测并连接每个控制台应用程序1个端口。

任何解决想法的想法和建议?

plt.tight_layout()

1 个答案:

答案 0 :(得分:0)

这里我假设您将以相同的方式处理每个串行端口(因此,在控制台中键入的相同消息写入每个串行端口,并以与从每个端口接收的相同方式打印到控制台)所以它启动多个线程,一个用于List中保存的每个串口。

class Program
{
    static bool _continue;
    static List<SerialPort> _serialPorts = new List<SerialPort>();

    public static void Main()
    {
        string name;
        string message;
        StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;

        //Add here each port you need
        _serialPorts.Add(new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One){ReadTimeout = 500, WriteTimeout = 500, Handshake = Handshake.None});

        List<Thread> readThreads = new List<Thread>();

        _serialPorts.ForEach(port => { 
            port.Open();
            Thread readThread = new Thread(Read);
            readThread.Start(port);
            readThreads.Add(readThread); 
        });

        _continue = true;

        Console.Write("Name: ");
        name = Console.ReadLine();

        Console.WriteLine("Type QUIT to exit");

        while (_continue)
        {
            message = Console.ReadLine();

            if (stringComparer.Equals("quit", message))
            {
                _continue = false;
            }
            else
            {
                _serialPorts.ForEach(port => port.WriteLine(String.Format("<{0}>: {1}", name, message)));
            }
        }

        _serialPorts.ForEach(port => port.Close());
        readThreads.ForEach(readThread => readThread.Join());
    }

    public static void Read(Object threadObj)
    {
        SerialPort serialPort = (SerialPort) threadObj;
        while (_continue)
        {
            try
            {
                string message = serialPort.ReadLine();
                Console.WriteLine(message);
            }
            catch (TimeoutException) { }
        }
    }
}

如果您希望将其保留为需要单独启动的单独程序,则只需使用静态值替换SerialPort的创建:

class Program
{
    static bool _continue;

    //Edit here your parameters
    private static SerialPort _serialPort = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One) {ReadTimeout = 500, WriteTimeout = 500, Handshake = Handshake.None};

    public static void Main()
    {
        string name;
        string message;
        StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;

        Thread readThread = new Thread(Read);

        _serialPort.Open();
        readThread.Start();

        _continue = true;

        Console.Write("Name: ");
        name = Console.ReadLine();

        Console.WriteLine("Type QUIT to exit");

        while (_continue)
        {
            message = Console.ReadLine();

            if (stringComparer.Equals("quit", message))
            {
                _continue = false;
            }
            else
            {
                _serialPort.WriteLine(String.Format("<{0}>: {1}", name, message));
            }
        }

        _serialPort.Close();
        readThread.Join();
    }

    public static void Read()
    {
        while (_continue)
        {
            try
            {
                string message = _serialPort.ReadLine();
                Console.WriteLine(message);
            }
            catch (TimeoutException) { }
        }
    }
}