示例#1
0
        public void SendButtonClick()
        {
            if (SpupObject == null)
            {
                return;
            }

            if (PanelViewed)
            {
                if (SerialPortInputField.text != string.Empty)
                {
                    //Write
                    switch (DataSendMode.value)
                    {
                    //NONE
                    default:
                    case 0: if (!SpupObject.Write(SerialPortInputField.text))
                        {
                            SpupObject.SerialDebugAddString(" [Send Error!]");
                        }
                        break;

                    //LF
                    case 1: if (!SpupObject.WriteLF(SerialPortInputField.text))
                        {
                            SpupObject.SerialDebugAddString(" [Send Error!]");
                        }
                        break;

                    //CR
                    case 2: if (!SpupObject.WriteCR(SerialPortInputField.text))
                        {
                            SpupObject.SerialDebugAddString(" [Send Error!]");
                        }
                        break;

                    //CRLF
                    case 3: if (!SpupObject.WriteCRLF(SerialPortInputField.text))
                        {
                            SpupObject.SerialDebugAddString(" [Send Error!]");
                        }
                        break;
                    }
                }
            }
        }
示例#2
0
        public override void OnInspectorGUI()
        {
            SerialPortUtilityPro obj = target as SerialPortUtilityPro;

            serializedObject.Update();

            //License

            if (_icon_free != null)
            {
                EditorGUILayout.BeginHorizontal();
                GUILayout.FlexibleSpace();
                GUILayout.Label(new GUIContent(_icon_free));
                GUILayout.FlexibleSpace();
                EditorGUILayout.EndHorizontal();
                GUI.backgroundColor = Color.red;
                if (GUILayout.Button("UNLOCK the 1MB transfer limited mode >>"))
                {
                    //SPAPLicense.AddSPAPLicense();
                    UnityEditorInternal.AssetStore.Open("content/125863");
                }
                GUI.backgroundColor = Color.white;
            }

            GUI.backgroundColor = new Color(0.50f, 0.70f, 1.0f);
            EditorGUILayout.Space();

            //button
            if (GUILayout.Button("SerialPort Configure", EditorStyles.toolbarButton))
            {
                ExpandConfigProperty.boolValue = !ExpandConfigProperty.boolValue;
            }

            GUI.backgroundColor = Color.white;

            if (ExpandConfigProperty.boolValue)
            {
                EditorGUILayout.BeginVertical(GUI.skin.box);
                EditorGUILayout.LabelField("SerialPort Status", EditorStyles.boldLabel);

                if (EditorApplication.isPlaying)
                {
                    if (obj.IsOpened())
                    {
                        EditorGUILayout.HelpBox("Device Opened.", MessageType.Info, true);
                        GUI.backgroundColor = Color.yellow;
                        if (GUILayout.Button("Close the device."))
                        {
                            obj.Close();
                        }
                        GUI.backgroundColor = Color.white;
                    }
                    else
                    {
                        if (obj.IsErrorFinished())
                        {
                            EditorGUILayout.HelpBox("Device Error Closed.", MessageType.Error, true);
                        }
                        else
                        {
                            EditorGUILayout.HelpBox("Device Closed.", MessageType.Warning, true);
                        }

                        GUI.backgroundColor = Color.yellow;
                        if (GUILayout.Button("Open the device."))
                        {
                            obj.Open();
                        }
                        GUI.backgroundColor = Color.white;
                    }
                }
                else
                {
                    string infoString = "Device is not running.";
                    EditorGUILayout.HelpBox(infoString, MessageType.Info, true);
                }

                EditorGUILayout.EndVertical();

                if (obj.IsOpened())
                {
                    EditorGUI.BeginDisabledGroup(true);
                }

                EditorGUILayout.BeginVertical(GUI.skin.box);
                EditorGUILayout.LabelField("Open Configure", EditorStyles.boldLabel);
                SerialPortUtilityPro.OpenSystem preMethod = obj.OpenMethod;
                obj.OpenMethod = (SerialPortUtilityPro.OpenSystem)EditorGUILayout.EnumPopup("Open Method", obj.OpenMethod);
                if (obj.OpenMethod != preMethod)
                {
                    obj.VendorID     = "";
                    obj.ProductID    = "";
                    obj.SerialNumber = "";
                    obj.Skip         = 0;
                }
                int intdata = 0;
                obj.IsAutoOpen = EditorGUILayout.Toggle("Auto Open", obj.IsAutoOpen);
                switch (obj.OpenMethod)
                {
                case SerialPortUtilityPro.OpenSystem.USB:
                    obj.VendorID  = EditorGUILayout.TextField("Vendor ID (0000-FFFF)", obj.VendorID);
                    obj.ProductID = EditorGUILayout.TextField("Product ID (0000-FFFF)", obj.ProductID);
                    if (!IsHexString(obj.VendorID))
                    {
                        obj.VendorID = "";
                    }
                    if (!IsHexString(obj.ProductID))
                    {
                        obj.ProductID = "";
                    }
                    obj.SerialNumber = EditorGUILayout.TextField("Serial Number", obj.SerialNumber);
                    EditorGUILayout.LabelField(" ", "* Empty item is targeted to all devices.");
                    obj.Skip = EditorGUILayout.IntField("Order (Default:0)", obj.Skip);
                    if (obj.Skip < 0)
                    {
                        obj.Skip = 0;
                    }

                    GUI.backgroundColor = Color.yellow;
                    GUILayout.BeginHorizontal();
                    GUILayout.FlexibleSpace();
                    if (GUILayout.Button("Show the devices connected to this PC.", GUILayout.Width(300)))
                    {
                        SPAPTools window = (SPAPTools)EditorWindow.GetWindow(typeof(SPAPTools), true, "Show the devices connected to this PC.", true);
                        window.spapObject = obj;
                        window.Show();
                    }
                    GUILayout.EndHorizontal();
                    break;

                case SerialPortUtilityPro.OpenSystem.PCI:
#if UNITY_STANDALONE_WIN || UNITY_STANDALONE_LINUX
                    obj.VendorID  = EditorGUILayout.TextField("Vendor ID (0000-FFFF)", obj.VendorID);
                    obj.ProductID = EditorGUILayout.TextField("Device ID (0000-FFFF)", obj.ProductID);
                    if (!IsHexString(obj.VendorID))
                    {
                        obj.VendorID = "";
                    }
                    if (!IsHexString(obj.ProductID))
                    {
                        obj.ProductID = "";
                    }
                    EditorGUILayout.LabelField(" ", "* Empty item is targeted to all devices.");
                    obj.Skip = EditorGUILayout.IntField("Order (Default:0)", obj.Skip);
                    if (obj.Skip < 0)
                    {
                        obj.Skip = 0;
                    }

                    GUI.backgroundColor = Color.yellow;
                    GUILayout.BeginHorizontal();
                    GUILayout.FlexibleSpace();
                    if (GUILayout.Button("Show the devices connected to this PC.", GUILayout.Width(300)))
                    {
                        SPAPTools window = (SPAPTools)EditorWindow.GetWindow(typeof(SPAPTools), true, "Show the devices connected to this PC.", true);
                        window.spapObject = obj;
                        window.Show();
                    }
                    GUILayout.EndHorizontal();
#else
                    GUI.backgroundColor = Color.white;
                    EditorGUILayout.HelpBox("This item is not supported on the platform under current selection.", MessageType.Warning);
#endif
                    break;

                case SerialPortUtilityPro.OpenSystem.BluetoothSSP:
                    obj.DeviceName = EditorGUILayout.TextField("Device ID", obj.DeviceName);
#if UNITY_ANDROID
                    obj.BluetoothSSPNoServerMode = EditorGUILayout.Toggle("Server mode is not available", obj.BluetoothSSPNoServerMode);
#endif
                    obj.Skip = EditorGUILayout.IntField("Order (Default:0)", obj.Skip);
                    if (obj.Skip < 0)
                    {
                        obj.Skip = 0;
                    }

                    GUI.backgroundColor = Color.yellow;
                    GUILayout.BeginHorizontal();
                    GUILayout.FlexibleSpace();
                    if (GUILayout.Button("Show the devices connected to this PC.", GUILayout.Width(300)))
                    {
                        SPAPTools window = (SPAPTools)EditorWindow.GetWindow(typeof(SPAPTools), true, "Show the devices connected to this PC.", true);
                        window.spapObject = obj;
                        window.Show();
                    }
                    GUILayout.EndHorizontal();
                    break;

                case SerialPortUtilityPro.OpenSystem.NumberOrder:
                    obj.Skip = EditorGUILayout.IntField("Order (Default:0)", obj.Skip);
                    if (obj.Skip < 0)
                    {
                        obj.Skip = 0;
                    }
                    break;

                case SerialPortUtilityPro.OpenSystem.TCPSerialEmulatorClient:
                    obj.VendorID  = EditorGUILayout.TextField("Connect IPv4 Address", obj.VendorID);
                    obj.ProductID = EditorGUILayout.TextField("Connect Port", obj.ProductID);
                    if (!int.TryParse(obj.ProductID, out intdata))
                    {
                        obj.ProductID = "";
                    }
                    break;

                case SerialPortUtilityPro.OpenSystem.TCPSerialEmulatorServer:
                    obj.VendorID  = "";
                    obj.ProductID = EditorGUILayout.TextField("Connected Port", obj.ProductID);
                    if (!int.TryParse(obj.ProductID, out intdata))
                    {
                        obj.ProductID = "";
                    }
                    break;
                }
                GUI.backgroundColor = Color.white;
                EditorGUILayout.EndVertical();

                EditorGUILayout.BeginVertical(GUI.skin.box);
                EditorGUILayout.PropertyField(DebugConsoleMonitorProperty, new GUIContent("Enabled Debug UI Console"));
                EditorGUILayout.PropertyField(ExternalConfig, new GUIContent("Enabled External Config"));
                if (ExternalConfig.boolValue)
                {
                    obj.ExternalConfigPath     = (SerialPortUtilityPro.SourcePath)EditorGUILayout.EnumPopup(" Config File Path", obj.ExternalConfigPath);
                    obj.ExternalConfigFileName = EditorGUILayout.TextField(" ", obj.ExternalConfigFileName);
                }
                obj.EnabledTransmission = EditorGUILayout.Toggle("Enabled Transmission", obj.EnabledTransmission);
                EditorGUILayout.EndVertical();

                if (obj.OpenMethod != SerialPortUtilityPro.OpenSystem.TCPSerialEmulatorClient &&
                    obj.OpenMethod != SerialPortUtilityPro.OpenSystem.TCPSerialEmulatorServer &&
                    obj.OpenMethod != SerialPortUtilityPro.OpenSystem.BluetoothSSP)
                {
                    EditorGUILayout.BeginVertical(GUI.skin.box);
                    EditorGUILayout.LabelField("Communication Structure", EditorStyles.boldLabel);

                    if (System.Enum.IsDefined(typeof(_baudrateSel), obj.BaudRate))
                    {
                        obj.BaudRate = (int)(_baudrateSel)EditorGUILayout.EnumPopup("BaudRate", (_baudrateSel)obj.BaudRate);
                        obj.BaudRate = EditorGUILayout.IntField(" ", obj.BaudRate);
                    }
                    else
                    {
                        obj.BaudRate = EditorGUILayout.IntField("BaudRate", obj.BaudRate);
                    }

                    obj.Parity              = (SerialPortUtilityPro.ParityEnum)EditorGUILayout.EnumPopup("Parity", obj.Parity);
                    obj.StopBit             = (SerialPortUtilityPro.StopBitEnum)EditorGUILayout.EnumPopup("Stop Bit", obj.StopBit);
                    obj.DataBit             = (SerialPortUtilityPro.DataBitEnum)EditorGUILayout.EnumPopup("Data Bit", obj.DataBit);
                    obj.RecvDiscardNull     = EditorGUILayout.Toggle("Discard Null Receive", obj.RecvDiscardNull);
                    obj.AutoRTSCTSHandshake = EditorGUILayout.Toggle("Auto RTS/CTS Handshake", obj.AutoRTSCTSHandshake);
                    obj.StartEnableDTR      = EditorGUILayout.Toggle("Start DTR Enable", obj.StartEnableDTR);
                    EditorGUILayout.EndVertical();
                }

                if (obj.IsOpened())
                {
                    EditorGUI.EndDisabledGroup();
                }

                EditorGUILayout.BeginVertical(GUI.skin.box);
                EditorGUILayout.LabelField("Read Data Structure", EditorStyles.boldLabel);
                obj.ReadProtocol = (SerialPortUtilityPro.MethodSystem)EditorGUILayout.EnumPopup("Read Protocol", obj.ReadProtocol);
                switch (obj.ReadProtocol)
                {
                case SerialPortUtilityPro.MethodSystem.Streaming:
                case SerialPortUtilityPro.MethodSystem.BinaryStreaming:
                    EditorGUILayout.PropertyField(ReadCompleteEventObject);
                    break;

                case SerialPortUtilityPro.MethodSystem.SplitStringToArray:
                    obj.SplitCode = GUISplitFeedCodeEnumPop("Split Code", obj.SplitCode);
                    obj.FeedCode  = GUISplitFeedCodeEnumPop("Feed Code", obj.FeedCode);
                    EditorGUILayout.PropertyField(ReadCompleteEventObject);
                    EditorGUILayout.HelpBox(" Receive Data Format: " +
                                            "AAA" + obj.SplitCode + "BBB" + obj.SplitCode + "CCC" + obj.FeedCode, MessageType.None);
                    break;

                case SerialPortUtilityPro.MethodSystem.FixedLengthDataToString:
                case SerialPortUtilityPro.MethodSystem.FixedLengthDataToBinary:
                    obj.FixedFeedCount = EditorGUILayout.IntField("Fixed-length Data Size", obj.FixedFeedCount);
                    EditorGUILayout.PropertyField(ReadCompleteEventObject);
                    break;

                case SerialPortUtilityPro.MethodSystem.SplitStringToDictionary:
                    obj.SplitCode = GUISplitFeedCodeEnumPop("Split Code", obj.SplitCode);
                    obj.FeedCode  = GUISplitFeedCodeEnumPop("Feed Code", obj.FeedCode);
                    EditorGUILayout.PropertyField(ReadCompleteEventObject);
                    EditorGUILayout.HelpBox(" Receive Data Format: " +
                                            "AAA=DDD" + obj.SplitCode + "BBB=EEE" + obj.SplitCode + "CCC=FFF" + obj.FeedCode, MessageType.None);
                    break;

                case SerialPortUtilityPro.MethodSystem.JSONToClassObject:
                    obj.ReadCompleteEventObjectType = EditorGUILayout.TextField("Object Type", obj.ReadCompleteEventObjectType);
                    GUI.backgroundColor             = Color.yellow;
                    GUILayout.BeginHorizontal();
                    GUILayout.FlexibleSpace();
                    if (GUILayout.Button("Show Object Type from this project.", GUILayout.Width(300)))
                    {
                        SPAPToolsObjectType window = (SPAPToolsObjectType)EditorWindow.GetWindow(typeof(SPAPToolsObjectType), true, "Show Object Type from this project.", true);
                        window.spapObject = obj;
                        window.Show();
                    }
                    GUILayout.EndHorizontal();
                    GUI.backgroundColor = Color.white;
                    EditorGUILayout.PropertyField(ReadCompleteEventObject);
                    break;

                case SerialPortUtilityPro.MethodSystem.SplitStringToGameObject:
                    obj.SplitCode = GUISplitFeedCodeEnumPop("Split Code", obj.SplitCode);
                    obj.FeedCode  = GUISplitFeedCodeEnumPop("Feed Code", obj.FeedCode);
                    obj.ReadClassMembersObject = EditorGUILayout.ObjectField("GameObject", obj.ReadClassMembersObject, typeof(GameObject), true) as GameObject;
                    EditorGUILayout.HelpBox(" Receive Data Format: " +
                                            "Class.VarName1=DDD" + obj.SplitCode + "Class.VarName2=EEE" + obj.SplitCode + "Class1.VarName1=FFF" + obj.FeedCode, MessageType.None);
                    break;

                case SerialPortUtilityPro.MethodSystem.LineFeedDataToString:
                case SerialPortUtilityPro.MethodSystem.LineFeedDataToBinary:
                    obj.FeedCode = GUISplitFeedCodeEnumPop("Feed Code", obj.FeedCode);
                    EditorGUILayout.PropertyField(ReadCompleteEventObject);
                    EditorGUILayout.HelpBox(" Receive Data Format: " +
                                            "AAABBBCCC" + obj.FeedCode, MessageType.None);
                    break;

                case SerialPortUtilityPro.MethodSystem.ModbusASCII:
                    EditorGUILayout.PropertyField(ReadCompleteEventObject);
                    EditorGUILayout.HelpBox(" Receive Data Format: " +
                                            ":AABBDDDDDDXX<CR><LF>", MessageType.None);
                    break;
                    //case SerialPortUtilityPro.MethodSystem.ModbusRTU:
                    //	break;
                }

                EditorGUILayout.EndVertical();

                EditorGUILayout.BeginVertical(GUI.skin.box);
                EditorGUILayout.LabelField("Write Data Command", EditorStyles.boldLabel);
                EditorGUILayout.HelpBox("bool Write(string);\n" +
                                        "bool Write(string, feedCode);\n" +
                                        "bool WriteCRLF(string);\n" +
                                        "bool WriteCR(string);\n" +
                                        "bool WriteLF(string);\n" +
                                        "bool Write(List<string>, splitCode, feedCode);\n" +
                                        "bool Write(Dictionary<key, string>, splitCode, feedCode);\n" +
                                        "bool Write(SPUPMudbusData, binaryMode);" +
                                        "bool Write(byte[] or byte);\n" +
                                        "bool WriteJSON(object);"
                                        , MessageType.Info);
                EditorGUILayout.EndVertical();
            }

            EditorGUILayout.Space();
            GUI.backgroundColor = new Color(0.50f, 0.70f, 1.0f);
            if (GUILayout.Button("SerialPort Utility Events", EditorStyles.toolbarButton))
            {
                ExpandEventsProperty.boolValue = !ExpandEventsProperty.boolValue;
            }

            GUI.backgroundColor = Color.white;
            GUI.color           = Color.white;
            if (ExpandEventsProperty.boolValue)
            {
                obj.UpdateProcessing = (SerialPortUtilityPro.UpdateMethod)EditorGUILayout.EnumPopup("Processing Update", obj.UpdateProcessing);
                if (obj.UpdateProcessing == SerialPortUtilityPro.UpdateMethod.ManualUpdate)
                {
                    EditorGUILayout.HelpBox("Please call ReadUpdate() at an arbitrary timing.", MessageType.Info);
                }
                EditorGUILayout.LabelField("Event Handler");
                EditorGUILayout.PropertyField(SystemEventObject);
            }

            EditorGUILayout.Space();
            GUI.backgroundColor = new Color(0.50f, 0.70f, 1.0f);
            if (GUILayout.Button("SerialPort Debug Monitor", EditorStyles.toolbarButton))
            {
                ExpandSPMonitorProperty.boolValue = !ExpandSPMonitorProperty.boolValue;
            }

            GUI.backgroundColor = Color.white;
            GUI.color           = Color.white;
            if (ExpandSPMonitorProperty.boolValue)
            {
                EditorGUILayout.Space();
                GUILayout.BeginHorizontal();
                if (!obj.IsOpened())
                {
                    EditorGUI.BeginDisabledGroup(true);
                }
                _sendText = EditorGUILayout.TextField(_sendText);
                _fcds     = (_feedCodeDebugSel)EditorGUILayout.EnumPopup(_fcds, GUILayout.Width(60));
                if (GUILayout.Button("Send", GUILayout.Width(60), GUILayout.Height(15)))
                {
                    if (_sendText != string.Empty)
                    {
                        string feedc = "";                              //None
                        switch (_fcds)
                        {
                        case _feedCodeDebugSel.LF: feedc = "<LF>";
                            break;

                        case _feedCodeDebugSel.CR: feedc = "<CR>";
                            break;

                        case _feedCodeDebugSel.CRLF: feedc = "<CR><LF>";
                            break;

                        default: break;
                        }
                        if (!obj.Write(_sendText, feedc))                               //Write
                        {
                            obj.SerialDebugAddString(" [Send Error!]");
                        }
                    }
                }

                GUILayout.EndHorizontal();
                EditorGUILayout.Space();
                GUILayout.BeginHorizontal();
                obj.RtsEnabled = EditorGUILayout.ToggleLeft("RTS(OUT)", obj.RtsEnabled, GUILayout.Width(80));
                obj.DtrEnabled = EditorGUILayout.ToggleLeft("DTR(OUT)", obj.DtrEnabled, GUILayout.Width(110));
                EditorGUILayout.ToggleLeft("CTS(IN)", obj.CtsHolding(), GUILayout.Width(80));
                EditorGUILayout.ToggleLeft("DSR(IN)", obj.DsrHolding(), GUILayout.Width(80));
                GUILayout.EndHorizontal();
                EditorGUILayout.Space();

                _scrollPos = EditorGUILayout.BeginScrollView(_scrollPos, EditorStyles.textArea, GUILayout.Height(260));
                foreach (string destr in obj.GetSerialDebugString.Split("\n".ToCharArray()))
                {
                    EditorGUILayout.SelectableLabel(destr, GUILayout.Height(15.0f));
                }
                EditorGUILayout.EndScrollView();

                if (!obj.IsOpened())
                {
                    EditorGUI.EndDisabledGroup();
                }
            }
            EditorGUILayout.Space();

            EditorGUILayout.LabelField("SerialPort Utility Pro Version " + SerialPortUtilityPro.VersionString);
            EditorGUILayout.Space();

            //changed param
            if (GUI.changed)
            {
                if (ExpandSPMonitorProperty.boolValue)
                {
                    if (obj.IsOpened())
                    {
                        obj.RtsEnable(obj.RtsEnabled);
                        obj.DtrEnable(obj.DtrEnabled);
                    }
                }

                //Todo
                serializedObject.ApplyModifiedProperties();
            }

            EditorUtility.SetDirty(target);             //editor set
        }