本人小菜鸟,这是我写的串口通讯程序,但是就是没有返回的数据,哪位高手能给我讲讲为什么?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;namespace WindowsApplication1
{
    public partial class Form1 : Form
    {       // private Thread ReadThread = null;
        SerialPort SerialPort1 = new SerialPort("com1",9600);
   
        
        public Form1()
        {   
            InitializeComponent();
           
          
        }
      
        private void button3_Click(object sender, EventArgs e)
        {
            if (button3.Text == "打开串口")
            {
                              serialPort1.Open();
                //ReadThread = new Thread(ReadTxt);
                //ReadThread.Start();
                button3.Text = "关闭串口";
            }            else
            {
                serialPort1.Close();
                
                button3.Text = "打开串口";
            }        }
        
        //发送按钮
        private void button1_Click(object sender, EventArgs e)
        {            if (textBox1.Text == "")
            {
                MessageBox.Show("发送数据不能为空!");
                return;
            }
            string str;
            str = textBox1.Text;
    
            byte[] myByte = GettoByte(str);
            serialPort1.Write(myByte,0,myByte.Length);
        }
       
            private byte[] GettoByte(string Msg)
        {
            int len = (Msg.Length + 1) / 3;
            Byte[] Data = new byte[len];
            int j = 0;
            for (int i = 0; i < len; i++)
            {
                int number = HexTochar(Msg[j * 3]) * 0X10 + HexTochar(Msg[j * 3 + 1]);
                Data[j] = Convert.ToByte(number);
                j++;
            }
            return Data;
        }
        Byte HexTochar(char src)
        {
            if ((src >= '0') && (src <= '9'))
                return Convert.ToByte(src - '0');            if ((src >= 'A') && (src <= 'F'))
                return Convert.ToByte(src - 'A' + 10);            if ((src >= 'a') && (src <= 'f'))
                return Convert.ToByte(src - 'a' + 10);
            return 0;
        }
        private string tohexstring(byte[] bytes, int len)
        {
            StringBuilder s;
            if (len == 0)
            {
                s = new StringBuilder(bytes.Length*2);
                foreach (byte b in bytes)
                {
                    s.Append(b.ToString("X2"));
                    s.Append(" ");
                }            }
           else
            {
                s = new StringBuilder(len*2);
                for (int i = 0; i < len; i++)
             {
                    s.Append(bytes[i].ToString("X2"));
                    if (i < len - 1)
                       s.Append(" ");
               }
            }            return s.ToString();
        }
     
               //退出 
        private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
        //清空
        private void button4_Click(object sender, EventArgs e)
        {
            textBox2.Text = string.Empty;
        }
        public void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            string strT="00";
            
            strT+=this.serialPort1.ReadExisting();            textBox2.Text = strT;
            
        }        
        public static int Figurative(byte[] testbye, int len, out byte[] outbyte)
        {
            byte[] resultbyte = new byte[1000];
            int ii, k;
            for (ii = 0, k = 0; ii < len; ii++)//判断接收的数据是否有转义字符
            {
                if (testbye[ii] == 0xA8 && ii + 1 < len)
                {
                    if (testbye[ii + 1] == 0)
                    {
                        resultbyte[k] = 0xAA;
                        ii++;
                    }
                    else if (testbye[ii + 1] == 1)
                    {
                        resultbyte[k] = 0xA9;
                        ii++;
                    }
                    else if (testbye[ii + 1] == 2)
                    {
                        resultbyte[k] = 0xA8;
                        ii++;
                    }
                    else
                    {
                        outbyte = resultbyte;
                        return 0;
                    }
                }
                else
                    resultbyte[k] = testbye[ii];
                k++;
            }
            outbyte = resultbyte;
            return k;
        }        private void textBox2_TextChanged(object sender, EventArgs e)
        {
           
        }        
    }
    
}

解决方案 »

  1.   

    1. 看看发送的函数成功了没有
    2. 你的串口设备的通讯协议是什么, 返回是text还是二进制, 你跟踪一下看会不会进入serialPort1_DataReceived 函数
      

  2.   

    发送的函数成功了,返回的是16进制的数据,就是不能进入serialPort1_DataReceived ,不知道为什么?
      

  3.   

    试试监听port(轮询)
    send()
    while(1)
    {
     sleep(100ms)
     receive()
    }如果没有收到就是没有返回了
      

  4.   

    硬件方面采用什么测试的
    可以先将COM的2和3短接起来测试下
    那样也好发现是哪里的问题