| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 723 人关注过本帖
标题:请教串口读数问题?
只看楼主 加入收藏
韩枫1983
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2013-12-21
结帖率:0
收藏
已结贴  问题点数:20 回复次数:2 
请教串口读数问题?
public string conver()
        {
            string rus = "";
            XmlDocument xml = new XmlDocument();
            xml.Load("C:\\txcs.xml");
            XmlNode root = xml.SelectSingleNode("//纳伏表");
            string com1 = root.FirstChild.InnerText;
            string rate1 = root.LastChild.InnerText;
            SerialPort sm = new SerialPort();
            sm = com(com1, rate1);
            sm.Open();
            if (sm.IsOpen == false)
                MessageBox.Show("纳伏表串口未打开!!!");
            sm.WriteLine("CONF:VOLT:DC 100,DEF,(@2)");
            sm.WriteLine("CONF:VOLT:DC 100,DEF,(@1)");
            sm.WriteLine("SYSTem:REMote");//读一个数据设置
            for (int i = 0; i < 10; i++)
            {               
                System.Threading.Thread.Sleep(500);
                sm.WriteLine("READ?");
                byte[] ru;
                int Count;
                Count = sm.BytesToRead;
                ru = new Byte[Count];
                sm.Read(ru, 0, Count);
                for (int j = 0; j < Count; j++)
                {
                    rus =rus+ru[j];
                }

                             
            }
            sm.Close();
            return rus;
            
        }
用上面程序读取串口中的值时,发现串口缓冲器中一直没有值, sm.BytesToRead一直为零,但是用串口调试软件发送READ?后,可以从缓冲器中读取值,请问我上面那里出了问题,缓冲器中没有值?
搜索更多相关主题的帖子: public false 
2013-12-21 17:55
iamjerry
Rank: 2
等 级:论坛游民
帖 子:10
专家分:20
注 册:2013-11-29
收藏
得分:20 
第一步,先跟踪一下串口是否真正打开了;从你的代码来看if (sm.IsOpen == false)时,你还是往串口写数据了,这点是肯定不对的;
第二步,sm.WriteLine("READ?");执行完后,你立即就去读串口了,这时你的设备还没有来得及返回串口数据时,你等了500毫秒后又sm.WriteLine("READ?");这样反复10次,这样你肯定始终也读不到数据。
我的读串口数据的代码如下:
public byte[] ReadWriteSerialPortData(SerialPort CurrentCom, byte[] SendData, bool bAns, double dbMaxWaitTime, ref bool bRevDataOK)
        {
            byte[] ASend = new byte[SendData.Length + 3];
            ASend[0] = Convert.ToByte("7F", 16);
            ASend[1] = Convert.ToByte("7F", 16);
            ASend[2] = Convert.ToByte("7F", 16);
            for (byte i = 0; i < SendData.Length; i++)
            {
                ASend[i + 3] = SendData[i];
            }
            PubFunction pf = new PubFunction();
            byte[] RevData = null;
            byte iTimes = 0;
            while (iTimes < 2)
            {
                CurrentCom.DiscardOutBuffer(); //清空发送缓冲区
                CurrentCom.DiscardInBuffer();  //清空接收缓冲区

                CurrentCom.Write(ASend, 0, ASend.Length);
                if (bAns == false)  //不需要应答
                {
                    bRevDataOK = true;
                    return RevData;
                }
                DateTime dtStart = DateTime.Now;
                double dbTime = 0;
                RevData = null;
                bRevDataOK = false;
                while (true)
                {
                    //pf.DelayTime(0.01); //延时10毫秒
                    int iLen = CurrentCom.BytesToRead;
                    if (iLen > 0)
                    {
                        byte[] btData = new byte[iLen];
                        CurrentCom.Read(btData, 0, iLen);
                        RevData = ByteArrayConcat(RevData, btData);
                    }
                    else
                    {
                        dbTime = DateTime.Now.Subtract(dtStart).TotalSeconds;
                        if (dbTime > dbMaxWaitTime) break;
                        else continue;
                    }
                    string strRevData = pf.GetArrayToSTR(RevData, false).ToUpper();
                    if ((strRevData.Equals("06")) || (strRevData.Equals("15")))
                    {
                        RevData = new byte[1];
                        if (strRevData.Equals("06")) RevData[0] = 6;
                        else RevData[0] = 21;
                        bRevDataOK = true;
                        break;
                    }
                    else
                    {
                        if ((strRevData.IndexOf("2F") >= 0) && (strRevData.IndexOf("0A") > 0))
                        {
                            strRevData = strRevData.Substring(strRevData.IndexOf("2F"), strRevData.IndexOf("0A") - strRevData.IndexOf("2F") + 2);
                            strRevData = strRevData.Replace(" ", "");
                            RevData = new byte[strRevData.Length / 2];
                            RevData = pf.HexStringToByteArray(strRevData);
                            bRevDataOK = true;
                            break;
                        }
                        else if (((strRevData.IndexOf("01") == 0) || (strRevData.IndexOf("02") == 0)) && (strRevData.IndexOf("03") > 0))
                        {
                            if (strRevData.IndexOf("03") + 5 == strRevData.Length)
                            {
                                strRevData = strRevData.Substring(0, strRevData.Length);
                                strRevData = strRevData.Replace(" ", "");
                                RevData = new byte[strRevData.Length / 2];
                                RevData = pf.HexStringToByteArray(strRevData);
                                bRevDataOK = true;
                                break;
                            }
                        }
                    }
                }
                if (bRevDataOK) break;
                else
                {
                    iTimes++;
                    pf.DelayTime(0.5); //延时0.5秒
                }
            }
            return RevData;
        }
以上代码希望对你有所帮助。
2013-12-22 19:47
韩枫1983
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2013-12-21
收藏
得分:0 
回复 2楼 iamjerry
谢谢,我改了试试看,
2013-12-29 21:12
快速回复:请教串口读数问题?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016783 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved