我正在流式传输来自设备的数据,我需要找到一种方法来捕获“行结束”符号结束分隔行的符号,就像任何软件一样,超级终端。但是我听不懂这个符号。
现在我收到的流看起来是这样的:
00 00 )0 00 00 )0 00 00 )0 00 00 )0 00 00 )0 00 00 )0 00 00 )0 00 00 )0 00 00 )0 00 00 )0 00 00 )0 00 00 )0 00 00 )0 0000 )0 00 00 )0 00 00 )0 00 00 )0 00 00 )0 00 00 )0 00 00 )0 00 00 )0 00 00 )0 00 00 )0 00 00 )0 00 00 )0 0000 )0 00 00 )0 00 00 )0 00 00 )0 00 00
但我需要它在我的windows窗体的textBox中看起来像这样:
)0 00 00
)0 00 00
)0 00 00
)0 00 00
)0 00 00
)0 00 00
我已经编写了一段代码,当它在其中找到特定的字符串时,它将分离流字符串,但“绑定”该字符串对我来说很棘手。我知道应该是ASCII字符,但我找不到如何将它编码到我的程序中并使用它。
附注:实际上,当我简单地将字符"“复制到特定的texBox中,然后应用于变量并在程序中使用时,它可以工作,但它看起来移动的方式是错误的,因为我不能直接将它粘贴到例如字符串变量中的Visual Studio中。
我使用此事件代码从串行端口接收数据:
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
try
{
this.Invoke(new EventHandler(write_to_field));
}
catch (ObjectDisposedException ex)
{
MessageBox.Show(Convert.ToString(ex), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (UnauthorizedAccessException ex)
{
MessageBox.Show(Convert.ToString(ex), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (ArgumentOutOfRangeException ex)
{
MessageBox.Show(Convert.ToString(ex), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (ArgumentException ex)
{
MessageBox.Show(Convert.ToString(ex), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (InvalidOperationException ex)
{
MessageBox.Show(Convert.ToString(ex), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}这是我使用的方法:
public void write_to_field(object sender, EventArgs e)
{
String stringToShow;
String delimiter = textBox4.Text;
if (textBox5.Text.Length <= Convert.ToInt16(textBox3.Text))
{
String DispString;
String toFind = textBox4.Text;
DispString = serialPort1.ReadExisting();
int index = DispString.IndexOf(toFind);
textBox5.AppendText(DispString);
if (index != 0)
{
textBox5.AppendText(DispString.Remove(0, 1));
}
}
else
{
stringToShow = textBox5.Text;
textBox6.Text = stringToShow;
textBox5.Text = "";
}
}我认为它离完美还很远,所以我也非常感谢大家对它的建议。
在一个建议之后,我重新编写了代码,如下所示:
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
write_to_field();
}
public void write_to_field()
{
serialPort1.NewLine = "/r/n";
try
{
textBox1.Text = serialPort1.ReadLine();
}
catch (TimeoutException ex)
{
MessageBox.Show(Convert.ToString(ex), "Some title", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (System.IO.IOException ex)
{
MessageBox.Show(Convert.ToString(ex), "Some title", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (ObjectDisposedException ex)
{
MessageBox.Show(Convert.ToString(ex), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (UnauthorizedAccessException ex)
{
MessageBox.Show(Convert.ToString(ex), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (ArgumentOutOfRangeException ex)
{
MessageBox.Show(Convert.ToString(ex), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (ArgumentException ex)
{
MessageBox.Show(Convert.ToString(ex), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (InvalidOperationException ex)
{
MessageBox.Show(Convert.ToString(ex), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}但是有System.IO.IOException https://msdn.microsoft.com/en-us/library/ms164917.aspx意味着没有什么可读的。但这可能是因为Hyperterm可以读取它。
我试着使用ReadLinewhen,我开始了这个项目,但它不能很好地工作。我到处都找不到好的解决方案。这里会有什么问题呢?
发布于 2017-09-04 18:37:14
谢谢您,或者提供您的实现的更多细节。
基于此,我看到您正在调用serialPort1.ReadExisting()方法。这将读取存储在serialPort缓冲区中的所有现有数据。您可以尝试指定换行符,并将其作为NewLine属性添加到SerialPort中。然后遍历SerialPort.ReadLine方法,该方法只读取缓冲区中的数据,直到命中下一个NewLine字符串。
有关更多详细信息,请参阅文档:NewLine、ReadLine with example
编辑:
根据ReadLine方法的文档,ReadLine方法应该只抛出InvalidOperationException和TimeoutException。当抛出IOException时,我认为可能还有其他问题。异常消息说什么?堆栈跟踪是什么?有没有Inner Exceptions
https://stackoverflow.com/questions/46034673
复制相似问题