C# 如何将字符串与传入消息进行比较
我知道这听起来很简单,但我遇到了一些麻烦。我正在尝试用pic微控制器(MCU)和xamarin android应用程序制作一个系统。从应用程序到pic MCU的发送部分已经解决了,但是当我想从MCU向应用程序发送数据时,它并不是完美的。我使用HC-06作为蓝牙设备来接收和发送信息 从MCU接收到应用程序的代码为:C# 如何将字符串与传入消息进行比较,c#,android,xamarin,bluetooth,C#,Android,Xamarin,Bluetooth,我知道这听起来很简单,但我遇到了一些麻烦。我正在尝试用pic微控制器(MCU)和xamarin android应用程序制作一个系统。从应用程序到pic MCU的发送部分已经解决了,但是当我想从MCU向应用程序发送数据时,它并不是完美的。我使用HC-06作为蓝牙设备来接收和发送信息 从MCU接收到应用程序的代码为: public void beginListenForData() { try { inStream = btSock
public void beginListenForData()
{
try
{
inStream = btSocket.InputStream;
}
catch (IOException ex)
{
Console.WriteLine(ex.Message);
}
Task.Factory.StartNew(() => {
byte[] buffer = new byte[1024];
int bytes;
while (true)
{
try
{
Array.Reverse(buffer, 0, buffer.Length);
bytes = inStream.Read(buffer, 0, buffer.Length);
if (bytes > 0)
{
string valor = Encoding.ASCII.GetString(buffer);
System.Diagnostics.Debug.WriteLine(buffer);
System.Diagnostics.Debug.WriteLine(bytes);
System.Diagnostics.Debug.WriteLine(valor);
if (valor == "D0O")
{
System.Diagnostics.Debug.WriteLine("Vergelijking gelukt!");
break;
}
//Result.Text = Result.Text + "\n" + valor;
}
}
catch (Java.IO.IOException)
{
//Result.Text = string.Empty;
break;
}
}
});
}
您可能会发现,当比较与我要调试写入的传入消息成功进行时,我尝试从MCU发送的消息是D0O(valor):
System.Diagnostics.Debug.WriteLine("Vergelijking gelukt!");
下一部分用于检查输入的数据:
System.Diagnostics.Debug.WriteLine(buffer);
System.Diagnostics.Debug.WriteLine(bytes);
System.Diagnostics.Debug.WriteLine(valor);
我注意到的是奇怪的输出(见图):
正如您所看到的,消息每次都被分为两部分。有人知道为什么以及如何解决这个问题吗
我确实使用以下命令更改了数组顺序:
Array.Reverse(buffer, 0, buffer.Length);
因为我注意到它输入的顺序不对。这确实起到了作用,把它放在了正确的顺序上
小更新:
我修改了一些代码,它工作起来更“完美”
但奇怪的是,第一位与接收字符串的其余部分分离。如果有人有想法,我不确定是什么导致了这个问题
提前感谢。我找到了解决我所面临问题的方法。因此,我将分享我的答案和思考过程,或许其他人也可以使用同样的方法 所以我认为有一个接收缓冲区可以保存传入的字符。如果使用streamReader.read读取缓冲区,则返回读取字符的整数。所以我制作了数据类型字符串[]的第二个缓冲区 如果字符串[0]为空,我会将其放入streamReader.read读取的第一个字符中。如果字符串[0]不是空的,则表示第一个字符已被读取,因此我将传入的字符放入字符串[1]中。这意味着被拆分的消息现在分为字符串[0]和字符串[1]。那么,如果我可以将它合并并保存到一个字符串变量中呢。这是通过以下方式完成的:
string-eindtekst=string.Join(“,buf”)
这给了我一个完整的字符串,这样我就可以比较它了。在进行比较时,务必清除两个数组的值,否则会添加新数据。正如您可能知道的,字符串[0]==null永远不会为真。因此,始终只覆盖字符串[1]get,这意味着您正在丢失数据
public void beginListenForData()
{
try
{
inStream = btSocket.InputStream;
streamReader = new StreamReader(inStream);
}
catch (IOException ex)
{
Console.WriteLine(ex.Message);
}
char[] buffer = new char[256];
string[] buf = new string[2];
int bytes;
while (1)
{
try
{
if ((bytes = streamReader.Read(buffer, 0, buffer.Length)) > 0)
{
string tekst = new string(buffer, 0, bytes);
if(buf[0] == null)
{
buf[0] = tekst;
}
else
{
buf[1] = tekst;
}
string eindtekst = string.Join("", buf);
if (eindtekst == "D0O")
{
System.Diagnostics.Debug.WriteLine("Vergelijking gelukt!");
System.Diagnostics.Debug.WriteLine(eindtekst);
Array.Clear(buffer, 0, buffer.Length);
Array.Clear(buf, 0, buf.Length);
writeData("D2O");
}
streamReader.DiscardBufferedData();
}
}
catch (Java.IO.IOException)
{
break;
}
}
}
感谢所有的帮助我认为您不能假设通过这种接口发送的消息会以完整的块形式出现。重新组装传入的数据只是你的应用程序需要处理的任务之一。我确实知道你来自哪里,但我的感觉是,我制作了一个1023字节的字节缓冲区,我希望它能作为一个块保存在这个缓冲区中。但是,如果不是这样,您将如何重新组合传入消息,以便将其与预设消息进行比较?只需将字节附加在一起即可。您可能需要固定长度的消息或一些分隔符来分隔消息。我会尝试,但这很奇怪,因为当我发送大于3个字符的消息时,它会被分割成更大的数据块,这意味着可以在1个字符中发送更小的数据
public void beginListenForData()
{
try
{
inStream = btSocket.InputStream;
streamReader = new StreamReader(inStream);
}
catch (IOException ex)
{
Console.WriteLine(ex.Message);
}
char[] buffer = new char[256];
string[] buf = new string[2];
int bytes;
while (1)
{
try
{
if ((bytes = streamReader.Read(buffer, 0, buffer.Length)) > 0)
{
string tekst = new string(buffer, 0, bytes);
if(buf[0] == null)
{
buf[0] = tekst;
}
else
{
buf[1] = tekst;
}
string eindtekst = string.Join("", buf);
if (eindtekst == "D0O")
{
System.Diagnostics.Debug.WriteLine("Vergelijking gelukt!");
System.Diagnostics.Debug.WriteLine(eindtekst);
Array.Clear(buffer, 0, buffer.Length);
Array.Clear(buf, 0, buf.Length);
writeData("D2O");
}
streamReader.DiscardBufferedData();
}
}
catch (Java.IO.IOException)
{
break;
}
}
}