Unity5.6.1 串口通讯亲测可行解决方案

使用System.IO.Ports命名空间

此版本的Unity只能使用.net2.0, 但是需要在playersetting 里面把 .net适应调整到.net2.0即可以使用该命名空间。

简单的串口类

using System.IO.Ports;
using System.Threading;
using System.Collections.Generic;
using System;
/// summary
/// 串口通信
///summary
public class SerialPortItem
{
    // 开启串口参数
    public class SerialPortParam
    {
        public string PortName;
        public int BaudRate;
        public Parity Parity;
        public StopBits StopBits;
        public int DataBits;
        public Handshake Handshake;
        public bool RtsEnable;
    }
    public static byte[] outSensorPara;
    private SerialPort serialPort;
    private bool isClose;
    private Thread serialPortThread;//收发串口线程
    //读取数据缓存
    private byte[] readDataBuffer = new byte[1024];
    private int readDataLen;//读取到数据长度
                            //写数据缓存
    private List<byte> writeDataBuffer = new List<byte>();

    private bool isOpen;

    // 是否开启端口
    public bool IsOpen
    {
        get { return isOpen; }
    }
    #region 事件

    // 接到数据(子线程)

    public Action<byte[]> OnReceiveData;

    // 错误(子线程)

    public Action<string> OnError;
    #endregion

    // 开启端口

    public void Open(SerialPortParam param)
    {
        serialPort = new SerialPort(param.PortName);
        serialPort.BaudRate = param.BaudRate;
        serialPort.Parity = param.Parity;
        serialPort.StopBits = param.StopBits;
        serialPort.DataBits = param.DataBits;
        serialPort.Handshake = param.Handshake;
        serialPort.RtsEnable = param.RtsEnable;
        serialPort.ReadTimeout = 1;
        serialPort.WriteTimeout = 1;
        try
        {
            serialPort.Open();
        }
        catch (Exception ex)
        {
            if (OnError != null)
                OnError(ex.ToString());
            Close();
        }
        isOpen = true;
        isClose = false;
        serialPortThread = new Thread(new ParameterizedThreadStart(work));
        serialPortThread.Start(serialPort);
    }

    // 发送数据

    public void Send(byte[] bytes)
    {
        if (serialPort != null
            && serialPort.IsOpen)
        {
            lock (writeDataBuffer)
            {
                writeDataBuffer.AddRange(bytes);
            }
        }
    }
    public void Close()
    {
        isOpen = false;
        isClose = true;
        serialPort = null;
    }
    private void work(object obj)
    {
        SerialPort sp = (SerialPort)obj;
        while (true)
        {
            if (isClose)
                break;
            if (!sp.IsOpen)
            {
                break;
            }
            //读取
            try
            {
                readDataLen = sp.Read(readDataBuffer, 0, readDataBuffer.Length);
                if (readDataLen>0)
                {
                    byte[] receiveBytes = new byte[readDataLen];
                    Array.Copy(readDataBuffer, 0, receiveBytes, 0, receiveBytes.Length);
                    outSensorPara = receiveBytes;
                    if (OnReceiveData != null)
                        OnReceiveData(receiveBytes);
                }
            }
            catch (TimeoutException)
            {//正常结束
            }
            catch (Exception ex)
            {
                sp.Close();
                if (OnError != null)
                    OnError(ex.ToString());
            }
            //写入
            if (writeDataBuffer.Count> 0)
            {
                byte[] writeBytes = null;
                lock (writeDataBuffer)
                {
                    writeBytes = writeDataBuffer.ToArray();
                    writeDataBuffer.Clear();
                }
                try
                {
                    sp.Write(writeBytes, 0, writeBytes.Length);
                }
                catch (TimeoutException)
                {//正常结束
                }
                catch (Exception ex)
                {
                    sp.Close();
                    if (OnError != null)
                        OnError(ex.ToString());
                }
            }
        }
        sp.Close();
    }
}

查找串口名称

打开串口需要知道串口名称 ,例如对于USB转串口的设备,在设备管理器中显示的名为USB Serial Port(COMX)
下面是一个根据名称枚举串口设备的类及方法

 public class SetupDiWrap
    {
        public static string ComPortNameFromFriendlyNamePrefix(string friendlyNamePrefix)
        {
            const string className = "Ports";
            Guid[] guids = GetClassGUIDs(className);

            System.Text.RegularExpressions.Regex friendlyNameToComPort =
                new System.Text.RegularExpressions.Regex(@".? \((COM\d+)\)$");  // "..... (COMxxx)" -> COMxxxx
            foreach (Guid guid in guids)
            {
                // We start at the "root" of the device tree and look for all
                // devices that match the interface GUID of a disk
                Guid guidClone = guid;
                Console.WriteLine(guidClone);
                IntPtr h = SetupDiGetClassDevs(ref guidClone, IntPtr.Zero, IntPtr.Zero, DIGCF_PRESENT | DIGCF_PROFILE);
                Console.WriteLine(h + "  h");
                if (h.ToInt64() != INVALID_HANDLE_VALUE)
                {
                    int nDevice = 0;
                    while (true)
                    {
                        SP_DEVINFO_DATA da = new SP_DEVINFO_DATA();
                        da.cbSize = (uint)Marshal.SizeOf(da);
                        if (0 == SetupDiEnumDeviceInfo(h, nDevice++, ref da))
                        {
                            Console.WriteLine("error");
                            break;
                        }

                        uint RegType;
                        byte[] ptrBuf = new byte[BUFFER_SIZE];
                        uint RequiredSize;
                        if (SetupDiGetDeviceRegistryProperty(h, ref da,
                            (uint)SPDRP.FRIENDLYNAME, out RegType, ptrBuf,
                            BUFFER_SIZE, out RequiredSize))
                        {
                            const int utf16terminatorSize_bytes = 2;
                            string friendlyName = System.Text.UnicodeEncoding.Unicode.GetString(ptrBuf, 0, (int)RequiredSize - utf16terminatorSize_bytes);

                            Console.WriteLine(friendlyName);
                            if (!friendlyName.StartsWith(friendlyNamePrefix))
                                continue;

                            if (!friendlyNameToComPort.IsMatch(friendlyName))
                                continue;

                            return friendlyNameToComPort.Match(friendlyName).Groups[1].Value;
                        }
                    } // devices
                    //SetupDiDestroyDeviceInfoList(h);
                }
            } // class guids

            return null;
        }

        /// <summary>
        /// The SP_DEVINFO_DATA structure defines a device instance that is a member of a device information set.
        /// </summary>
        [StructLayout(LayoutKind.Sequential)]
        private struct SP_DEVINFO_DATA
        {
            /// <summary>Size of the structure, in bytes.</summary>
            public uint cbSize;
            /// <summary>GUID of the device interface class.</summary>
            public Guid ClassGuid;
            /// <summary>Handle to this device instance.</summary>
            public uint DevInst;
            /// <summary>Reserved; do not use.</summary>
            public uint Reserved;
        }

        const int BUFFER_SIZE = 1024;


        private enum SPDRP
        {
            FRIENDLYNAME = 0x0000000C,
        }

        [DllImport("setupapi.dll", SetLastError = true)]
        static extern bool SetupDiClassGuidsFromName(string ClassName,
                                                     ref Guid ClassGuidArray1stItem, UInt32 ClassGuidArraySize,
                                                     out UInt32 RequiredSize);

        [DllImport("setupapi.dll")]
        private static extern Int32 SetupDiEnumDeviceInfo(IntPtr DeviceInfoSet,
                                                          Int32 MemberIndex, ref SP_DEVINFO_DATA DeviceInterfaceData);


        //p
        [DllImport("setupapi.dll", CharSet = CharSet.Auto)]
        static extern IntPtr SetupDiGetClassDevs(           // 1st form using a ClassGUID only, with null Enumerator
            ref Guid ClassGuid,
            IntPtr Enumerator,
            IntPtr hwndParent,
            int Flags
        );

        /// <summary>
        /// The SetupDiGetDeviceRegistryProperty function retrieves the specified device property.
        /// This handle is typically returned by the SetupDiGetClassDevs or SetupDiGetClassDevsEx function.
        /// </summary>
        /// <param Name="DeviceInfoSet">Handle to the device information set that contains the interface and its underlying device.</param>
        /// <param Name="DeviceInfoData">Pointer to an SP_DEVINFO_DATA structure that defines the device instance.</param>
        /// <param Name="Property">Device property to be retrieved. SEE MSDN</param>
        /// <param Name="PropertyRegDataType">Pointer to a variable that receives the registry data Type. This parameter can be NULL.</param>
        /// <param Name="PropertyBuffer">Pointer to a buffer that receives the requested device property.</param>
        /// <param Name="PropertyBufferSize">Size of the buffer, in bytes.</param>
        /// <param Name="RequiredSize">Pointer to a variable that receives the required buffer size, in bytes. This parameter can be NULL.</param>
        /// <returns>If the function succeeds, the return value is nonzero.</returns>
        [DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern bool SetupDiGetDeviceRegistryProperty(
            IntPtr DeviceInfoSet,
            ref SP_DEVINFO_DATA DeviceInfoData,
            uint Property,
            out UInt32 PropertyRegDataType,
            byte[] PropertyBuffer,
            uint PropertyBufferSize,
            out UInt32 RequiredSize);


        const int DIGCF_DEFAULT = 0x1;
        const int DIGCF_PRESENT = 0x2;
        const int DIGCF_ALLCLASSES = 0x4;
        const int DIGCF_PROFILE = 0x8;
        const int DIGCF_DEVICEINTERFACE = 0x10;
        const int INVALID_HANDLE_VALUE = -1;

        private static Guid[] GetClassGUIDs(string className)
        {
            UInt32 requiredSize = 0;
            Guid[] guidArray = new Guid[1];

            bool status = SetupDiClassGuidsFromName(className, ref guidArray[0], 1, out requiredSize);
            if (true == status)
            {
                if (1 < requiredSize)
                {
                    guidArray = new Guid[requiredSize];
                    SetupDiClassGuidsFromName(className, ref guidArray[0], requiredSize, out requiredSize);
                }
            }
            else
                throw new System.ComponentModel.Win32Exception();

            return guidArray;
        }
    }

需要引用系统dll,但是该方法直接在Unity playmode下无法实现,经过多次测试发现,使用x86架构Build出来的exe程序可以实现该方法。不完美的解决了这个问题。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,287评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,346评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,277评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,132评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,147评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,106评论 1 295
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,019评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,862评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,301评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,521评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,682评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,405评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,996评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,651评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,803评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,674评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,563评论 2 352

推荐阅读更多精彩内容