日期:2014-05-18  浏览次数:20795 次

serialport 开发 rs232脚 COM1 接口程序,发信测试成功,但就是接收不到信息,请前辈出手相救!!万分感谢!!

Form1.cs

C# code

 public Form1()
        {
            InitializeComponent();
        }

      
        //初始化SerialPort对象方法.PortName为COM口名称,例如"COM1","COM2"等,注意是string类型
       public void InitCOM(string PortName)
         {
             serialPort1 = new SerialPort(PortName);
             serialPort1.BaudRate = 9600;//波特率
             serialPort1.Parity = Parity.None;//无奇偶校验位
             serialPort1.StopBits = StopBits.Two;//两个停止位
             serialPort1.Handshake = Handshake.RequestToSend;//控制协议
             serialPort1.ReceivedBytesThreshold = 4;//设置 DataReceived 事件发生前内部输入缓冲区中的字节数
           
        }

        
 
         //打开串口的方法
         public void OpenPort()
         {
             try
             {
                 serialPort1.Open();
             }
             catch { }
             if (serialPort1.IsOpen)
             {
                 Console.WriteLine("the port is opened!");
             }
             else
            {
                 Console.WriteLine("failure to open the port!");
             }
         }

         //关闭串口的方法
         public void ClosePort()
         {
             serialPort1.Close();
             if (!serialPort1.IsOpen)
            {
                 Console.WriteLine("the port is already closed!");
             }
        }
  
        
         
         //调用实例,发送数据
        
        private void button1_Click(object sender, EventArgs e)
        {
            InitCOM("COM3");
            OpenPort();

            byte[] data = Encoding.Unicode.GetBytes(this.textBox1.Text);
            string str = Convert.ToBase64String(data);
            serialPort1.WriteLine(str);
            ClosePort();
            MessageBox.Show(this, "利用串口成功发送数据!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

        }

        //接收数据
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                byte[] datar = Convert.FromBase64String(serialPort1.ReadLine());
                textBox2.Text = Encoding.Unicode.GetString(datar);
                ClosePort();
                MessageBox.Show(this, "利用串口成功接收数据!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch
            {
                ClosePort();
                MessageBox.Show(this, "利用串口接收数据失败!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

       




以上代码,测试能发送成功,但是就是接收不到,而且点接收按钮后,程序就像死在那了,点哪都动不了了。因为刚接触
rs232接口编程,有些东西还不太懂,请各位前辈指点迷津,万分感谢!!!!



或是前辈有更好的 rs232串口例子程序,可以给演示一下,最后是自己实际测试成功的源码,因为在网上我试了很很多的代码,都是测试不成功。


------解决方案--------------------
死掉应该是serialPort1.ReadLine读不到数据,会一直等待,可以设置timeout超时时间
另外
serialPort1.WriteLine(str);之后,你关闭了com 即 ClosePort();这样会清空缓冲区的,帮助中对Close的解释是(调用此方法会关闭 SerialPort 对象并清除接收缓冲区和传输缓冲区。),所以你
ReadLine根本读不到数据,必死
------解决方案--------------------
(m_portA.ReadBufferSize=1024;)问题感觉不是输入缓冲区不够大,你给发出的指令加上"+ (char)3 +(char)13"看看
------解决方案--------------------
1楼的解释是对的,不能open并发送后立即close,这样发送出去的会被清掉。所以你什么也收不到。
为serialport实例增加一个receivedata的委托。
------解决方案--------------------
是异步的方式接受的是么? 改成while(true)方式去接收信息试试看,。。。
------解决方案--------------------
InitCOM("COM3");
OpenPort();

byte[] data = Encoding.Unicode.GetBytes(this.textBox1.Text);
string str = Convert.ToBase64String(data);