예제 #1
0
        // Receive data back and print out to the debug console
        private static void comDataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            if (rs485 == null)
            {
                return;
            }

            int dataLength = rs485.rxBytesLength();

            byte[] rxBuffer = new byte[dataLength];
            rs485.Read(rxBuffer, 0, dataLength);

            // Conver to string
            String rxString = "";

            try
            {
                rxString = new String(System.Text.Encoding.UTF8.GetChars(rxBuffer));
            }
            catch (System.Exception exception)
            {
            }

            Debug.Print(rxString);
        }
예제 #2
0
        // Receive RS485 data and send back an answer
        private static void comDataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            if (rs485 == null)
            {
                return;
            }

            int dataLength = rs485.rxBytesLength();

            if (dataLength < 16)
            {
                return;
            }

            byte[] rxBuffer = new byte[dataLength];
            rs485.Read(rxBuffer, 0, dataLength);

            // Conver to string
            String rxString = "";

            try
            {
                rxString = new String(System.Text.Encoding.UTF8.GetChars(rxBuffer));
                Debug.Print(rxString);
            }
            catch (System.Exception exception)
            {
            }

            // Send back an answer
            byte[] txBuffer = rxBuffer;
            if (rxString.CompareTo(pingString) == 0)
            {
                txBuffer = System.Text.Encoding.UTF8.GetBytes(pongString);
            }

            rs485.Write(txBuffer);
        }