显示错误::'Timer'是'System.Windows.Forms.Timer'和'System.Timers.Timer'之间的模糊引用..............无法解析plz帮助 [英] showing error :: 'Timer' is an ambiguous reference between 'System.Windows.Forms.Timer' and 'System.Timers.Timer' ..............unable to resolve plz help

查看:73
本文介绍了显示错误::'Timer'是'System.Windows.Forms.Timer'和'System.Timers.Timer'之间的模糊引用..............无法解析plz帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 使用系统; 
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.Linq;
使用 System.Text;
使用 System.Windows.Forms;
使用 System.IO.Ports;
使用 System.Timers;



命名空间 serialcomm

{
public partial class Form1:Form
{
SerialPort ComPort = new SerialPort();
private 队列< byte> recievedData = new 队列< byte>();


public Form1()
{
InitializeComponent();
}

private void ports_Click( object sender,EventArgs e)
{


string [] ArrayComPortsNames = null ;
int index = -1;
string ComPortName = null ;

ArrayComPortsNames = SerialPort.GetPortNames();
执行
{
index + = 1 ;
comboBox1.Items.Add(ArrayComPortsNames [index]);
}

while (!((ArrayComPortsNames [index] == ComPortName)
||(index == ArrayComPortsNames.GetUpperBound( 0 ))));
Array.Sort(ArrayComPortsNames);

// 想要先出
if (index == ArrayComPortsNames.GetUpperBound( 0 ))
{
ComPortName = ArrayComPortsNames [ 0 ];
}
comboBox1.Text = ArrayComPortsNames [ 0 ];
// 波特率值

cboBaudRate.Items.Add ( 300 );
cboBaudRate.Items.Add( 600 );
cboBaudRate.Items.Add( 1200 );
cboBaudRate.Items.Add( 2400 );
cboBaudRate.Items.Add( 9600 );
cboBaudRate.Items.Add( 14400 );
cboBaudRate.Items.Add( 19200 );
cboBaudRate.Items.Add( 38400 );
cboBaudRate.Items.Add( 57600 );
cboBaudRate.Items.Add( 115200 );
cboBaudRate.Items.ToString();
// 获取文本中的第一项打印
cboBaudRate.Text = cboBaudRate。项目[ 0 ]。ToString();
// 握手位
cboHandShaking.Items.Add( );
cboHandShaking.Items.Add( XOnXOff);
cboHandShaking.Items.Add( RequestToSend);
cboHandShaking.Items.Add( RequestToSendXOnXOff);
cboHandShaking.Items.ToString();
cboHandShaking.Text = cboHandShaking.Items [ 0 ]。ToString();

// 数据位
cboDataBits.Items.Add( 7);
cboDataBits.Items.Add( 8);
cboDataBits.Items.ToString();
cboDataBits.Text = cboDataBits.Items [ 0 ]。ToString();
// 停止位
cboStopBits.Items.Add( 一个);
cboStopBits.Items.Add( OnePointFive);
cboStopBits.Items.Add( 两个);
// 获取文本中的第一项打印
cboStopBits.Text = cboStopBits.Items [ 0 ]。ToString();

// 奇偶校验
cboParity.Items.Add(< span class =code-string> );
cboParity.Items.Add( Even);
cboParity.Items.Add( Mark);
cboParity.Items.Add( 奇数);
cboParity.Items.Add( Space);
cboParity.Text = cboParity.Items [ 0 ]。ToString();

}



private void button1_Click( object sender,EventArgs e)
{


if (button1.Text == 已关闭
{
button1.Text = 打开;
ComPort.PortName = Convert.ToString(comboBox1.Text);
ComPort.BaudRate = Convert.ToInt32(cboBaudRate.Text);
ComPort.DataBits = Convert.ToInt32(cboDataBits.Text);
ComPort.StopBits =(StopBits)Enum.Parse( typeof (StopBits),cboStopBits.Text);
ComPort.Handshake =(握手)Enum.Parse( typeof (Handshake),cboHandShaking.Text);
ComPort.Parity =(奇偶校验)Enum.Parse( typeof (Parity),cboParity.Text);

ComPort.Open();
button1.BackColor = Color.Green;
}
else if (button1.Text == 打开
{
button1.Text = 已关闭;
ComPort.Close();
button1.BackColor = Color.Red;
}
}

私有 void textBox1_TextChanged( object sender,EventArgs e)
{

}



public class CommTimer
{
public 计时器tmrComm = new Timer();
public bool timedout = false ;
public CommTimer()
{
timedout = false ;
tmrComm.AutoReset = false ;
tmrComm.Enabled = false ;
tmrComm.Interval = 1000 ; // 默认为1秒
tmrComm.Elapsed + = new ElapsedEventHandler(OnTimedCommEvent);
}

public void OnTimedCommEvent( object source,ElapsedEventArgs e)
{
timedout = true ;
tmrComm.Stop();
}

public void 开始( double timeoutperiod)
{
tmrComm.Interval = timeoutperiod; // 以毫秒为单位的超时时间
tmrComm.Stop();
timedout = false ;
tmrComm.Start();
}


}





private void button2_Click( object sender,EventArgs e)
{
string str = textBox1.Text;
byte [] raw = new byte [textBox1.TextLength/2];
for int i = 0 ; i < raw.Length; i ++)
{
raw [i] = Convert.ToByte(str.Substring(i *) 2 2 ), 16 );



}
ComPort.Write(raw, 0 ,raw.Length);
ComPort.DataReceived + = new SerialDataReceivedEventHandler(sp_DataReceived);


}

private void sp_DataReceived( object sender,SerialDataReceivedEventArgs e)
{





int dataLength = ComPort.BytesToRead;
byte [] data = new byte < /跨度> [DATALENGTH];
int nbrDataRead = ComPort.Read(data, 0 ,data.Length);
if (nbrDataRead == 0
返回;

// 向有兴趣的人发送数据
CommTimer tmrComm = new CommTimer();
tmrComm.Start( 4000 );

while ((ComPort.BytesToRead == 0 )&& (tmrComm.timedout == false ))
{
Application.DoEvents();
}





if (data.Length > 0
{
byte value =( byte )data.GetValue( 0 );
textBox2.Text = Convert.ToString( value );
// 执行您可能需要的其他必要处理。
}

tmrComm.tmrComm.Dispose();
ComPort.DiscardInBuffer();
ComPort.DiscardOutBuffer();
ComPort.Close();

}


private void textBox2_TextChanged( object sender,EventArgs e)
{

}

private void Form1_Load( object sender,EventArgs e)
{

}


}



}

解决方案

这很简单:你有使用语句引用两个命名空间:

< pre lang =cs> 使用 System.Windows.Forms;
...
使用 System.Timers;



所以当你输入类的名称(在本例中为 Timer )编译器无法确定您的意思是哪个计时器: System.Windows.Forms.Timer [ ^ ]或System.Timers.Timer [ ^ ]



所以你必须使用语句删除不必要的;或完全限定所需类的命名空间:

  public  System.Windows.Forms.Timer tmrComm =  new  System.Windows.Forms.Timer(); 





  public  System.Timers.Timer tmrComm =  new  System.Timers.Timer(); 


由于使用指令,您的应用程序中存在名称冲突。

要修复它,您可以完全限定您打算使用的计时器,例如:

  public  System.Timers.Timer tmrComm =  new  System.Timers.Timer(); 


< blockquote>当然你可以写出完整的命名空间路径:

 System.Windows.Forms.Timer MyTimer =  new  System.Windows.Forms .Timer(); 

另一种方法是通过创建.NET 别名为您想要使用的Timer类创建自己有意义的名称:

 使用 SysTimerTimer = System.Timers.Timer; 
使用 WinFormTimer = System.Windows.Forms.Timer;
使用 DiagnosticsStopwatchTimer = System.Diagnostics.Stopwatch;代码中

//

WinFormTimer MyTimer = new WinFormTimer();

在.NET中使用Timer的大多数应用程序都可以从System.Diagnostics中使用Stopwatch类中受益(。 NET 2.0及更高版本):[ ^ ]。

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



namespace serialcomm
    
{ 
    public partial class Form1 : Form
    {
        SerialPort ComPort = new SerialPort();
        private Queue<byte> recievedData = new Queue<byte>();
        
        
        public Form1()
        { 
            InitializeComponent();
        }

        private void ports_Click(object sender, EventArgs e)
        { 
             
            
    string[] ArrayComPortsNames = null;
    int index = -1;
    string ComPortName = null;

    ArrayComPortsNames = SerialPort.GetPortNames();
    do
    {
        index += 1;
        comboBox1.Items.Add(ArrayComPortsNames[index]);
    }

    while (!((ArrayComPortsNames[index] == ComPortName)
                  || (index == ArrayComPortsNames.GetUpperBound(0))));
    Array.Sort(ArrayComPortsNames);

    //want to get first out
    if (index == ArrayComPortsNames.GetUpperBound(0))
    {
        ComPortName = ArrayComPortsNames[0];
    }
    comboBox1.Text = ArrayComPortsNames[0];
            // baud rate values

    cboBaudRate.Items.Add(300);
    cboBaudRate.Items.Add(600);
    cboBaudRate.Items.Add(1200);
    cboBaudRate.Items.Add(2400);
    cboBaudRate.Items.Add(9600);
    cboBaudRate.Items.Add(14400);
    cboBaudRate.Items.Add(19200);
    cboBaudRate.Items.Add(38400);
    cboBaudRate.Items.Add(57600);
    cboBaudRate.Items.Add(115200);
    cboBaudRate.Items.ToString();
    //get first item print in text
    cboBaudRate.Text = cboBaudRate.Items[0].ToString();
            // handshaking bits
    cboHandShaking.Items.Add("None");
    cboHandShaking.Items.Add("XOnXOff");
    cboHandShaking.Items.Add("RequestToSend");
    cboHandShaking.Items.Add("RequestToSendXOnXOff");
    cboHandShaking.Items.ToString();
    cboHandShaking.Text=cboHandShaking.Items[0].ToString();
            
           // data bits
            cboDataBits.Items.Add("7"); 
            cboDataBits.Items.Add("8");
            cboDataBits.Items.ToString();
            cboDataBits.Text = cboDataBits.Items[0].ToString();
            // stop bits
            cboStopBits.Items.Add("One");
            cboStopBits.Items.Add("OnePointFive");
            cboStopBits.Items.Add("Two");
            //get the first item print in the text
            cboStopBits.Text = cboStopBits.Items[0].ToString();

            //Parity 
            cboParity.Items.Add("None");
            cboParity.Items.Add("Even");
            cboParity.Items.Add("Mark");
            cboParity.Items.Add("Odd");
            cboParity.Items.Add("Space");
            cboParity.Text = cboParity.Items[0].ToString();
               
        }

       
        
        private void button1_Click(object sender, EventArgs e)
        { 
           

    if (button1.Text == "Closed")
    {
        button1.Text = "Open";
        ComPort.PortName = Convert.ToString(comboBox1.Text);
        ComPort.BaudRate = Convert.ToInt32(cboBaudRate.Text);
        ComPort.DataBits = Convert.ToInt32(cboDataBits.Text);
        ComPort.StopBits = (StopBits)Enum.Parse(typeof(StopBits), cboStopBits.Text);
        ComPort.Handshake = (Handshake)Enum.Parse(typeof(Handshake), cboHandShaking.Text);
        ComPort.Parity = (Parity)Enum.Parse(typeof(Parity), cboParity.Text);
        
        ComPort.Open();
        button1.BackColor = Color.Green;
    }
    else if (button1.Text == "Open")
    {
        button1.Text= "Closed";
        ComPort.Close();
        button1.BackColor = Color.Red;
    }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }



        public class CommTimer
        {
            public Timer tmrComm = new Timer();
            public bool timedout = false;
            public  CommTimer()
            {
                timedout = false;
                tmrComm.AutoReset = false;
                tmrComm.Enabled = false;
                tmrComm.Interval = 1000; //default to 1 second
                tmrComm.Elapsed += new ElapsedEventHandler(OnTimedCommEvent);
            }

            public void OnTimedCommEvent(object source, ElapsedEventArgs e)
            {
                timedout = true;
                tmrComm.Stop();
            }

            public void Start(double timeoutperiod)
            {
                tmrComm.Interval = timeoutperiod;             //time to time out in milliseconds
                tmrComm.Stop();
                timedout = false;
                tmrComm.Start();
            }


        }



      

        private void button2_Click(object sender, EventArgs e)
        {
            string str = textBox1.Text;
            byte[] raw = new byte[textBox1.TextLength/2];
            for (int i = 0; i < raw.Length; i++)
            {
                raw[i] = Convert.ToByte(str.Substring(i * 2, 2), 16);



            }
           ComPort.Write(raw,0,raw.Length);
           ComPort.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);

           
        }

        private void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {





            int dataLength = ComPort.BytesToRead;
            byte[] data = new byte[dataLength];
            int nbrDataRead = ComPort.Read(data, 0, data.Length);
            if (nbrDataRead == 0)
                return;

            //Send data to whom ever interested
            CommTimer tmrComm = new CommTimer();
            tmrComm.Start(4000);

            while ((ComPort.BytesToRead == 0) && (tmrComm.timedout == false))
            {
                Application.DoEvents();
            }





            if (data.Length > 0)
            {
                byte value = (byte)data.GetValue(0);
                textBox2.Text = Convert.ToString(value);
                //do other necessary processing you may want. 
            }

            tmrComm.tmrComm.Dispose();
       ComPort.DiscardInBuffer();
       ComPort.DiscardOutBuffer();
       ComPort.Close();

        }
                        

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
                        
          
  }

       
  
        }

解决方案

It's pretty simple: you have using statements which reference both namespaces:

using System.Windows.Forms;
...
using System.Timers;


So when you type the name of a class (in this case Timer) the compiler can't be certain which Timer you mean: System.Windows.Forms.Timer[^] or System.Timers.Timer[^]

So you have to either remove the unnecessary using statement; or fully qualify the namespace of the class you wanted:

public System.Windows.Forms.Timer tmrComm = new System.Windows.Forms.Timer();


Or

public System.Timers.Timer tmrComm = new System.Timers.Timer();


There is a name clash in you application, due to using directives.
To fix it you may fully qualify the timer you intend to use, e.g.:

public System.Timers.Timer tmrComm = new System.Timers.Timer();


Of course you can write out the full namespace path:

System.Windows.Forms.Timer MyTimer = new System.Windows.Forms.Timer();

Another way is to create your own meaningful name for which Timer class you want to use by creating a .NET alias:

using SysTimerTimer = System.Timers.Timer;
using WinFormTimer = System.Windows.Forms.Timer;
using DiagnosticsStopwatchTimer = System.Diagnostics.Stopwatch;

// in your code

WinFormTimer MyTimer = new WinFormTimer();

Most applications that use a Timer in .NET can benefit from using the Stopwatch class in System.Diagnostics (.NET 2.0 and later): [^].


这篇关于显示错误::'Timer'是'System.Windows.Forms.Timer'和'System.Timers.Timer'之间的模糊引用..............无法解析plz帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆