Reputation: 1
I just want to start off by saying im new to programming in C# and Visual Studio.
I have a project where I am using a telematics unit to send sensor data into my Azure IoT hub. I have the connection set up and I am trying to read the raw data coming from the telematics unit. I have also set up the Azure IoT hub trigger in Visual Studio using the Azure function, IoT Hub Trigger v2.
See below for the code:
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Azure.EventHubs;
using System.Text;
using System.Net.Http;
using Microsoft.Extensions.Logging;
namespace Teltonika_trigger_v3
{
public static class Function1
{
private static HttpClient client = new HttpClient();
[FunctionName("Function1")]
public static void Run([IoTHubTrigger("messages/events", Connection = "EventHubConnection")]EventData message, ILogger log)
{
log.LogInformation($"C# IoT Hub trigger function processed a message: {Encoding.UTF8.GetString(message.Body.Array)}");
}
}
}
I am using a Teltonika telematics unit and I know that they use a special encoding called Codec8 (https://wiki.teltonika-gps.com/view/Codec)
Initially I am just interested in reading the raw bytes being sent to my IoT hub without any translation, how do I do this?
With the code above I get some very weird results
I have tried removing the Encoding.UTF8.GetString
part but this just shows the data as "System.Byte[]".
How do I read the raw data coming into my IoT hub?
Upvotes: -1
Views: 386
Reputation: 4085
By calling Encoding.UTF8.GetString(message.Body.Array)
, you're telling the Function to kindly take these bytes and treat them as Unicode, hence 'weird results'. If you just want to get the byte values as text, check out this StackOverflow answer.
Upvotes: 0