gpt4 book ai didi

C# 读取HEX串口数据并发送给Streamwriter

转载 作者:行者123 更新时间:2023-12-03 05:35:51 28 4
gpt4 key购买 nike

我正在尝试从以十六进制发送数据的设备读取串行数据,然后将其写入到接收器应用程序的 azure 中继流写入器。设备正在发送这些十六进制值:16 51 1D 65 D4 A6然而我的程序似乎正在阅读:

:enter image description here

如何让串行端口读取十六进制数据并通过流写入器发送它?我在下面附上了我的代码。感谢您的帮助!

// Read from the serial and write to the hybrid connection.
var writes = Task.Run(async () => {
// var reader = Console.In;
var writer = new StreamWriter(relayConnection) { AutoFlush = true };


do
{
// Read serial data from the serial port.
string line = sp.ReadExisting();
// Write the line to the hybrid connection (Azure Relay)
if(line.ToString() != "")
{
await writer.WriteLineAsync(line);

}

//cancel connection and break loop
if (Main.disconnect == true)
{
break;
}

}
while (true);
});

最佳答案

您可以将其读取为字节数组而不是文本:

int length = sp.BytesToRead;
byte[] buf = new byte[length];

sp.Read(buf, 0, length);
System.Diagnostics.Debug.WriteLine("Received Data:" + buf);

关于C# 读取HEX串口数据并发送给Streamwriter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62743773/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com