在C#中将字符写入com端口 [英] writing characters to com ports in c#
问题描述
大家好,我想知道是否有人可以帮我...
我是C#的新手,而且很难将后续字符写入COM端口.
我基本上需要发送一个用户输入的字符,然后在字符"x"之前稍作停顿.
我可以发送输入字符...或程序中定义的char,但是仅执行第一条指令,我不确定为什么.我尝试了几种方法,但似乎都没有解决问题的方法.
这是我的代码段,我真的很乐意提供帮助.
理想情况下,我希望程序等待将char输入到buff [0]中.将此字符发送到COM端口;发送"x"字符到通讯端口,然后等待下一个字符输入到buff [0]
尝试一个...
Hey guys, was wondering if somebody could give me a hand...
I''m relativly new to c# and I''m having difficulty writing subsequent characters to a com port.
I basically need to send a user-input character followed by a short pause before the char ''x''.
I can send either the input character...OR a char defined in the program but only the first instruction is executed and I''m not sure why. I''ve tried a few methods but none seem to do the trick...
Here''s snippets of my code, I''d really appriciate the help.
Ideally I''d like the program to wait for a char to be entered into buff[0]; send this character to com port; send ''x'' char to com port and then wait for the next character to be entered into buff[0]
attempt one...
char[] buff = new char[1];
char[] xbuff = new char[1];
buff[0] = e.KeyChar;
xbuff[0] = 'x';
serialPort1.Write(buff, 0, 1);
System.Threading.Thread.Sleep(500);
serialPort1.Write(xbuff, 0, 1);
System.Array.Clear(buff, 0, 1);
尝试两个...
attempt two...
string lettera = ((char)97).ToString();
string letterb = ((char)98).ToString();
serialPort1.Write(lettera);
System.Threading.Thread.Sleep(500);
serialPort1.Write(letterb);
由于两次尝试的某种原因,只会发送第一个字符.最近两天我一直在拔头发,真的很感谢任何想法
M.
For some reason on both attempts only the first char will be sent. I''ve been pulling my hair out for the last 2 days and would really be grateful for any ideas
M.
推荐答案
问题可能是因为您可能以字节数组而不是char发送数据.
无论如何,这可以为您提供帮助:点击
It''s possible that the problem comes because you may send data in byte arrays instead of char.
Anyway, this can help you: CLICK
Or this: CLICK
看看在本文中:
使用C#和Whidbey进行的串行通信 [
Take a look at this article:
Serial Communication using C# and Whidbey[^]
Regards
Espen Harlinn
采用了一种完全使用的方法,但它得到了排序...为回复打起精神
adopted a completly method but got it sorted...cheers for the reply
这篇关于在C#中将字符写入com端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!