serialPort SerialDataReceivedEventArgs正在读取空字节 [英] serialPort SerialDataReceivedEventArgs is reading empty bytes
问题描述
亲爱的开发人员,
首先简短介绍一下我在做什么:
我正在使用光学探针进行串行通信.探针装有FTDI芯片(虚拟ComPort),并且我的设备正在使用光电二极管发送数据. (每2秒大约持续350毫秒,在350毫秒之后,使用9V直流电压会中断,直到出现下一个电报'').
我正在使用SerialDataReceivedEvent处理数据.
Dear Developer,
first short what i''m doin'':
I''m using an optical probe for serial communication. The probe has got a FTDI Chip (virtual ComPort) and my Device is sending the data with an optical diode. (Every 2 seconds for about 350 ms and after the 350 ms there is an break with 9V DC voltage until the next telegram is comein'').
I''m using the SerialDataReceivedEvent to handle the data.
private void getSerialData(object sender, SerialDataReceivedEventArgs e)
活动开始后,我等待400毫秒
System.Threading.Thread.Sleep(400);
并读出所有数据
数据= serialPort.ReadExisting();
我的问题是,该事件每秒钟开始.第一个字符串是正常的,第二个字符串是"(什么都没有).我已经终止了来自光学探头的任何形式的光,并弄乱了设备正在发送的信号,除了正常的350ms电报之外,找不到任何东西.
你们中的某个人可能已经遇到了同样的问题,并且可以告诉我为什么下一个电报开始时第二个字符串为",为什么事件每隔一秒而不是第二秒开始?
After the event starts i''m waitin 400ms
System.Threading.Thread.Sleep(400);
and read out all the data
data = serialPort.ReadExisting();
My problem is, that the Event starts every second. The first string is normal, the second is "" (nothing). I already terminate any form of light from my optical probe and messure the signal my device is sending and can''t find anything excepted the normal 350ms telegram.
May someone of you already had the same problem and can tell my why every second string is "" and why the Event starts every second and not every 2nd second when the next telegram starts?
推荐答案
您是否正确检查了COM端口名称,波特率以及所有与串行通信有关的内容?
Have you properly checked COM Port name, baudrate and all that stuff relavant to serial communication ?
确定我已经完成了.
这是我将我的serialPort初始化的代码
Sure i''ve done.
Here is the code i''ve init my serialPort
private bool intiCom()
{
if (serialPort == null)
serialPort = new SerialPort();
serialPort.Handshake = Handshake.None;
serialPort.PortName = comport;
serialPort.BaudRate = baudrate;
serialPort.DataBits = databits;
// StopBits
if (stopbits == 1)
serialPort.StopBits = StopBits.One;
else if (stopbits == 1.5)
serialPort.StopBits = StopBits.OnePointFive;
else if (stopbits == 2)
serialPort.StopBits = StopBits.Two;
else
return false;
// Parity
if (parity == 1)
serialPort.Parity = Parity.Even;
else if (parity == 2)
serialPort.Parity = Parity.Mark;
else if (parity == 3)
serialPort.Parity = Parity.None;
else if (parity == 4)
serialPort.Parity = Parity.Odd;
else if (parity == 5)
serialPort.Parity = Parity.Space;
else
return false;
// RTS
serialPort.RtsEnable = true;
serialPort.Encoding = Encoding.ASCII;
try
{
serialPort.Open();
}
catch (Exception)
{
return false;
}
return true;
}
设置来自我的ini文件:)
And the settings came from my ini file :)
portname=4
baudrate=9600
databits=8
stopbits=1
parity=3
showsettings=0
showdisplay=1
tray=1
topmost=0
excelinstalled=1
excellang=german
saveinginterval=3600
scalebars=1
这篇关于serialPort SerialDataReceivedEventArgs正在读取空字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!