using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading; using System.Threading.Tasks; namespace MqttClient { class Program { [DllImport("DataTransferDll.dll", EntryPoint = "SetLogFileDir", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)] public static extern int SetLogFileDir(string logFileDir); [DllImport("DataTransferDll.dll", EntryPoint = "Open", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)] public static extern int Open(string a, int b); [DllImport("DataTransferDll.dll", EntryPoint = "Write", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)] public static extern int Write(string id, string value); // 配合使用ToUTF8,发送带中文的value [DllImport("DataTransferDll.dll", EntryPoint = "Write", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)] public static extern int Write(string id, byte[] value); [DllImport("DataTransferDll.dll", EntryPoint = "WriteWithDataId", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)] public static extern int WriteWithDataId(string id, string value, string dataId); // 配合使用ToUTF8,发送带中文的value [DllImport("DataTransferDll.dll", EntryPoint = "WriteWithDataId", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)] public static extern int WriteWithDataId(string id, byte[] value, string dataId); [DllImport("DataTransferDll.dll", EntryPoint = "SetCallbackWithDataId", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)] private static extern void SetCallbackWithDataId(CallbackWithDataIdDelegate callback); [DllImport("DataTransferDll.dll", EntryPoint = "Close", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)] public static extern int Close(); [UnmanagedFunctionPointer(CallingConvention.Cdecl)] public delegate void CallbackWithDataIdDelegate(string id, string value, string dataId); [DllImport("DataTransferDll.dll", EntryPoint = "ParameterConfig", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)] public static extern int ParameterConfig(string parameterJson); static public void CallbackWithDataId(string id, string v, string dataId) { Console.WriteLine("-------CallbackWithDataId-------"); byte[] buffer1 = Encoding.Default.GetBytes(v); byte[] buffer2 = Encoding.Convert(Encoding.UTF8, Encoding.Default, buffer1, 0, buffer1.Length); string strBuffer = Encoding.Default.GetString(buffer2, 0, buffer2.Length); Console.WriteLine("{0} -> {1} {2}", id, strBuffer, dataId); } public static byte[] ToUTF8(string text) { byte[] buffer1 = Encoding.Unicode.GetBytes(text); byte[] buffer2 = Encoding.Convert(Encoding.Unicode, Encoding.UTF8, buffer1, 0, buffer1.Length); return buffer2; } static void Main(string[] args) { string ip_addr = "127.0.0.1"; int ip_port = 6666; // 连接MQTT工具 Open(ip_addr, ip_port); // 设置回调函数 SetCallbackWithDataId(CallbackWithDataId); // 参数配置 string parameterJson = "{\r\n \"function\":\"parameterConfig\",\r\n \"redId\":\"123456\",\r\n \"parameter\":{\r\n \"mqtt\":{\r\n \"address\":\"staging-cnbj2-rmq-mqtt.api.xiaomi.com\",\r\n \"port\":\"80\",\r\n \"username\":\"AKMO5BGFQUZL2SBW4X\",\r\n \"password\":\"5AChJjCOYB+No68BpyDocd7uR7cv/foE20RKIpOE\"\r\n },\r\n \"equiment\":{\r\n \"factoryCode\":\"\",\r\n \"deviceCode\":\"DIGITION-TEST\",\r\n \"stationCode\":\"\",\r\n \"project\":\"\",\r\n \"productMode\":\"debug\"\r\n },\r\n \"other\":{\r\n \"logLevel\":\"0\",\r\n \"runMode\":\"online\",\r\n \"uploadDigitalTwinData\":\"true\"\r\n }}}"; ParameterConfig(parameterJson); Thread.Sleep(3000); for (int i = 0; i < 10; i++) { string msg = "{\"beat_tm\":\"2023-03-31 13:54:27.937\",\"signal_name\":\"中文\",\"signal_type\":\"DI\",\"target_status\":true}"; // 写入ID、值 int ret = Write("beat_log/control/device_control_signal/DT_motionbeat", ToUTF8(msg)); Console.WriteLine("第{0}次,ret = {1}", i, ret); Thread.Sleep(50); } Console.WriteLine("请按任意键继续..."); Console.ReadKey(); // 关闭连接 Close(); } } }