读书人

C# serialPort串口通讯有关问题

发布时间: 2012-02-15 12:09:44 作者: rapoo

C# serialPort串口通讯问题
现在我碰到一个很头疼的问题。
我是才接触到串口通讯的
也就用了一个通宵的时间看了一下。现在项目上的一个程序需要用到串口通讯,接收称的信息
程序写完,在我机子上调试了 通过 没什么问题
然后到客户的机子上面就出问题了,只要一点关闭串口操作,就程序卡死了
后来我又去其他电脑上做了测试
我的笔记本 xp系统,我同事的笔记本 win7系统, 客户的服务器4核的 2003系统 都没有问题
但是到了他们操作员的电脑是的时候就出现这样的情况

后来我以为是程序有问题,在网上也搜索了好多解决方案,测试了还是不行
原来以为是在close串口的时候 可能有事件过程没有执行完成,就会出异常,然后程序停止响应,有的人说是.NET的bug
后来我也写了方法防止错误 测试了还是卡死
我现在怀疑是不是他们系统的问题,就想请教下大家

C# code
#region 监听电子秤Com口通信        private delegate void MonitorCom1Callback(byte[] arr);        private MonitorCom1Callback receive;        private bool _isReceiving = false;                private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)        {            //MonitorCom1();            byte[] DataRecivce = new byte[32];            if (serialPort1.IsOpen)            {                if (_isReceiving == false)                {                    try                    {                                                serialPort1.Read(DataRecivce, 0, 32);                        //MonitorCom1(DataRecivce);                        receive = new MonitorCom1Callback(MonitorCom1);                        this.Invoke(receive, new object[] { DataRecivce });                        Thread.Sleep(300);                    }                    catch                    {                        MessageBox.Show(this, "电子秤打开错误,请检查电子秤是否正确连接!", "通讯失败!", MessageBoxButtons.OK, MessageBoxIcon.Error);                        return;                    }                }            }        }        #endregionpublic void MonitorCom1(byte[] text)        {            _isReceiving = true;            string str = "";            string newWeight = "";            int len = 0;            try            {                //if (this.txtGrossWeight.InvokeRequired)                //{                //    MonitorCom1Callback d = new MonitorCom1Callback(MonitorCom1);                //    this.Invoke(d, new object[] { text });                //}                //else                //{                    for (int m = 0; m < text.Length; m++)                    {                        if (Convert.ToInt32(Convert.ToChar(text[m])) == 2)                        {                            while (Convert.ToInt32(Convert.ToChar(text[m])) != 3)                            {                                str += Convert.ToChar(text[m]); m++;                            }                            str += Convert.ToChar(text[m]);                            break;                        }                        break;                    }                    len = Convert.ToInt32(str.Substring(8, 1));                    int x = Convert.ToInt32(str.Substring(2, 6 - len));                    int y = len != 0 ? Convert.ToInt32(str.Substring(8 - len, len)) : -1;                    if (Convert.ToChar(str.Substring(1, 1)) == '-')                    {                        newWeight = "-";                    }                    if (y == -1)                    {                        newWeight += x.ToString();                    }                    else                    {                        newWeight += x.ToString() + "." + y.ToString();                    }                                        txtGrossWeight.Text = newWeight;                                                        //}            }            catch            { }                        _isReceiving = false;      }        private void button1_Click(object sender, EventArgs e)        {            if (_isPause )            {                                seriaportClose();                            }            else if (!_isPause)            {                                erialPort1.DataReceived += serialPort1_DataReceived;                serialPort1.Open();            }        }        private void seriaportClose()        {            serialPort1.DataReceived -= serialPort1_DataReceived;            //int i = Environment.TickCount;            while (_isReceiving) Application.DoEvents();                        serialPort1.Close();                                                } 



[解决办法]
应该和机器接口有关
看看出错的机器是否采用了USB接口和COM接口互转。
[解决办法]
问题可能出在serialPort1_DataReceived这个方法里,这个里面你不要去写别的代码,所有的串处理过程都
用异步调用处理。你可以在这个方法里只写一句: this.Invoke(new EventHandler(DoUpdate), sender);
然后你在DoUpdate方法里写串口数据处理。

private void DoUpdate(object sender, EventArgs e)
{
SerialPort objSerialPort = (SerialPort)sender; //这个就是你的串口对象
.......
}
其它的代码自己加上。

读书人网 >C++ Builder

热点推荐