Beispiel #1
0
    protected void btReadTag_Click(object sender, EventArgs e)
    {
        if (String.IsNullOrEmpty(tbServerName.Text) || String.IsNullOrEmpty(tbTagName.Text))
        {
            liResult.Text = "OPC Server or tag name is invalid";
            return;
        }

        OPCServer srv = new OPCServer(tbServerName.Text);

        if (!srv.IsConnected)
        {
            liResult.Text = "Could not establish connection to OPC Server";
            return;
        }

        OPCServerClient opcClient = new OPCServerClient(srv);

        liResult.Text = String.Format("{0}: {1}", tbTagName.Text, opcClient.ReadTagVal(tbTagName.Text).ToString());

        // Do not use in production code, it is only a demo.
        // The connection to the OPC server should not be open and closed on each HTTP request
        // It should only be opened once and kept in the user's session
        // Disconnecting from the OPC must be handled inside the Session_End event
        srv.Disconnect();
    }
Beispiel #2
0
        // Async method to send tags telemetry
        private static async void SendDeviceToCloudMessagesAsync()
        {
            var       opcDAServerUrl = ConfigurationManager.AppSettings["OPCDAServerUrl"];
            OPCServer srv            = new OPCServer(opcDAServerUrl);

            Console.WriteLine(srv.ErrorMessage);
            Console.WriteLine("Connected to OPCDA Server:" + srv.IsConnected);

            OPCServerClient opcClient = new OPCServerClient(srv);

            while (true)
            {
                // Create JSON message
                var results = opcClient.ReadTagVal(ConfigurationManager.AppSettings["TagFileNamePath"]);
                // Console.WriteLine("Number of tags data:" + results.Length);
                foreach (var item in results)
                {
                    var telemetryDataPoint = new
                    {
                        itemname  = item.ItemName,
                        value     = Convert.ToDouble(item.Value),
                        quality   = item.Quality.ToString(),
                        timestamp = item.Timestamp
                    };
                    var messageString = JsonConvert.SerializeObject(telemetryDataPoint);
                    var message       = new Message(Encoding.ASCII.GetBytes(messageString));

                    // Add a custom application property to the message.
                    // An IoT hub can filter on these properties without access to the message body.
                    // message.Properties.Add("temperatureAlert", (currentTemperature > 30) ? "true" : "false");

                    // Send the telemetry message
                    await s_deviceClient.SendEventAsync(message);

                    Console.WriteLine("{0} > Sending message: {1}", DateTime.Now, messageString);
                }
                await Task.Delay(Convert.ToInt32(ConfigurationManager.AppSettings["StreamInterval"]));
            }
        }