字符串和COM端口 [英] Strings and COM ports
问题描述
大家好,
感觉有点厚脸皮,因为我得到了答案,但终点线已经在眼前,我只有一点障碍可以克服.
我有这段代码(由mark ... legend提供)在一个字符串输入的字符之间放置一个"x"字符(不要问为什么!).
Hey guys,
Feel a little cheeky because I got an answer but the finishing line is in sight and I just have one little hurdle to get over.
I have this code (supplied by mark...legend) to put an ''x'' character between chars entered on a string (don''t ask why!).
StringBuilder sb = new StringBuilder();
foreach (Char c in textBox1.Text)
{
sb.Append(c);
if (!Char.IsWhiteSpace(c))
{
sb.Append("x");
serialPort1.Write(sb.ToString());
}
}
textBox2.Text = sb.ToString();
剩下要做的就是分别将这些字符发送到com端口.到目前为止,我可以从按键或存储的变量中发送单个字符,但是我需要某种循环来逐个发送上述字符串(sb).
我尝试了很多方法,可以列出它们,但是我似乎没有任何成功.
M
All is left to do is to send these characters to a com port individually. So far I can send a single char from a keypress or stored variable, but i need some kind of loop to send the above string (sb) one by one.
I''ve tried so many methods, and can list them, but I don''t seem to be having any success.
M
推荐答案
这是一种方法:
This is one way:
string x = sb.ToString();
foreach (char ch in x)
{
seriealPort1.Write(ch);
}
或者,使用您的示例:
Or, to use your sample:
StringBuilder sb = new StringBuilder();
foreach (Char c in textBox1.Text)
{
sb.Append(c);
serialPort1.Write(c.ToString());
if (!Char.IsWhiteSpace(c))
{
sb.Append("x");
serialPort1.Write("x");
}
}
textBox2.Text = sb.ToString();
[/EDIT]
[/EDIT]
这篇关于字符串和COM端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!