您的位置:程序门 -> .net技术 -> c#



串口数据显示在textbox里有问题


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


串口数据显示在textbox里有问题
发表于:2007-01-15 21:06:08 楼主
using   system;
using   system.collections.generic;
using   system.componentmodel;
using   system.data;
using   system.drawing;
using   system.text;
using   system.windows.forms;
using   system.collections;
using   system.threading;
using   system.io.ports;
using   system.io;

namespace   serialport
{    

        public   partial   class   form1   :   form
        {
                public   form1()
                {
                        initializecomponent();                        
                }                
                /*==================================================================================================
                  *   初始化定义
                  *=================================================================================*/
                private   float[]   m_vol   =   new   float[5];//全局数组
                private   byte[]   lpbuffer   =   new   byte[10];//接收数据的buff
                private   uint[]   m_v   =   new   uint[10];
               
                private   float   start_f   =   0;
                private   float   end_f   =   0;
                private   float   step_f   =   0;
                private   float   cur_f   =   0;
                private   system.windows.forms.timer   work_timer   =   new   system.windows.forms.timer();        
                private   byte[]   receivedata   =   new   byte[10];
                private   arraylist   datalist   =   new   arraylist();
                private   streamwriter   sw;
               
                private   struct   impedata
                {
                        public   float   m_freq;
                        public   float   m_g;
                        public   float   m_b;
                        public   float   m_r;
                        public   float   m_x;
                        public   float   m_z;
                        public   float   m_y;
                        public   float   m_phi;
                        public   float   m_logz;
                        public   int       m_ds;
                }
                private   int   m_ds2y_select=0;
                private   int   ds2y_adjust_flag   =   0;
               

                /*==================================================================================================
                  *  
                  *===============================================================================================   */
                private   void   harddeviceiniti()
                {                        
                        if   (serialport1.isopen   ==   false)
                        {
                                serialport1.open();                                
                        }
                        serialport1.write( "1111o ");
                        serialport1.write( "4111k ");
                }//硬件初始化

                private   void   sendfrequnce()
                {
                        double   lf   =   (double)(cur_f)   *   859   /   4;                        
                        ulong   m_dwfre   =   (ulong)lf;

                        byte[]   fre   =   new   byte[5];
                        fre[4]   =   convert.tobyte( 'h ');
                        fre[3]   =   convert.tobyte(m_dwfre   &   0xff);
                        m_dwfre   > > =   8;
                        fre[2]   =   convert.tobyte(m_dwfre   &   0xff);
                        m_dwfre   > > =   8;
                        fre[1]   =   convert.tobyte(m_dwfre   &   0xff);
                        m_dwfre   > > =   8;
                        fre[0]   =   convert.tobyte(m_dwfre   &   0xff);
                        serialport1.write(fre,   0,   5);
                       
                }//发送频率字
                       
                private   void   form1_load(object   sender,   eventargs   e)//程序初始化
                {
                       
                        selectlau.text   =   "简体中文 ";
                        selectcoord.text   =   "lgz   phi-f ";
                       
                        line_checkbox.checked   =   true;
                        label20.visible   =   true;
                        advancedtextbox.visible   =   true;
                        advancedcombox.visible   =   false;
                        work_timer.interval   =   70;//70毫秒
                        work_timer.enabled   =   false;
                        work_timer.tick   +=   new   eventhandler(work_timer_tick);                      
                       
                }

                private   void   work_timer_tick(object   sender,   eventargs   e)
                {          
                        work_timer.enabled   =   false;    
                        sendfrequnce();                        
                        work_timer.enabled   =   true;
                     
                }
               
                private   void   start_click(object   sender,   eventargs   e)//启动
                {
                       
                        harddeviceiniti();
                        start_f   =   1000   *   convert.tosingle(startfrequency.text);
                        end_f   =   1000   *   convert.tosingle(endfrequency.text);
                        step_f   =   convert.tosingle(advancedtextbox.text);
                        cur_f   =   start_f;                        
                        work_timer.enabled   =   true;
                       
                }
                private   void   doupdate(object   s,   eventargs   e)
                {                
                        cur_r.text   =   m_vol[0].tostring();
                        cur_x.text   =   m_vol[1].tostring();
                        cur_g.text   =   m_vol[2].tostring();
                        cur_b.text   =   m_vol[3].tostring();
                        cur_phase.text   =   m_vol[4].tostring();                        
                }              

                private   void   serialport1_datareceived(object   sender,   system.io.ports.serialdatareceivedeventargs   e)
                {
                        try
                        {
                                serialport1.read(lpbuffer,   0,   10);
                                                             
                                m_v[0]   =   convert.touint32((lpbuffer[0]   < <   4)   +   (lpbuffer[1]   &   0x0f));
                                m_v[1]   =   convert.touint32((lpbuffer[2]   < <   4)   +   (lpbuffer[3]   &   0x0f));
                                m_v[2]   =   convert.touint32((lpbuffer[4]   < <   4)   +   (lpbuffer[5]   &   0x0f));
                                m_v[3]   =   convert.touint32((lpbuffer[6]   < <   4)   +   (lpbuffer[7]   &   0x0f));
                                m_v[4]   =   convert.touint32((lpbuffer[8]   < <   4)   +   (lpbuffer[9]   &   0x0f));

                                m_vol[0]   =   convert.tosingle((float)(m_v[0])   *   5   /   4095);
                                m_vol[1]   =   convert.tosingle((float)(m_v[1])   *   5   /   4095);
                                m_vol[2]   =   convert.tosingle((float)(m_v[2])   *   5   /   4095);
                                m_vol[3]   =   convert.tosingle((float)(m_v[3])   *   5   /   4095);
                                m_vol[4]   =   convert.tosingle((float)(m_v[4])   *   5   /   4095);              

                                this.begininvoke(new   eventhandler(doupdate));
                        }
                        catch(exception   ce)
                        {
                                messagebox.show(ce.tostring());
                                return;
                        }
                                 
                                         
                }

               
        }
       
}
现在的问题是:只有       cur_r,   cur_x,   cur_g有数据,另外两个没有数据显示。但是,当我移动鼠标的时候,就会有数据显示出来,如果用messagebox来显示m_vol[3],m_vol[4]的话,显示出来的数值是正确的。这个问题困扰了我好久,各位大虾,帮帮忙,看看到底是什么原因。
发表于:2007-01-15 21:08:591楼 得分:0
第三贴了吧...

确实比较奇怪..

帮顶一下吧..
发表于:2007-01-16 13:50:092楼 得分:0
我都快被这个现象搞疯了,那位知道,立马给分,不够再加
发表于:2007-01-16 14:14:253楼 得分:0
这个现象确实比较奇怪...

可惜我也没见过...


快速检索

最新资讯
热门点击