Esempio n. 1
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);
        }
Esempio n. 2
0
        public static void Main()
        {
            // Instantiate the main RS485Shield handler
            rs485 = new RS485Shield("COM1", 9600, Parity.None, 8, StopBits.One, Pins.GPIO_PIN_D2);

            // Add a suitable RxEvent Handler Callback
            rs485.initHandlers(new SerialDataReceivedEventHandler(comDataReceived), null);

            long lastTick = Utility.GetMachineTime().Ticks;

            // Endless loop.
            while (true)
            {
                long curTick = Utility.GetMachineTime().Ticks;
                if ((curTick - lastTick) > timePeriod)
                {
                    // Because I'm the "ping actor", just start a ping-pong game
                    rs485.Write(txBuffer);

                    // repeat each second
                    lastTick = curTick;
                }
            }
        }