通过串口延长器延长串口通讯,使用串口助手可以接收到数据,而我自己写的程序却不可以
清风树下 2010-10-28 09:45:19 通过串口延长器延长串口通讯,使用串口助手可以接收到数据,而我自己写的程序却不可以,接收到的数据是不完整的,,,,谁能帮帮我!!!谢谢大家了
我的接收数据和发送数据部份代码。
定义:
public SerialPort SeriaP;//串口连接
SeriaP = new SerialPort(PostName, baudRate, parity, DataBits, stopBits);
public System.Timers.Timer CTimer = new System.Timers.Timer();//实时监控连接的状态
this.SeriaP.ReadBufferSize = 2048;
this.CTimer.Interval = 1000;
this.CTimer.AutoReset = true;
this.CTimer.Elapsed += new ElapsedEventHandler(Timer_Elapsed);
发送数据部份:
/// <summary>
/// 发送字符串数据
/// </summary>
/// <param name="StrTxt">发送数据</param>
public void Send(string StrTxt)
{
try
{
if (this.IsConn)
{
//this.SeriaP.WriteLine(StrTxt);
this.SeriaP.Write(StrTxt);
}
else
{
throw (new Exception("发送信息时连接异常断开"));
}
}
catch (Exception Ex)
{
//出错处理
}
}
接收数据:
private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
try
{
if (this.IsConn) //串口是否打开 true为打开
{
int Int = 0;
Int = this.SeriaP.BytesToRead;
if (Int > 0)
{
this.ClientDTConnect = DateTime.Now;
byte[] Byte = new byte[Int];
this.SeriaP.Read(Byte, 0, Int);
this.DataVoid(Byte);//数据处理
}
}
}
catch ()
{}
}