none
【求助】怪哉!serialport控件拒绝接受数据! RRS feed

  • 常规讨论

  • 背景知识:

    (1)问题源头:近日用串口程序操纵wavecom的一个gprs猫。

    (2)该gprs猫有一个最基本的反应就是:发送给猫“AT回车”,猫返回“回车OK回车”。并且该猫没有问题。

    (3)程序写作语言:C#;IDE:vs2005;version:.NetFramework2;OS:winxp pro sp2。

    (4)程序功能:发送给猫“AT回车”,等待猫返回“回车OK回车”并显示在窗口里。

     

    程序完成之后测试结果:

    (1)电脑A是主机,电脑B模拟gprs模块。在A上运行我的程序并在B上用串口工具监听,发现A可以成功发过来“AT回车”。然后,用在B上的串口工具向A返回“回车ok回车”,A上我的程序可以正常显示“回车OK回车”。

    (2)将A连接至GPRS猫,点击发送后,程序没有显示“回车OK回车”。此时关掉我的程序,立即打开A上的串口工具,发现可以接受到“回车OK回车”。

     

    我猜测有两种可能的原因:

    (1)serialport控件拒绝接受了“回车OK回车”这个数据,仍然保存在系统的缓存中,所以关掉我的程序立刻打开串口调试工具后可以接收到“回车OK回车”。

    (2)操作系统在接收到返回的数据之后没有通知serialport控件,导致serialport控件的DataReceive事件没有激发,没有调用我自己写的读串口的指令,所以数据仍留在缓存中。

    不论是哪种原因,都是serialport串口有问题。

     

    抑或是我的猜想是错误的,请达人指教!

     

     

    附我的代码:

    (窗体中包含一个按钮,一个输入框,一个显示框)

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Threading;
    using System.IO.Ports;
    using System.Windows.Forms;

    namespace com_test_081101
    {
        public partial class Form1 : Form
        {
            string buffer;
            public Form1()
            {
                InitializeComponent();
                tr.Text = "";
                sp.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
                if (sp.IsOpen != true)
                {
                    sp.Open();
                }
                buffer = "";
            }

            private void bs_Click(object sender, EventArgs e)
            {
                if ((tsb.TextLength != 0) && (tsb.TextLength < 10))
                {
                    sp.Write(new char[] { 'A', 'T' }, 0, 2);
                    sp.Write(new byte[] { 0x0D }, 0, 1);
                    ts.Text += tsb.Text;
                    ts.Text += "\r\n";
                }
            }

            private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
            {
                // Show all the incoming data in the port's buffer
                string temp;
               
                temp = sp.ReadExisting();
                do
                {
                    buffer += temp;
                    Thread.Sleep(10);
                    temp = sp.ReadExisting();
                } while (temp.Length != 0);

                if (buffer.Length != 0)
                {
                    safeAddtrText(buffer);
                    safeAddtrText("\r\n");

                }

            }

            public delegate void _SafeAddtrTextCall(string text);

            private void safeAddtrText(string text)
            {
                if (this.InvokeRequired)
                {
                    _SafeAddtrTextCall call =
                    delegate(string s)
                    {
                        this.tr.Text += s;
                    };

                    this.Invoke(call, text);
                }
                else
                {
                    this.tr.Text += text;
                }
            }
        }
    }

    2008年11月1日 12:19