如何将字符串与传入消息进行比较
How to do a string compare with the incoming message
我知道这听起来很简单,但我遇到了一些麻烦。我正在尝试制作一个带有 pic 微控制器 (MCU) 和 xamarin android 应用程序的系统。从应用程序到 pic MCU 的发送部分已解决,但是当我想将数据从 MCU 发送到应用程序时,它不会完美无缺。我正在使用 HC-06 作为蓝牙设备来接收和发送消息。
MCU接收到APP的代码为:
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(勇气),当比较与传入的消息一起工作时,我想调试写入是成功的:
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);
因为我确实注意到输入顺序错误。这确实有助于将其按正确的顺序排列。
小更新:
我更改了一些代码行,它工作得更好"flaweless"
while ((count = inStream.Read(buffer, 0, buffer.Length)) > 0)
但奇怪的是第一位与接收字符串的其余部分分开了。如果有人有想法,我不确定是什么导致了这个问题?
提前致谢。
我找到了解决我面临的问题的方法。所以我会分享我的答案和思考过程,这样也许其他人也可以使用。
所以我认为有一个接收缓冲区可以保存传入的字符。如果使用 streamReader.Read 读取缓冲区,它 returns 读取的字符的整数。所以我制作了数据类型 string[] 的第二个缓冲区。
如果字符串[0] 为空,我将放置在streamReader.Read 读取的第一个字符中。如果字符串 [0] 不为空,则意味着第一个字符已被读取,因此我将传入的字符放入字符串 [1] 中。这意味着被拆分的消息现在分为 string[0] 和 string[1]。那么,如果我可以将它组合起来并将其保存到一个字符串变量中呢?这是由以下人员完成的:string eindtekst = string.Join("", buf);
这给了我一个字符串,所以我可以比较它。在完成比较时清除两个数组很重要,否则会添加新数据。你可能会说 string[0] == null 永远不会是真的。所以只有 string[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;
}
}
}
感谢大家的帮助
我知道这听起来很简单,但我遇到了一些麻烦。我正在尝试制作一个带有 pic 微控制器 (MCU) 和 xamarin android 应用程序的系统。从应用程序到 pic MCU 的发送部分已解决,但是当我想将数据从 MCU 发送到应用程序时,它不会完美无缺。我正在使用 HC-06 作为蓝牙设备来接收和发送消息。
MCU接收到APP的代码为:
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(勇气),当比较与传入的消息一起工作时,我想调试写入是成功的:
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);
因为我确实注意到输入顺序错误。这确实有助于将其按正确的顺序排列。
小更新:
我更改了一些代码行,它工作得更好"flaweless"
while ((count = inStream.Read(buffer, 0, buffer.Length)) > 0)
但奇怪的是第一位与接收字符串的其余部分分开了。如果有人有想法,我不确定是什么导致了这个问题?
提前致谢。
我找到了解决我面临的问题的方法。所以我会分享我的答案和思考过程,这样也许其他人也可以使用。
所以我认为有一个接收缓冲区可以保存传入的字符。如果使用 streamReader.Read 读取缓冲区,它 returns 读取的字符的整数。所以我制作了数据类型 string[] 的第二个缓冲区。
如果字符串[0] 为空,我将放置在streamReader.Read 读取的第一个字符中。如果字符串 [0] 不为空,则意味着第一个字符已被读取,因此我将传入的字符放入字符串 [1] 中。这意味着被拆分的消息现在分为 string[0] 和 string[1]。那么,如果我可以将它组合起来并将其保存到一个字符串变量中呢?这是由以下人员完成的:string eindtekst = string.Join("", buf);
这给了我一个字符串,所以我可以比较它。在完成比较时清除两个数组很重要,否则会添加新数据。你可能会说 string[0] == null 永远不会是真的。所以只有 string[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;
}
}
}
感谢大家的帮助