[英]How to send messages from Cloud to Device with IOT Hub?
我嘗試使用以下鏈接中提到的步驟通過 IOT Hub 接收雲到設備消息: https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-csharp-csharp-c2d
我附上了我正在使用的代碼和我得到的結果。
在模擬設備應用程序中,我將此方法稱為“ReceiveMessageAsync()”。
我已經更新了我得到的 output 的屏幕截圖。 輸入“Enter”后,我沒有得到任何 output。 解碼代碼后,我可以看到在 ReceiveMessageAsync() 方法中,我收到了receivedMessage == null
。
請幫助我處理這段代碼,並提出我應該做出的改變以使其完美運行。
========================================
public async void ReceiveMessageAsync()
{
try
{
Message receivedMessage = await _deviceClient?.ReceiveAsync();
if(receivedMessage == null)
{
ReceivedMessage = null;
return;
}
ReceivedMessage = Encoding.ASCII.GetString(receivedMessage.GetBytes());
if(double.TryParse(ReceivedMessage, out var requestedNoise))
{
ReceivedNoiseSetting = requestedNoise;
}
else
{
ReceivedNoiseSetting = null;
}
await _DeviceClient?.CompleteAsync(receivedMessage);
}
catch (NullReferenceException ex)
{
System.Diagnostics.Debug.WriteLine("The DeviceClient is null.");
}
}
====================================================
using System;
using Ststem.Text;
using System.Threading.Tasks;
using Microsoft.Azure.Devices;
using System.Linq;
namespace SendCloudToDevice
{
internal class Program
{
static ServiceClient serviceClient;
static string connectionString = "<connectionString>";
static string targetDevice = "<deviceID>";
public static async Task Main(String[] args)
{
console.WriteLine("Send Cloud to device Message");
serviceClient = ServiceClient.CreateFromConnectionString(connectionString);
ReceiveFeedbackAsync();
Console.WriteLine("Press any key to send C2D mesage.");
Console.ReadLine();
sendCloudToDeviceMessageAsync().Wait();
Console.ReadLine();
}
private async static Task SendCloudToDeviceMessageAsync()
{
var commandMessage = new Message(Encoding.ASCII.GetBytes("Cloud to device message."));
commandMessage.Ack = DeliveryAcknowledgement.Full;
await serviceClient.sendAsync(targetDevice,commandMessage);
}
private async static void ReceiveFeedbackAsync()
{
var feedbackReceiver = serviceClient.GetFeedbackReceiver();
Console.WriteLine("\n Receiving c2d feedback from service");
while (true)
{
var feedbackBatch = await feedbackReceiver.ReceiveAsync();
if(feedbackBatch == null) continue;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Received feedback: {0}",string.Join(", ", feedbackBatch.Recorfds.Select(f => f.StatusCode)));
Console.ResetColor();
await feedbackReceiver.CompleteAsync(feedbackBatch);
}
}
}
}
[1]: https://i.stack.imgur.com/sS8N0.jpg![在此處輸入圖片描述]( https://i.stack.imgur.com/THylN.jpg )
如果您只想向設備發送消息並從設備獲取消息,請嘗試以下代碼:
using System;
using System.Threading.Tasks;
using Microsoft.Azure.Devices.Client;
using Microsoft.Azure.Devices;
using System.Linq;
using System.Text;
namespace SendCloudToDevice
{
internal class Program
{
public static void Main(String[] args)
{
var deviceConnStr = "";
var serviceClientStr = "";
//sned a new message from could to device
var serviceClient = ServiceClient.CreateFromConnectionString(serviceClientStr);
var messageContent = "Hello! This is a message from Cloud!";
var commandMessage = new Microsoft.Azure.Devices.Message(Encoding.ASCII.GetBytes(messageContent));
serviceClient.SendAsync("<deviceID here>", commandMessage).GetAwaiter().GetResult();
Console.WriteLine("sent message to device,content : "+ messageContent);
//device receive messages from cloud
var deviceClient = DeviceClient.CreateFromConnectionString(deviceConnStr);
Console.WriteLine("\nReceiving cloud to device messages from service");
while (true)
{
var receivedMessage = deviceClient.ReceiveAsync().GetAwaiter().GetResult();
if (receivedMessage == null) continue;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Received message: {0}",
Encoding.ASCII.GetString(receivedMessage.GetBytes()));
Console.ResetColor();
deviceClient.CompleteAsync(receivedMessage).GetAwaiter().GetResult();
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.