1. 在SerialPort的DataReceived触发事件程序中,可以写入SerialPort.Write发送资料吗?例如:private void SP_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
int BufferByteLength = ZIG100toSP.BytesToRead; byte[] DataBuffer = new byte[BufferByteLength]; SP.Read(DataBuffer, 0, BufferByteLength); ///
/// 处理数据
/// SP.Write(DataBuffer, 0, BufferByteLength); // 写入发送}
2. 请问要如何知道DataReceived触发事件已经完全地处理执行完了呢?
烦请指导,谢谢。
{
int BufferByteLength = ZIG100toSP.BytesToRead; byte[] DataBuffer = new byte[BufferByteLength]; SP.Read(DataBuffer, 0, BufferByteLength); ///
/// 处理数据
/// SP.Write(DataBuffer, 0, BufferByteLength); // 写入发送}
2. 请问要如何知道DataReceived触发事件已经完全地处理执行完了呢?
烦请指导,谢谢。
所以可以在DataReceived事件里面,加入Write的方法,让SerialPort自动接收和发送?烦请指导,谢谢。
http://topic.csdn.net/u/20100407/10/b5a1c725-9225-421c-b29a-915e6c3ed252.html
不好意思,我的意思是指…能否在SerialPort的DataReceived触发事件”程序”里,写入SerialPort.Write来发送串口的数据?例如下面的例子:private void SP_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
int BufferByteLength = ZIG100toSP.BytesToRead; byte[] DataBuffer = new byte[BufferByteLength]; SP.Read(DataBuffer, 0, BufferByteLength); ///
/// 处理数据
/// SP.Write(DataBuffer, 0, BufferByteLength); // 写入发送}因为不知道在DataReceived触发事件”程序”里面,写入SerialPort.Write发送串口的数据,会不会造成什么问题? PC串口的发送和接收是否需要时间来切换?谢谢。
protected SerialPort ComPort;
protected byte[] DataBuffer;//读取串口字符数组
protected int CurrentIndex = 0;//当前字符
private void ComPort_DataReceived(object sender,SerialDataReceivedEventArgs e)
{
SerialPort comport = sender as SerialPort;
int reciveCount = comport.BytesToRead;
int bytData;
lock(o_lock)
{
for(int i = 0; i < reciveCount; i++)
{
bytData = comport.ReadByte(); if(bytData == EndChar)//结束符03H
{
DataSplit(DataBuffer);//数据处理 for(int j = 0; j < DataBuffer.Length; DataBuffer[j] = 0,j++)
;
CurrentIndex = 0;
comport.DiscardInBuffer();
break;
}
else
{
DataBuffer[CurrentIndex++] = (byte)bytData;
}
}
}
}
object o_lock = new object();
protected SerialPort ComPort;
protected byte[] DataBuffer;//读取串口字符数组
protected int CurrentIndex = 0;//当前字符
private void ComPort_DataReceived(object sender,SerialDataReceivedEventArgs e)
{
SerialPort comport = sender as SerialPort;
int reciveCount = comport.BytesToRead;
int bytData;
lock(o_lock)
{
for(int i = 0; i < reciveCount; i++)
{
bytData = comport.ReadByte(); if(bytData == EndChar)//结束符03H
{
DataSplit(DataBuffer);//数据处理 for(int j = 0; j < DataBuffer.Length; DataBuffer[j] = 0,j++)
;
CurrentIndex = 0;
comport.DiscardInBuffer();
break;
}
else
{
DataBuffer[CurrentIndex++] = (byte)bytData;
}
}
}
}
读数据->产生事件提交到仲裁判断并作出相应的动作;
写数据->写完后同样提交到仲裁,判断下一步操作;
比如说流程是这样的,接收到串口发来的"ready?",然后向串口发送 "yes!",串口再回应"yes!",事件结束,这应该如何做?