串口发送接收数据信息(接收不到信息超时)

zoushunliang 2009-10-26 02:52:23
我写了个发送接收程序,不知道哪里错了,我也刚了解这个,请各位大虾帮我找找问题出在哪?谢谢
急!!!!!!!!!!!



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.IO.Ports;

namespace Serialexpample
{
partial class Form1 : Form
{
PropertyPage pp = new PropertyPage();

SerialPort sp = new SerialPort();

public Form1()
{
InitializeComponent();
}

private void propertyButton_Click(object sender, EventArgs e)
{
pp.ShowDialog();

propertyButton.Hide();
}

private void sendButton_Click(object sender, EventArgs e)
{
try
{
sp.WriteLine(textBox.Text);//发送数据
textBox.Text = "";
}
catch (System.Exception ex)
{
baudRatelLabel.Text = ex.Message;
}

}

private void ReadButton_Click(object sender, EventArgs e)
{
try
{
textBox.Text = "";
textBox.Text = sp.ReadLine();//接受数据
}
catch(System.Exception ex)
{
baudRatelLabel.Text = ex.Message;
}
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
MessageBox.Show("Do u want to Close the App");
sp.Close();
}

private void startCommButton_Click(object sender, EventArgs e)
{
startCommButton.Hide();
sendButton.Show();
ReadButton.Show();
textBox.Show();
}

private void saveStatusButton_Click_1(object sender, EventArgs e)
{
if (pp.bRate == "" && pp.sBits == "")
{
dataBitLabel.Text = "BaudRate = " + sp.BaudRate.ToString();//设置串口通讯波特率
readTimeOutLabel.Text = "StopBits = " + sp.StopBits.ToString();//设置串口通讯停止位
}
else
{
dataBitLabel.Text = "BaudRate = " + pp.bRate;
readTimeOutLabel.Text = "StopBits = " + pp.sBits;
}

parityLabel.Text = "DataBits = " + sp.DataBits.ToString();//设置串口通讯数据位
stopBitLabel.Text = "Parity = " + sp.Parity.ToString();//设置串口通讯奇偶校验位
readTimeOutLabel.Text = "ReadTimeout = " +
sp.ReadTimeout.ToString();

if (propertyButton.Visible == true)
propertyButton.Hide();
saveStatusButton.Hide();
startCommButton.Show();

try
{
sp.Open();
sp.ReadTimeout = 500;
}
catch (System.Exception ex)
{
baudRatelLabel.Text = ex.Message;
}
}

}
}
...全文
438 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
jdhlowforever 2009-10-27
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 zoushunliang 的回复:]
大家有类似的案例程序吗?
[/Quote]
我有,加我QQ: 10906266
wudliang 2009-10-27
  • 打赏
  • 举报
回复
将你的代码改了一下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.IO.Ports;

namespace Serialexpample
{
partial class Form1 : Form
{
PropertyPage pp = new PropertyPage();

SerialPort sp = new SerialPort();

public Form1()
{
InitializeComponent();
sp.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
}

//响应串口接收到数据事件。
private void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
string tmp = sp.ReadExisting(); //读取接收到的数据。
}
catch { }

//防止文本框溢出
int n = textBox.Text.Length;
int m = tmp.Length;
if (n + m > 32000)
textBox.Text = textBox.Text.Substring(m, n - m);

textBox.AppendText(tmp);
textBox.Select(textBox.Text.Length, 0);
}

private void propertyButton_Click(object sender, EventArgs e)
{
pp.ShowDialog();

propertyButton.Hide();
}

private void sendButton_Click(object sender, EventArgs e)
{
try
{
sp.WriteLine(textBox.Text);//发送数据
textBox.Text = "";
}
catch (System.Exception ex)
{
baudRatelLabel.Text = ex.Message;
}

}

//private void ReadButton_Click(object sender, EventArgs e)
//{
// try
// {
// textBox.Text = "";
// textBox.Text = sp.ReadLine();//接受数据
// }
// catch (System.Exception ex)
// {
// baudRatelLabel.Text = ex.Message;
// }
//}

private void Form1_Load(object sender, EventArgs e)
{

}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
MessageBox.Show("Do u want to Close the App");
sp.Close();
}

private void startCommButton_Click(object sender, EventArgs e)
{
startCommButton.Hide();
sendButton.Show();
ReadButton.Show();
textBox.Show();
}

private void saveStatusButton_Click_1(object sender, EventArgs e)
{
if (pp.bRate == "" && pp.sBits == "")
{
dataBitLabel.Text = "BaudRate = " + sp.BaudRate.ToString();//设置串口通讯波特率
readTimeOutLabel.Text = "StopBits = " + sp.StopBits.ToString();//设置串口通讯停止位
}
else
{
dataBitLabel.Text = "BaudRate = " + pp.bRate;
readTimeOutLabel.Text = "StopBits = " + pp.sBits;
}

parityLabel.Text = "DataBits = " + sp.DataBits.ToString();//设置串口通讯数据位
stopBitLabel.Text = "Parity = " + sp.Parity.ToString();//设置串口通讯奇偶校验位
readTimeOutLabel.Text = "ReadTimeout = " +
sp.ReadTimeout.ToString();

if (propertyButton.Visible == true)
propertyButton.Hide();
saveStatusButton.Hide();
startCommButton.Show();

try
{
sp.ReadTimeout = 500;
sp.Open();
}
catch (System.Exception ex)
{
baudRatelLabel.Text = ex.Message;
}
}

}
}
zoushunliang 2009-10-27
  • 打赏
  • 举报
回复
大家有类似的案例程序吗?
zoushunliang 2009-10-26
  • 打赏
  • 举报
回复
默认的不行吗?不是可以不设置嘛!~
freeboy827 2009-10-26
  • 打赏
  • 举报
回复
没有看到你设置了串口的参数啊,

110,561

社区成员

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

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

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