public void Run()
            {
                // Prints the version of the application and the CAS BACnet stack.
                Console.WriteLine("Starting Modbus RTU Master Example version {0}.{1}", APPLICATION_VERSION, CIBuildVersion.CIBUILDNUMBER);
                Console.WriteLine("https://github.com/chipkin/ModbusTCPMasterExampleCSharp");
                Console.WriteLine("FYI: CAS Modbus Stack version: {0}.{1}.{2}.{3}",
                                  CASModbusAdapter.GetAPIMajorVersion(),
                                  CASModbusAdapter.GetAPIMinorVersion(),
                                  CASModbusAdapter.GetAPIPatchVersion(),
                                  CASModbusAdapter.GetAPIBuildVersion());

                // Set up the API and callbacks.
                uint returnCode = CASModbusAdapter.Init(CASModbusAdapter.TYPE_RTU, SendMessage, RecvMessage, CurrentTime);

                if (returnCode != CASModbusAdapter.STATUS_SUCCESS)
                {
                    Console.WriteLine("Error: Could not init the Modbus Stack, returnCode={0}", returnCode);
                    return;
                }

                // All done with the Modbus setup.
                Console.WriteLine("FYI: CAS Modbus Stack Setup, successfuly");

                // Create a new SerialPort object with default settings.
                _serialPort = new SerialPort();

                // Allow the user to set the appropriate properties.
                _serialPort.PortName  = SetPortName(_serialPort.PortName);
                _serialPort.BaudRate  = SetPortBaudRate(_serialPort.BaudRate);
                _serialPort.Parity    = SetPortParity(_serialPort.Parity);
                _serialPort.DataBits  = SetPortDataBits(_serialPort.DataBits);
                _serialPort.StopBits  = SetPortStopBits(_serialPort.StopBits);
                _serialPort.Handshake = SetPortHandshake(_serialPort.Handshake);

                // Set the read/write timeouts
                _serialPort.ReadTimeout  = 10;
                _serialPort.WriteTimeout = 10;

                _serialPort.Open();

                // Program loop.
                while (true)
                {
                    // Check for user input
                    this.DoUserInput();

                    // Run the Modbus loop proccessing incoming messages.
                    CASModbusAdapter.Loop();

                    // Give some time to other applications.
                    System.Threading.Thread.Sleep(1);
                }
            }
            public void Run()
            {
                // Prints the version of the application and the CAS BACnet stack.
                Console.WriteLine("Starting Modbus TCP Slave Example  version {0}.{1}", APPLICATION_VERSION, CIBuildVersion.CIBUILDNUMBER);
                Console.WriteLine("https://github.com/chipkin/ModbusTCPMasterExampleCSharp");
                Console.WriteLine("FYI: CAS Modbus Stack version: {0}.{1}.{2}.{3}",
                                  CASModbusAdapter.GetAPIMajorVersion(),
                                  CASModbusAdapter.GetAPIMinorVersion(),
                                  CASModbusAdapter.GetAPIPatchVersion(),
                                  CASModbusAdapter.GetAPIBuildVersion());

                // Set up the API and callbacks.
                uint returnCode = CASModbusAdapter.Init(CASModbusAdapter.TYPE_TCP, SendMessage, RecvMessage, CurrentTime);

                if (returnCode != CASModbusAdapter.STATUS_SUCCESS)
                {
                    Console.WriteLine("Error: Could not init the Modbus Stack, returnCode={0}", returnCode);
                    return;
                }

                // Set the modbus slave address. For Modbus TCP 0 and 255 are the only valid slave address.
                CASModbusAdapter.SetSlaveId(ModbusSlave.SETTING_MODBUS_SERVER_SLAVE_ADDRESS);

                // Set up the call back functions for data.
                CASModbusAdapter.RegisterGetValue(GetModbusValue);
                CASModbusAdapter.RegisterSetValue(SetModbusValue);

                // All done with the Modbus setup.
                Console.WriteLine("FYI: CAS Modbus Stack Setup, successfuly");

                // Create the database and fill it with default data
                this.database = new UInt16[SETTING_MODBUS_DATABASE_MAX_SIZE];
                for (UInt16 offset = 0; offset < SETTING_MODBUS_DATABASE_MAX_SIZE; offset++)
                {
                    this.database[offset] = SETTING_MODBUS_DATABASE_DEFAULT_VALUE;
                }

                // Configure the TCP Listen class.
                // https://docs.microsoft.com/en-us/dotnet/framework/network-programming/synchronous-server-socket-example
                IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, SETTING_MODBUS_SERVER_TCP_PORT);

                this.tcpListener = new Socket(IPAddress.Any.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

                // Bind the socket to the local endpoint and
                // listen for incoming connections.
                try
                {
                    Console.WriteLine("FYI: Binding tcp port. port=[{0}]...", SETTING_MODBUS_SERVER_TCP_PORT);
                    this.tcpListener.Bind(localEndPoint);
                    this.tcpListener.Listen(1);
                    Console.WriteLine("FYI: Waiting on TCP connection TCP port=[{0}]...", SETTING_MODBUS_SERVER_TCP_PORT);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                }

                // Program loop.
                while (true)
                {
                    try
                    {
                        // Program is suspended while waiting for an incoming connection.
                        this.tcpClient = this.tcpListener.Accept();
                        Console.WriteLine("FYI: Got a connection from IP address=[{0}]", this.tcpClient.RemoteEndPoint.ToString());

                        // Do loop
                        while (IsSocketConnected(this.tcpClient))
                        {
                            // Check for user input
                            this.DoUserInput();

                            // Run the Modbus loop proccessing incoming messages.
                            CASModbusAdapter.Loop();

                            // Finally flush the buffer
                            CASModbusAdapter.Flush();

                            // Give some time to other applications.
                            System.Threading.Thread.Sleep(1);
                        } // Client connected loop.
                        Console.WriteLine("FYI: TCP Connection disconnected");
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.ToString());
                    }
                } // Main program loop
            }
            public void Run()
            {
                // Prints the version of the application and the CAS BACnet stack.
                Console.WriteLine("Starting Modbus RTU Slave Example  version {0}.{1}", APPLICATION_VERSION, CIBuildVersion.CIBUILDNUMBER);
                Console.WriteLine("https://github.com/chipkin/ModbusTCPMasterExampleCSharp");
                Console.WriteLine("FYI: CAS Modbus Stack version: {0}.{1}.{2}.{3}",
                                  CASModbusAdapter.GetAPIMajorVersion(),
                                  CASModbusAdapter.GetAPIMinorVersion(),
                                  CASModbusAdapter.GetAPIPatchVersion(),
                                  CASModbusAdapter.GetAPIBuildVersion());

                // Set up the API and callbacks.
                uint returnCode = CASModbusAdapter.Init(CASModbusAdapter.TYPE_RTU, SendMessage, RecvMessage, CurrentTime);

                if (returnCode != CASModbusAdapter.STATUS_SUCCESS)
                {
                    Console.WriteLine("Error: Could not init the Modbus Stack, returnCode={0}", returnCode);
                    return;
                }

                // Set the modbus slave address. For Modbus TCP 0 and 255 are the only valid slave address.
                CASModbusAdapter.SetSlaveId(ModbusSlave.SETTING_MODBUS_SERVER_SLAVE_ADDRESS);

                // Set up the call back functions for data.
                CASModbusAdapter.RegisterGetValue(GetModbusValue);
                CASModbusAdapter.RegisterSetValue(SetModbusValue);


                // All done with the Modbus setup.
                Console.WriteLine("FYI: CAS Modbus Stack Setup, successfuly");

                // Create the database and fill it with default data
                this.database = new UInt16[SETTING_MODBUS_DATABASE_MAX_SIZE];
                for (UInt16 offset = 0; offset < SETTING_MODBUS_DATABASE_MAX_SIZE; offset++)
                {
                    this.database[offset] = SETTING_MODBUS_DATABASE_DEFAULT_VALUE;
                }

                // Connect and set up the serial ports

                // Create a new SerialPort object with default settings.
                _serialPort = new SerialPort();

                // Allow the user to set the appropriate properties.
                _serialPort.PortName  = SetPortName(_serialPort.PortName);
                _serialPort.BaudRate  = SetPortBaudRate(_serialPort.BaudRate);
                _serialPort.Parity    = SetPortParity(_serialPort.Parity);
                _serialPort.DataBits  = SetPortDataBits(_serialPort.DataBits);
                _serialPort.StopBits  = SetPortStopBits(_serialPort.StopBits);
                _serialPort.Handshake = SetPortHandshake(_serialPort.Handshake);

                // Set the read/write timeouts
                _serialPort.ReadTimeout  = 10;
                _serialPort.WriteTimeout = 10;

                _serialPort.Open();

                // Program loop.
                while (true)
                {
                    // Check for user input
                    this.DoUserInput();

                    // Run the Modbus loop proccessing incoming messages.
                    CASModbusAdapter.Loop();

                    // Give some time to other applications.
                    System.Threading.Thread.Sleep(1);
                }
            }
            public void Run()
            {
                // Prints the version of the application and the CAS BACnet stack.
                Console.WriteLine("Starting Modbus TCP Master Example  version {0}.{1}", APPLICATION_VERSION, CIBuildVersion.CIBUILDNUMBER);
                Console.WriteLine("https://github.com/chipkin/ModbusTCPMasterExampleCSharp");
                Console.WriteLine("FYI: CAS Modbus Stack version: {0}.{1}.{2}.{3}",
                                  CASModbusAdapter.GetAPIMajorVersion(),
                                  CASModbusAdapter.GetAPIMinorVersion(),
                                  CASModbusAdapter.GetAPIPatchVersion(),
                                  CASModbusAdapter.GetAPIBuildVersion());


                // Set up the API and callbacks.
                uint returnCode = CASModbusAdapter.Init(CASModbusAdapter.TYPE_TCP, SendMessage, RecvMessage, CurrentTime);

                if (returnCode != CASModbusAdapter.STATUS_SUCCESS)
                {
                    Console.WriteLine("Error: Could not init the Modbus Stack, returnCode={0}", returnCode);
                    return;
                }

                // All done with the Modbus setup.
                Console.WriteLine("FYI: CAS Modbus Stack Setup, successfuly");


                // Configure the TCP Listen class.
                // https://docs.microsoft.com/en-us/dotnet/framework/network-programming/synchronous-server-socket-example
                IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, SETTING_MODBUS_SERVER_TCP_PORT);

                this.tcpListener = new Socket(IPAddress.Any.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

                // Program loop.
                while (true)
                {
                    try
                    {
                        // Check for user input
                        DoUserInput();

                        // Run the Modbus loop proccessing incoming messages.
                        CASModbusAdapter.Loop();

                        // Give some time to other applications.
                        System.Threading.Thread.Sleep(1);

                        // If we are connected to a Modbus Slave device. proccess incoming message.
                        if (this.tcpClient != null && this.tcpClient.Connected)
                        {
                            // Finally flush the buffer
                            CASModbusAdapter.Flush();
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.ToString());
                        if (this.tcpClient != null && this.tcpClient.Connected)
                        {
                            this.tcpClient.Disconnect(true);
                        }
                    }
                } // Main program loop
            }