我用串口调试精灵往串口发送数据时侯能够接收到数据,但自己写的代码发送给串口数据后,没有任何数据返回。用CommMonitor监听下来的结果是正常情况IOCTL_SET_CHAR :Chars Eof: 26, Error: 0, break: 0, Event: 0, Xon: 17, Xoff: 19,我的代码Event:26。请教大家有没有人碰到过这些情况,该怎么解决,谢谢

解决方案 »

  1.   

    这跟串口接收不到数据有关系吗? 
     private void write_Click(object sender, EventArgs e)
            {
                mute.WaitOne();   
                int n = 0;
           
              
                try
                {
                    com.DiscardOutBuffer();
                    com.DiscardInBuffer();
                    com.Write(new byte[] {0x04,0x33,0x31,0x72}, 0, 4);
                   while (n < 104)
                       { 
                        
                       com.Write(new byte[] { 0x04, 0x33, 0x31, 0x05 }, 0, 4);
                    
                       n++;
                       }
                      
                }
                catch 
                {
                    MessageBox.Show("串口通讯错误");
                }
       
            
            }        private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
            {
                try
                {
                    Thread.Sleep(100); 
                    int bytes = com.BytesToRead;
                    byte[] buffer = new byte[bytes];                // 读取缓冲区的数据到数组                com.Read(buffer, 0, bytes);
                }
                // 显示读取的数据到数据窗口
                catch { }
            }     我发送数据没有问题的,按理应该每发送一个数据,都能收到一个返回的数据,但是串口调试工具可以收到,我用received事件不行。  
      

  2.   

    瞎写,首先发送的地方最好组成一个大数组,一次调用write,让wirte自己去分解,其次,data_received是中断触发后,触发线程调用的事件,这里不需要等待。
      

  3.   

    先确定下DataReceived事件已经注册了
    另外你用串口助手也是一次性发104组数据吗?在你的button_Click事件里,一下子发了好多组数据啊,这样能达到效果?
      

  4.   


    SerialPort comPort;
    comPort = new SerialPort(cmbPort.Text, baudRate, tempParity, dataBits, stopBit);
     comPort.DataReceived += new SerialDataReceivedEventHandler(comPort_DataReceived);
      /// <summary>
            /// 串口接收
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            void comPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
            {
                if (ClosingPort)
                    return;
                
                byte[] readBuffer = new byte[comPort.ReadBufferSize + 1];
                int strLength = comPort.Read(readBuffer, 0, readBuffer.Length);
                recCount += strLength;
                string msg = "";            msg += recEncode.GetString(readBuffer, 0, strLength);
                if (rdoHex.Checked)
                    msg = BaseHelper.ConvertToHex(msg, recEncode);
                if (ckNext.Checked)
                    msg += "\r\n";
                if (ckPauseRec.Checked)//暂停接收
                    return;
                this.Invoke(new MethodInvoker(delegate
                {
                    if (!ckSave.Checked)
                        txtRec.Text += msg;
                    tsblRecCount.Text = recCount.ToString();
                }));
                if (ckSave.Checked)
                {
                    FileStream fs = new FileStream(saveFileName, FileMode.Append);
                    fs.Write(readBuffer, 0, strLength);
                    fs.Flush();
                    fs.Close();
                }
            }
      

  5.   

    我也遇到同样问题:vb.net里面,commMonitor监测仪器厂家软件的IOCTL_SET_CHARS里面Event是0,但我自己写的软件Event=26,不知道怎么修改这个数值,还有IOCTL_SET_HANDFLOW里面的几个数值怎么修改?麻烦讲清楚一点