ArduinityCommunicator.ReceiveMessage C# (CSharp) 메소드

ReceiveMessage() 공개 메소드

public ReceiveMessage ( ) : string
리턴 string
    public string ReceiveMessage()
    {
        string response = "";

        if (IsSetup())
        {
            try
            {
                while (response == "" || response == null)
                {
                    response = serialPort.ReadLine();
                    if (response != null && response != "")
                    {
                        #if debugSerial
                        Debug.Log("ArduinityCommunicator::RecieveMessage(): got response " + response);
                        #endif
                        return response;
                    }
                }
            }
            #if debugSerial
            catch (Exception e)
            {
                Debug.Log("ArduinityCommunicator::RecieveMessage(): exception trying to read " + e.Message);
            }
            #else
            catch
            {

            }
            #endif
        }
        else
        {
        #if debugSerial
            Debug.Log("ArduinityCommunicator::RecieveMessage(): serial is not open for reading");
        #endif
        }

        return response;
    }

Usage Example

예제 #1
0
    void LateUpdate()
    {
        if (continuousUpdate && IsSetup())
        {
            // TODO: wrap this all in an updatepins function
            string message;
            // TODO: switch this to batch mode
            for (int i = 0; i < digitalPins.Length; i++)
            {
                if (digitalPinModes[i] == ArduinoDigitalPinMode.INPUT ||
                    digitalPinModes[i] == ArduinoDigitalPinMode.INPUT_PULLUP)
                {
                    // response should be something like 'arduinity_dvalue 6 1'
                    // which is pin six value high
                    string response = arduinoCommunicator.SendReceiveMessage("arduinity_dread " + i);

                    // read the last number in the response string

                    /*int receivedPin = int.Parse(response.Substring(response.IndexOf(' ')+1,response.LastIndexOf(' ')));
                     * if(receivedPin != i) Debug.Log("Arduinity::LateUpdate(): recieved late message (" + response + ")");
                     * digitalPins[receivedPin] = int.Parse(response.Substring(response.LastIndexOf(' ') + 1));*/

                    digitalPins[i] = int.Parse(response.Substring(response.LastIndexOf(' ') + 1));
                }
                else if (digitalPinModes[i] == ArduinoDigitalPinMode.OUTPUT)
                {
                    message = "arduinity_dwrite " + i + " " + digitalPins[i];
                    arduinoCommunicator.SendMessage(message);
                    #if debugSerialMessage
                    Debug.Log("ArduinitySerialInterface::LateUpdate(): sent message: " + message);
                    #endif
                }
                else if (digitalPinModes[i] == ArduinoDigitalPinMode.OUTPUT_PWM)
                {
                    message = "arduinity_pwrite " + i + " " + digitalPins[i];
                    arduinoCommunicator.SendMessage(message);
                    #if debugSerialMessage
                    Debug.Log("ArduinitySerialInterface::LateUpdate(): sent message: " + message);
                    #endif
                }
            }

            for (int i = 0; i < analogPins.Length; i++)
            {
                if (analogPinModes[i] == ArduinoAnalogPinMode.INPUT)
                {
                    string response = arduinoCommunicator.SendReceiveMessage("arduinity_aread " + i);
                    // this will protect from trying to parse empty strings
                    if (response == "" || response == null)
                    {
                        continue;
                    }
                    analogPins[i] = int.Parse(response.Substring(response.LastIndexOf(' ') + 1));
                }

                /*else if (analogPinModes[i] == ArduinoAnalogPinMode.OUTPUT_DIGITAL)
                 * {
                 *  message = "arduinity_dwrite A" + i + analogPins[i];
                 *  arduinoCommunicator.SendMessage(message);
                 * }*/
            }
            #if debugSerial
            Debug.Log(arduinoCommunicator.ReceiveMessage());
            #endif
        }
    }