串口数据接收

moyue008 2009-08-20 07:31:01
新手请教高手,为何我的port1_DataReceived 不被调用呢,串口另一边传值过来了啊,这么接收不到呢 , 高手指点下
谢谢,急........

{
InitializeComponent();

port1.BaudRate = 1200;//波特率
port1.Parity = Parity.Odd;//无奇偶校验位
port1.StopBits = StopBits.One;//两个停止位
port1.Handshake = Handshake.None;//控制协议
port1.DataBits = 7;
port1.DataReceived+= new System.IO.Ports.SerialDataReceivedEventHandler(port1_DataReceived);
OpenPort();
}
//打开串口的方法
public void OpenPort()
{
try
{
port1.Open();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
if (port1.IsOpen)
{
MessageBox.Show("COM1" + "开的!");
}
else
{
MessageBox.Show("COM1" + "关的!");
}
}

//关闭串口的方法
public void ClosePort()
{
port1.Close();
if (!port1.IsOpen)
{
Console.WriteLine("the port is already closed!");
}
}
private void port1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
try
{
MessageBox.Show("测试3");
StringBuilder currentline = new StringBuilder();
//循环接收数据
MessageBox.Show(port1.BytesToRead + "");
while (port1.BytesToRead > 0)
{
char ch = (char)port1.ReadByte();
currentline.Append(ch);
}
//在这里对接收到的数据进行处理
//
MessageBox.Show(port1.BytesToRead.ToString());
textBox1.Text = currentline.ToString();//currentline.ToString();
MessageBox.Show("测试1");

}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString() + "测试2");
}
}
...全文
258 37 打赏 收藏 转发到动态 举报
写回复
用AI写文章
37 条回复
切换为时间正序
请发表友善的回复…
发表回复
24K純帥 2009-08-21
  • 打赏
  • 举报
回复
up
jwh2004 2009-08-21
  • 打赏
  • 举报
回复
楼主还是花40元买个usb转RS232转换线,接到笔记本上用VS环境调试一下程序吧。
具体源码可以下个参考程序,地址:
http://download.csdn.net/source/214378
jhdxhj 2009-08-21
  • 打赏
  • 举报
回复
ding
shui8iuhs 2009-08-21
  • 打赏
  • 举报
回复
你一步一步调试一下,设个断点看看也,看是哪里报的错。
chaozi_249 2009-08-21
  • 打赏
  • 举报
回复
波特率问题,如果其他都正常的话,波特率不同,就接受不了数据。。
wdgphc 2009-08-21
  • 打赏
  • 举报
回复
你有没有这句:

port1.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(this.port1_DataReceived);
ccc0009 2009-08-21
  • 打赏
  • 举报
回复
usb转换线接收的字节数组还是有点区别的,
dai78 2009-08-21
  • 打赏
  • 举报
回复
没有设置接收多少数据触发事件啊
zbking 2009-08-21
  • 打赏
  • 举报
回复
请确认下2边串口的通讯参数是一致的。
灯火斑斓 2009-08-20
  • 打赏
  • 举报
回复
你是从工具栏中拖SerialPort到项目的主窗体的吗?总觉得你的代码怪怪的。你已经确认你的代码已经正确打开串口监听了吗?
moyue008 2009-08-20
  • 打赏
  • 举报
回复
额 照做了 没用 可惜问题出在 没进serialPort1_DataReceived方法 。 谢谢高手指点。
灯火斑斓 2009-08-20
  • 打赏
  • 举报
回复
首先,你的应用程序的监听串口的参数设置要同发送端的串口参数设置一样。
其次,按照下面的技术要点来实现。

技术要点:
(1).首先,SerialPort的ReceivedBytesThreshold先设置成1,表示只要有1个字符送达端口时便触发DataReceived事件
(2).当DataReceived触发时,先把ReceivedBytesThreshold设置成一个比较大的值,达到读取本次端口数据时,不再触发DataReceived.
(3).循环读取端口中的数据,直至读完。
(4).移除读取数据中的非法字符。
(5).触发一个后台线程处理收到的数据。
(6).在finally中把ReceivedBytesThreshold重置回1

private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
if (sender.GetType() != typeof(System.IO.Ports.SerialPort))
{
return;
}
string strReceive = string.Empty;
string strCollect = string.Empty;
System.IO.Ports.SerialPort comPort = (System.IO.Ports.SerialPort)sender;

try
{
comPort.ReceivedBytesThreshold = comPort.ReadBufferSize;
while (true)
{
strReceive = comPort.ReadExisting();
if (string.Equals(strReceive, string.Empty))
{
break;
}
else
{
strCollect += strReceive;
Application.DoEvents();
Thread.Sleep(100);
}
}
strCollect = strCollect.Replace("\0", string.Empty);
strCollect = strCollect.Replace("\r\n", string.Empty);
strCollect = strCollect.Replace("\r", string.Empty);
strCollect = strCollect.Replace("\n", string.Empty);

if (!this.bIsHandleCom)
{
this.bIsHandleCom = true;
mReceiveData = strCollect;
if (ReceiveDataParserEvent != null)
ReceiveDataParserEvent(mReceiveData);
if (ThreadReceiveParser != null && !ThreadReceiveParser.IsAlive)
{
ThreadReceiveParser.Start();
}
}

}
catch (Exception ex)
{
MessageBox.Show(this, ex.ToString(), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
comPort.ReceivedBytesThreshold = 1;
}
}
moyue008 2009-08-20
  • 打赏
  • 举报
回复
加了
ttianqq 2009-08-20
  • 打赏
  • 举报
回复
38239612 加我QQ
moyue008 2009-08-20
  • 打赏
  • 举报
回复
我就是在测试工程里试的 没任何多余的杂项...
ttianqq 2009-08-20
  • 打赏
  • 举报
回复
看了一下,没什么问题

你如果新建一个测试工程,就是打开串口,然后接收,看看行不行
moyue008 2009-08-20
  • 打赏
  • 举报
回复
看不到图片 点属性进网页看下吧。
moyue008 2009-08-20
  • 打赏
  • 举报
回复
ttianqq 2009-08-20
  • 打赏
  • 举报
回复
不是有插入图片么,要不你就大概写写

感觉应该不是什么大问题,估计就是哪设置不对造成的
moyue008 2009-08-20
  • 打赏
  • 举报
回复
没人能看出代码有啥问题吗, 急啊。。。
那个 图片我做好了 没地方上传。
加载更多回复(17)

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧