| 
 | 
 
20金钱 
 本帖最后由 sd7081433 于 2014-6-10 18:28 编辑  
 
我以前没做过编程,刚学过c#不久,这是我编的第一个程序,这是第二回发帖,第一回什么都不懂,随便发的(其实这回也是)。这是第一认认真真的发的。 
之前写的太复杂了,我简单的说下,就是如题,c#通过串行通信,发送数据帧与帧的时间间隔,以0.1ms为单位,但是测试的时候不准确。测试时我是用示波器测试的。 
希望舒飞大哥帮帮我,也希望有做过的朋友帮一下,谢谢! 
这是我测试用的发送函数 
 #region Function  - Write 
        public bool send_function(byte[] message,int messageLength, ref byte[] Response,int ResponseLength) 
        { 
            UsingTimer ut=new UsingTimer(); 
             
             
             
             
             
            T35Counter = ((1 + DataBits + 1 + StopBitscounter) * 3.5) / BaudRate; 
            T15Counter = ((1 + DataBits + 1 + StopBitscounter) * 1.5) / BaudRate; 
            T35Counter = Convert.ToInt32(T35Counter * 10000); 
            
            //Ensure port is open: 
             
            while (sp.BytesToWrite != 0) 
            { 
            } 
            // Start timer 
            //Send Modbus message to Serial Port: 
             
            
                try 
                { 
                    sp.DiscardInBuffer(); 
                    sp.DiscardOutBuffer(); 
                    ////写入数据 
 
                    sp.Write(message, 0, 1); 
                     
                    while (true) 
                    { 
                        
                         
                        if (sp.BytesToWrite == 0) 
                        { 
                            ut.Counter1(); 
                            
                            break; 
                        } 
                    } 
                    while (true) 
                    { 
                        ut.Counter2(); 
 
                       a = (uint)ut.Duration; 
                        if ((a) >= (T35Counter + 10)) 
                        { 
                            sp.Write(message, 0, 1); 
                           
                            return true; 
 
                        } 
                    } 
                     
 
                                        
                      
                } 
                catch (Exception err) 
                { 
                    modbusStatus = "Error in write event: " + err.Message; 
                    //MessageBox.Show(modbusStatus); 
                    return false; 
                } 
 
               
                 
 
          
        } 
         
        #endregion 
这是我调用的api函数 
   public UsingTimer() 
        { 
            counter1 = 0; 
           counter2 = 0; 
 
            if (QueryPerformanceFrequency(ref freq) == false) 
            { 
                // high-performance counter not supported  
                throw new Win32Exception(); 
            } 
        } 
 
        
        public void Counter1() 
        { 
             
            Thread.Sleep(0); 
 
            QueryPerformanceCounter(ref counter1); 
        } 
 
       
        public void Counter2() 
        { 
            QueryPerformanceCounter(ref counter2); 
        } 
 
        
        public double Duration 
        { 
            get 
            { 
                return (double)10000*(counter2 - counter1) / freq; 
            } 
        } 
    } |   
 
 
 
 
 
 |