Click here to Skip to main content
16,013,824 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
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)
        {

        }
                        
          
  }

       
  
        }
Posted
Updated 28-Oct-14 21:16pm
v2

It's pretty simple: you have using statements which reference both namespaces:
C#
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:
C#
public System.Windows.Forms.Timer tmrComm = new System.Windows.Forms.Timer();

Or
C#
public System.Timers.Timer tmrComm = new System.Timers.Timer();
 
Share this answer
 
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.:
C#
public System.Timers.Timer tmrComm = new System.Timers.Timer();
 
Share this answer
 
Of course you can write out the full namespace path:
C#
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:
C#
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): [^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900