• (6 Pages)
  • +
  • « First
  • 4
  • 5
  • 6

Serial Port Communication in C#

#76 ocaccy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-August 12

Posted 29 August 2012 - 08:27 PM

I am not able to get the InstanceName!
Could you help me?

I need show in comboBoxUSB only USB Serial Port?
Thanks in advance,
ocaccy

        private void Form1_Load(object sender,EventArgs e)
            {
            CommPort com=CommPort.Instance;
            int foundRSP=0;

            string[] portList=com.GetAvailablePorts();
            #region This code takes all the serial ports
            for(int i=0;i<portList.Length;++i)
                {
                string name=portList[i];
                comboBox1.Items.Add(name);
                if(name==Settings.Port.PortName)
                    foundRSP=i;
                }

            if(portList.Length>0)
                comboBox1.SelectedIndex=foundRSP;
            #endregion

            ManagementObjectSearcher searcher=new ManagementObjectSearcher("root\\WMI","SELECT * FROM MSSerial_PortName");
            string[] USBPorts=System.IO.Ports.SerialPort.GetPortNames();
            #region In this code I need only the USB Serial Port

            //FriendlyName
            //PID & VID

            foreach(ManagementObject queryObj in searcher.Get)
                {
                lbl_InstanceName.Text="InstanceName: {0}"+queryObj["InstanceName"];
                lbl_PortName.Text="PortName: {0}"+queryObj["PortName"];

                //If the serial port's instance name contains USB Serial Port
                //it must be a USB to serial device
                if(queryObj["InstanceName"].ToString().Contains("USB Serial Port"))
                    {
                    comboBoxUSB.Items.Add(queryObj["PortName"]);
                    }
                }
            #endregion

            Int32[] baudRates= {
                100,300,600,1200,2400,4800,9600,14400,19200,
                38400,56000,57600,115200,128000,256000,0
            };
            foundRSP=0;
            for(int i=0;baudRates[i]!=0;++i)
                {
                comboBox2.Items.Add(baudRates[i].ToString());
                if(baudRates[i]==Settings.Port.BaudRate)
                    foundRSP=i;
                }
            comboBox2.SelectedIndex=foundRSP;

            comboBox3.Items.Add("5");
            comboBox3.Items.Add("6");
            comboBox3.Items.Add("7");
            comboBox3.Items.Add("8");
            comboBox3.SelectedIndex=Settings.Port.DataBits-5;

            foreach(string s in Enum.GetNames(typeof(Parity)))
                {
                comboBox4.Items.Add(s);
                }
            comboBox4.SelectedIndex=(int)Settings.Port.Parity;

            foreach(string s in Enum.GetNames(typeof(StopBits)))
                {
                comboBox5.Items.Add(s);
                }
            comboBox5.SelectedIndex=(int)Settings.Port.StopBits;

            foreach(string s in Enum.GetNames(typeof(Handshake)))
                {
                comboBox6.Items.Add(s);
                }
            comboBox6.SelectedIndex=(int)Settings.Port.Handshake;

            switch(Settings.Option.AppendToSend)
                {
                case Settings.Option.AppendType.AppendNothing:
                    radioButton1.Checked=true;
                    break;
                case Settings.Option.AppendType.AppendCR:
                    radioButton2.Checked=true;
                    break;
                case Settings.Option.AppendType.AppendLF:
                    radioButton3.Checked=true;
                    break;
                case Settings.Option.AppendType.AppendCRLF:
                    radioButton4.Checked=true;
                    break;
                }

            checkBox1.Checked=Settings.Option.HexOutput;
            checkBox2.Checked=Settings.Option.MonoFont;
            checkBox3.Checked=Settings.Option.LocalEcho;
            checkBox4.Checked=Settings.Option.StayOnTop;
            checkBox5.Checked=Settings.Option.FilterUseCase;
            }



Was This Post Helpful? 0
  • +
  • -

#77 Fred_Lee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 11-September 12

Posted 11 September 2012 - 01:54 PM

Great Tutorial.
I found I was unable to close the port, so tried the code below:

        

private void cmdClose_Click(object sender, EventArgs e)
        {
            comm.PortName = cboPort.Text;
            comm.ClosePort();
            
            cmdOpen.Enabled = true;
            cmdClose.Enabled = false;
            cmdSend.Enabled = true; 
        }





Was This Post Helpful? 0
  • +
  • -

#78 Fred_Lee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 11-September 12

Posted 11 September 2012 - 02:23 PM

Great Tutorial.
I found I was unable to close the port, so tried the code below:

in frmMain.cs
        private void cmdClose_Click(object sender, EventArgs e)
        {
            comm.PortName = cboPort.Text;
            comm.ClosePort();
          

            cmdOpen.Enabled = true;
            cmdClose.Enabled = false;
            cmdSend.Enabled = true;
        }
        



also in CommunicationManager.cs

        #region ClosePort
        public bool ClosePort()
        {
            try
            {
                //first check if the port is already open
                //if its open then close it
                if ((comPort.IsOpen == true) && (comPort.BytesToRead == 0)) comPort.Close();

                //display message
                DisplayData(MessageType.Normal, "Port closed at " + DateTime.Now + "\n");
                //return false
                return false;
            }
            catch (Exception ex)
            {
                DisplayData(MessageType.Error, ex.Message);
                return true;
            }
        } 
        #endregion
        


it seems to work OK.

I am puzzled by the code below:

in frmMain.cs
        private void rtbDisplay_TextChanged(object sender, EventArgs e)
        {

        }
        

It does'nt seem to be referenced anywhere. What does it do?
Any ideas would be appreciated.
Was This Post Helpful? 0
  • +
  • -

#79 arjunwalmiki  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 19-November 12

Posted 27 November 2012 - 10:38 PM

View PostTenRC, on 14 January 2009 - 01:34 PM, said:

The downloaded file is corrupt or incomplete. Please test and verify. I have uploaded what I received.

sir it is very nice tutorial.

know i have done little change in that program
1)I was all data those are display rtbDisplay (RichTextBox) it will save at my desktop in txt file the code are as below:-

private void cmdOpen_Click(object sender, EventArgs e)
{
comm.PortName = cboPort.Text;
comm.Parity = cboParity.Text;
comm.StopBits = cboStop.Text;
comm.DataBits = cboData.Text;
comm.BaudRate = cboBaud.Text;
if (rtbDisplay != null)
{
comm.DisplayWindow = rtbDisplay;
// FileMode.Append
StreamWriter sw = new StreamWriter("C:\\Users\\Arjun\\Desktop\\test.txt");

sw.WriteLine(rtbDisplay.Text);
sw.Close();


}
Know i want to hide the rtbDisplay(Rich Text Box) and write same data in test.txt with \n .so pls help me wt i do any body pls

But i want to display
Was This Post Helpful? 0
  • +
  • -

#80 arjunwalmiki  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 19-November 12

Posted 27 November 2012 - 11:05 PM

View PostFred_Lee, on 11 September 2012 - 02:23 PM, said:

Great Tutorial.
I found I was unable to close the port, so tried the code below:

in frmMain.cs
        private void cmdClose_Click(object sender, EventArgs e)
        {
            comm.PortName = cboPort.Text;
            comm.ClosePort();
          

            cmdOpen.Enabled = true;
            cmdClose.Enabled = false;
            cmdSend.Enabled = true;
        }
        



also in CommunicationManager.cs

        #region ClosePort
        public bool ClosePort()
        {
            try
            {
                //first check if the port is already open
                //if its open then close it
                if ((comPort.IsOpen == true) && (comPort.BytesToRead == 0)) comPort.Close();

                //display message
                DisplayData(MessageType.Normal, "Port closed at " + DateTime.Now + "\n");
                //return false
                return false;
            }
            catch (Exception ex)
            {
                DisplayData(MessageType.Error, ex.Message);
                return true;
            }
        } 
        #endregion
        


it seems to work OK.

I am puzzled by the code below:

in frmMain.cs
        private void rtbDisplay_TextChanged(object sender, EventArgs e)
        {

        }
        

It does'nt seem to be referenced anywhere. What does it do?
Any ideas would be appreciated.




pLS Check all code when we click on closed button so no need of
//display message
DisplayData(MessageType.Normal, "Port closed at " + DateTime.Now + "\n");

code it is create confusion
Was This Post Helpful? 0
  • +
  • -

#81 bvdfeen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 13-December 14

Posted 13 December 2014 - 11:25 AM

View Postth4k1dd, on 20 March 2008 - 06:46 AM, said:

Hello, I was so excited to find this post and can't wait to play. I did run into one problem I am having with the code. I went ahead and threw it into C# and compiled. The program detects my one and only COM port, COM12 and adds it to the port drop down box like it should.

However, when I hit the Open Port button, it throws up an error in the Rich Text Box which says that COM1 does not exist. Of course that was not a mistype. I select COM12 and yet it tries to connect to COM1.

I took a look at the code and followed the _portName variable but was unable to see why it would cut off the 2 in 12. It would seem it should not as the variable is setup as a string.

Quick Info:
Windows Vista Business
Microsoft .Net Visual Studio 2008 Express
Arduino USB Microcontroller Card
Port Settings (should be): COM12, 9600, None, 1, 8
Programs Receiving COM12: Arduino Software, Putty, & Realterm

Am I missing something? You help is greatly appreciated.

One thing I noticed...I went into Device Manager and changed the USB Virtual COM port to use COM1 and for some reason the program works fine. Also works great if changed to COM2 in Device Manager.

Any ideas?

* Needs to read up on variable tracing *


First of all... A great piece of educating code PsychoCoder!!

Ran into the same issue with the example code, it's only one little issue though.
When checking
 private void cmdOpen_Click(object sender, EventArgs e) 

in frmMain.cs I saw it missed the line
             comm.PortName = cboPort.Text; 

after adding that not only COM1 works, but any COM port.

Thanks a lot for this great resource!

Cheers
Ben
Was This Post Helpful? 0
  • +
  • -

  • (6 Pages)
  • +
  • « First
  • 4
  • 5
  • 6