C#将字节写入串口,如失败

时间:2017-03-24 08:43:30

标签: c# serial-port

为什么当我在自己的程序上使用时,从另一个程序输出的字节就像没有发生一样?数据来自示例程序: -

enter image description here

当我在程序中使用A5 00 01 02 13 02 01 55时,示例程序可以在设备上触发灯光时没有任何反应。

我的代码: -

using System;
using System.Text;
using ComTypes = System.Runtime.InteropServices.ComTypes;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.IO.Ports;
using COMLib;
using System.Linq;

namespace PortLPT
{
    class Program
    {
        static SerialPort twrport = new SerialPort();
        static string InputData = String.Empty;
        static string comname;
        [STAThread]
        static void Main(string[] args)
        {
            COMLibrary comport = new COMLibrary();
            var comdevice = comport.GetAllCOMPorts();
            foreach(var devinfo in comdevice)
            {
                Console.WriteLine($"Port Number: {devinfo.name}.\nPort Description: {devinfo.decsription}.\n{new string('=',"Port Description".Length + devinfo.decsription.Length + 3)}");
                if (devinfo.decsription.Contains("MOXA"))
                    comname = devinfo.name;
            }

            twrport.PortName = comname;
            twrport.BaudRate = 9600;
            twrport.DataBits = 8;
            twrport.StopBits = StopBits.One;
            twrport.Parity = Parity.None;
            twrport.Open();
            twrport.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
            twrport.RtsEnable = true;
            twrport.DtrEnable = true;   


            if (!(twrport.IsOpen))
            {
                try
                {
                    twrport.Open();
                    Console.WriteLine("Serial port connection successfully open.");
                }
                catch (UnauthorizedAccessException ex)
                {
                    Console.WriteLine($"Exception on opening serial port connection. Ex-{ex.Message}");
                }
            }
            else
            {
                Console.WriteLine("Serial port connection successfully open.");
            }

            try
            {
                string str = "A5 00 01 02 13 02 01 55";
                byte[] bytes = str.Split(' ').Select(s => Convert.ToByte(s, 16)).ToArray();

                byte[] buffer = new byte[8] { 0xA5, 0x00, 0x01, 0x02, 0x13, 0x02, 0x01, 0x55 };
                if (!(twrport.IsOpen))
                    twrport.Open();
                twrport.Write(bytes, 0, bytes.Length);
                twrport.Close();
                Console.WriteLine($"Write to port: {PrintBytes(bytes)}");
            }
            catch(Exception ex)
            { Console.WriteLine($"Error write to port. Ex-{ex.Message.ToString()}"); twrport.Close(); }



            Console.ReadLine();
        }
        static void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            // Show all the incoming data in the port's buffer
            Console.WriteLine(twrport.ReadExisting());
        }
        static string PrintBytes(byte[] byteArray)
        {
            var sb = new StringBuilder("{");
            for (var i = 0; i < byteArray.Length; i++)
            {
                var b = byteArray[i];
                sb.Append(b);
                if (i < byteArray.Length - 1)
                {
                    sb.Append(", ");
                }
            }
            sb.Append("}");
            return sb.ToString();
        }
    }
}

我尝试了两种字节样式,即变量bytes[]buffer[]。两者都不成功。如何相应地写入串口?

我对此并不那么专业。我已尝试从搜索引擎结果中写入串口链接,仍然无法触发设备

更新

enter image description here

0 个答案:

没有答案