串行COM端口仅在调试中有效 [英] Serial COM port works only in debug

查看:85
本文介绍了串行COM端口仅在调试中有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从COM端口读取数据。我使用断点时工作正常。没有我得不到任何回复。



这是我的代码。

  public   void  comport_DataReceived( object  sender,SerialDataReceivedEventArgs e)
{
SerialPort comport =(SerialPort)sender;

var bytes = comport.BytesToRead;

// 创建一个字节数组缓冲区来保存传入数据
var buffer = new byte [字节];

// 从端口读取数据并将其存储在缓冲区中
comport.Read(buffer, 0 ,bytes);

strReceived = ConvertHex(BitConverter.ToString(buffer));

SetText(strReceived.ToString());

}



提前致谢

解决方案

更好地调用父代表。



  private   void  rs_DataReceived( object  sender,System.IO.Ports.SerialDataReceivedEventArgs e)
{
string data = ;

if (rs.BytesToRead > 0
{
try
{
data = rs.ReadLine();
if (Parent!= null && [my_delegate] != null
{
Parent.Invoke( [my_delegate] new Object [] {data});
}
}
catch (例外情况)
{
// 错误
}
}
}






我已经尝试过你给出的解决方案。但行为仍然是一样的。


你的问题告诉我们的很少。此代码的工作原理如下: SerialComms.Manager - 串行通信用于.NET的插件 [ ^ ]



  public   void  comport_DataReceived(object sender,SerialDataReceivedEventArgs e)
{
SerialPort comport =(SerialPort)sender;

if (!comport.IsOpen)
return ;

var bytes = comport.BytesToRead;

// 创建一个字节数组缓冲区来保存传入数据
var buffer = new byte [bytes];

// 从端口读取数据并将其存储在缓冲区中
int bytesread = comport.Read(buffer, 0 ,bytes);

if (bytesread> 0
strReceived =编码.ASCII.GetString(buffer, 0 ,bytesread);

}





使用调试器,您可以检查是否获得数据,然后它应该在调试或发布中工作


I want to read data from COM Port. its working fine when i use breakpoints. without i couldn't get any response .

Here is my code.

public  void comport_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
            SerialPort comport = (SerialPort)sender;

            var bytes = comport.BytesToRead;

            // Create a byte array buffer to hold the incoming data
            var buffer = new byte[bytes];

            // Read the data from the port and store it in our buffer
            comport.Read(buffer, 0, bytes);

            strReceived = ConvertHex(BitConverter.ToString(buffer));            
                          
            SetText(strReceived.ToString());
           
}


Thanks in advance

解决方案

Better Invoke a parent's delegate.

private void rs_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
	string data="";

	if (rs.BytesToRead > 0)
	{
		try
		{
			data = rs.ReadLine();
			if (Parent != null && [my_delegate] != null)
			{
				Parent.Invoke([my_delegate], new Object[] { data });
			}
		}
		catch (Exception ex)
		{
			// error
		}
	}
}


Hi,

I have tried the solution you given .But the behaviour is same still.


Your question tells us very little. This code works as it comes from here:SerialComms.Manager - A serial communications plug-in for .NET[^]

public  void comport_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
            SerialPort comport = (SerialPort)sender;

           if (!comport.IsOpen)
                return;

            var bytes = comport.BytesToRead;

            // Create a byte array buffer to hold the incoming data
            var buffer = new byte[bytes];

            // Read the data from the port and store it in our buffer
            int bytesread = comport.Read(buffer, 0, bytes);

            if (bytesread > 0)
                strReceived = Encoding.ASCII.GetString(buffer, 0, bytesread);

}



With the debugger you can check that you get data then it should work in debug or release.


这篇关于串行COM端口仅在调试中有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆