Ejemplo n.º 1
0
    static void Main()
    {
        IPConnection  ipcon = new IPConnection();            // Create IP connection
        BrickletRS485 rs485 = new BrickletRS485(UID, ipcon); // Create device object

        ipcon.Connect(HOST, PORT);                           // Connect to brickd
        // Don't use device before ipcon is connected

        // Enable full-duplex mode
        rs485.SetRS485Configuration(115200, BrickletRS485.PARITY_NONE,
                                    BrickletRS485.STOPBITS_1, BrickletRS485.WORDLENGTH_8,
                                    BrickletRS485.DUPLEX_FULL);

        // Register read callback to function ReadCB
        rs485.ReadCallback += ReadCB;

        // Enable read callback
        rs485.EnableReadCallback();

        // Write "test" string
        rs485.Write("test".ToCharArray());

        Console.WriteLine("Press enter to exit");
        Console.ReadLine();
        ipcon.Disconnect();
    }
Ejemplo n.º 2
0
    static void Main()
    {
        IPConnection  ipcon = new IPConnection();            // Create IP connection
        BrickletRS485 rs485 = new BrickletRS485(UID, ipcon); // Create device object

        ipcon.Connect(HOST, PORT);                           // Connect to brickd
        // Don't use device before ipcon is connected

        // Set operating mode to Modbus RTU master
        rs485.SetMode(BrickletRS485.MODE_MODBUS_MASTER_RTU);

        // Modbus specific configuration:
        // - slave address = 1 (unused in master mode)
        // - master request timeout = 1000ms
        rs485.SetModbusConfiguration(1, 1000);

        // Register Modbus master write single register response callback to function
        // ModbusMasterWriteSingleRegisterResponseCB
        rs485.ModbusMasterWriteSingleRegisterResponseCallback += ModbusMasterWriteSingleRegisterResponseCB;

        // Write 65535 to register 42 of slave 17
        expectedRequestID = rs485.ModbusMasterWriteSingleRegister(17, 42, 65535);

        Console.WriteLine("Press enter to exit");
        Console.ReadLine();
        ipcon.Disconnect();
    }
Ejemplo n.º 3
0
    // Callback function for Modbus master write single register response callback
    static void ModbusMasterWriteSingleRegisterResponseCB(BrickletRS485 sender,
                                                          byte requestID,
                                                          short exceptionCode)
    {
        Console.WriteLine("Request ID: " + requestID);
        Console.WriteLine("Exception Code: " + exceptionCode);

        if (requestID != expectedRequestID)
        {
            Console.WriteLine("Error: Unexpected request ID");
        }
    }
Ejemplo n.º 4
0
    private static string UID  = "XYZ";    // Change XYZ to the UID of your RS485 Bricklet

    // Callback function for Modbus slave write single register request callback
    static void ModbusSlaveWriteSingleRegisterRequestCB(BrickletRS485 sender,
                                                        byte requestID,
                                                        long registerAddress,
                                                        int registerValue)
    {
        Console.WriteLine("Request ID: " + requestID);
        Console.WriteLine("Register Address: " + registerAddress);
        Console.WriteLine("Register Value: " + registerValue);

        if (registerAddress != 42)
        {
            Console.WriteLine("Error: Invalid register address");
            sender.ModbusSlaveReportException(requestID, BrickletRS485.EXCEPTION_CODE_ILLEGAL_DATA_ADDRESS);
        }
        else
        {
            sender.ModbusSlaveAnswerWriteSingleRegisterRequest(requestID);
        }
    }
Ejemplo n.º 5
0
    static void Main()
    {
        IPConnection  ipcon = new IPConnection();            // Create IP connection
        BrickletRS485 rs485 = new BrickletRS485(UID, ipcon); // Create device object

        ipcon.Connect(HOST, PORT);                           // Connect to brickd
        // Don't use device before ipcon is connected

        // Set operating mode to Modbus RTU slave
        rs485.SetMode(BrickletRS485.MODE_MODBUS_SLAVE_RTU);

        // Modbus specific configuration:
        // - slave address = 17
        // - master request timeout = 0ms (unused in slave mode)
        rs485.SetModbusConfiguration(17, 0);

        // Register Modbus slave write single register request callback to function
        // ModbusSlaveWriteSingleRegisterRequestCB
        rs485.ModbusSlaveWriteSingleRegisterRequestCallback += ModbusSlaveWriteSingleRegisterRequestCB;

        Console.WriteLine("Press enter to exit");
        Console.ReadLine();
        ipcon.Disconnect();
    }
Ejemplo n.º 6
0
    private static string UID  = "XYZ";    // Change XYZ to the UID of your RS485 Bricklet

    // Callback function for read callback
    static void ReadCB(BrickletRS485 sender, char[] message)
    {
        // Assume that the message consists of ASCII characters and
        // convert it from an array of chars to a string
        Console.WriteLine("Message: \"" + new string(message) + "\"");
    }