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触发事件已经完全地处理执行完了呢?
烦请指导,谢谢。

解决方案 »

  1.   

    回2楼 tellxp:
    所以可以在DataReceived事件里面,加入Write的方法,让SerialPort自动接收和发送?烦请指导,谢谢。
      

  2.   

    system.io.serialport建立串口连接,使用二进制方式打开文件,然后按照一定的协议(自己定义的发送接收)发送数据,远端收到后组成新的文件。
    http://topic.csdn.net/u/20100407/10/b5a1c725-9225-421c-b29a-915e6c3ed252.html
      

  3.   

    回5楼  wuyq11:
    不好意思,我的意思是指…能否在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串口的发送和接收是否需要时间来切换?谢谢。
      

  4.   

    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;
                   }
                }
             }
          }
      

  5.   


    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;
                   }
                }
             }
          }
      

  6.   

    读和写应该是两个线程,可以同时进行,也可以读了以后再写,我觉得这样比较好控制一些,你在datareceived事件中,MSDN上说了,每次并不是接收固定字节的数据,所以你应该用个仲裁的模式:
    读数据->产生事件提交到仲裁判断并作出相应的动作;
    写数据->写完后同样提交到仲裁,判断下一步操作;
      

  7.   


    比如说流程是这样的,接收到串口发来的"ready?",然后向串口发送 "yes!",串口再回应"yes!",事件结束,这应该如何做?