Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C# (http://www.programmingforums.org/forum16.html)
-   -   Hi New member with System.IO.Ports.Parity Question (http://www.programmingforums.org/showthread.php?t=14128)

metron9 Oct 7th, 2007 5:09 AM

Hi New member with System.IO.Ports.Parity Question
 
I am using VSC# 2005

I am confused with the enumeration type System.IO.Ports.Parity
I understand the concept of enumeration but I dont understand the syntax I need to set two fields in a serialport component in a Form.

The serialport component is in the form and you can set the parity and stop bits using a drop down combobox in the Properties. I don't know how to do it using program code.

:

            serialPort1.BaudRate = int.Parse(baudRate.Text);
            serialPort1.DataBits = int.Parse(dataBits.Text);
            serialPort1.Parity = ?
            serialPort1.PortName = comPort.Text;
            serialPort1.StopBits = ?



I have only been programming for three days with this language and I really like it. I work on AVR microntrollers in assembler most of the time and i am designing a PC interface for DMX lighting control. Here is a png file of my screen and my first interface window of the project to set up the serial port. it is all working except for the parity and stop bits.

http://acousticlights.com/serialport.png

Dameon Oct 7th, 2007 1:29 PM

Something along the lines of

:

if (parityBox.SelectedIndex == -1) //Nothing selected, add an error message
  return;
if (parityBox.SelectedIndex == 0) //First item selected
  serialPort1.Parity = ...;
...


It can be a lot cleaner by using more advanced language features which you probably aren't familiar with just yet.

metron9 Oct 7th, 2007 3:03 PM

You stopped short of answering the question I was driving at however.

SerialPort1.Parity = ...

serialPort.Parity is not an int type it is a System.IO.Ports.Parity type
(and I dont understand that fully)

So trying some of the following I get errors

serialPort1.Parity = parityComboBox.SelectedIndex;

error is cannot implicity convert type 'int' to Syetem.IO.Ports.Parity. An explicit conversion exists (are you missing a cast?)

I try

serialPort1.Parity = (int)parityComboBox.SelectedIndex;

serialPort1.Parity = parityComboBox.SelectedItem;

serialPort1.Parity = parityComboBox.SelectedValue;

So the Combo box named "parityComboBox" has a collection of strings

Even
Odd
None
Mark
Space

I understand element 0 is Even and element 4 is Space

I don't understand how to get the selected item into the property parity of the Component SerialPort.

Dameon Oct 7th, 2007 3:19 PM

:

if (parityBox.SelectedIndex == -1) //Nothing selected, add an error message
  return;
if (parityBox.SelectedIndex == 0) //First item selected
  serialPort1.Parity = System.IO.Ports.Parity.Even;
...


metron9 Oct 7th, 2007 5:06 PM

Ahhh I see now, Thankyou very much. that will help me with many potential questions I won't have to ask in the future.

metron9 Oct 7th, 2007 5:47 PM

1 Attachment(s)
Here is my code to test a serial port for communication.
If you have any comments on anything about it I would welcome them as i learn this language.

Thank.s again, i hope this helps others using the serial port.

:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;



namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
 
        public Form1()
        {
            InitializeComponent();

// Clear the combobox
            comPort.Items.Clear();
         
// get com port list from the system into combobox comport
            foreach (string s in System.IO.Ports.SerialPort.GetPortNames())
            {
                comPort.Items.Add(s);
            }
            comPort.SelectedIndex = 0;
            baudRate.SelectedIndex = 0;
            dataBits.SelectedIndex = 3;
            parityComboBox.SelectedIndex = 0;
            stopBits.SelectedIndex = 0;
            flowControl.SelectedIndex = 2;
        }
        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void button1_Click(object sender, EventArgs e)
        {

//Set port name
            serialPort1.PortName = comPort.Text;

//set baud rate
            serialPort1.BaudRate = int.Parse(baudRate.Text);

//set data bits
            serialPort1.DataBits = int.Parse(dataBits.Text);

//Set parity
            if (parityComboBox.SelectedIndex == -1)
            {
                MessageBox.Show("Unable to set parity");
                return;
            }
            if (parityComboBox.SelectedIndex == 0)
            {
                serialPort1.Parity = System.IO.Ports.Parity.Even;
            }
            if (parityComboBox.SelectedIndex == 1)
            {
                serialPort1.Parity = System.IO.Ports.Parity.Odd;
            }
            if (parityComboBox.SelectedIndex == 2)
            {
                serialPort1.Parity = System.IO.Ports.Parity.None;
            }
            if (parityComboBox.SelectedIndex == 3)
            {
                serialPort1.Parity = System.IO.Ports.Parity.Mark;
            }
            if (parityComboBox.SelectedIndex == 4)
            {
                serialPort1.Parity = System.IO.Ports.Parity.Space;
            }

// Set stop bits

            if (stopBits.SelectedIndex == -1)
            {
                MessageBox.Show("Unable to set Stop Bits");
                return;
            }
            if (stopBits.SelectedIndex == 1)
            {
                serialPort1.StopBits = System.IO.Ports.StopBits.None;
            }
            if (stopBits.SelectedIndex == 2)
            {
                serialPort1.StopBits = System.IO.Ports.StopBits.One;
            }
            if (stopBits.SelectedIndex == 3)
            {
                serialPort1.StopBits = System.IO.Ports.StopBits.OnePointFive;
            }
            if (stopBits.SelectedIndex == 4)
            {
                serialPort1.StopBits = System.IO.Ports.StopBits.Two;
            }


// Open port
            try
            {
                serialPort1.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }

// send data
            try
            {
                serialPort1.WriteLine(textToSend.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                try
                {
                    serialPort1.Close();
                }
                catch (Exception ex1)
                    {
                        MessageBox.Show(ex1.Message);
                        return;
                    }
                return;
            }

// success , close port and return
            serialPort1.Close();
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
     


        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void comPort_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void parity_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}


Dameon Oct 7th, 2007 7:19 PM

If you wish to clean things up, and learn a little on the way, look in to the switch statement.

metron9 Oct 7th, 2007 7:29 PM

Another question just surfaced i could use your help on if you would be so kind,

The serial port Stop bits is not working during run time for One

the line
:

serialPort1.StopBits = System.IO.Ports.StopBits.One;

Compiles but I get an exception

The SerialPort1 Properties has One as a choice so I am stuck again.

( I put the whole block in a trycatch so it looks like this)

:

  if (stopBits.SelectedIndex == -1)
            {
                MessageBox.Show("Unable to set Stop Bits");
                return;
            }
           
            try
            {

                if (stopBits.SelectedIndex == 1)
                {
                    serialPort1.StopBits = System.IO.Ports.StopBits.None;
                }
                if (stopBits.SelectedIndex == 2)
                {
                    serialPort1.StopBits = System.IO.Ports.StopBits.One;
                }
                if (stopBits.SelectedIndex == 3)
                {
                    serialPort1.StopBits = System.IO.Ports.StopBits.OnePointFive;
                }
                if (stopBits.SelectedIndex == 4)
                {
                    serialPort1.StopBits = System.IO.Ports.StopBits.Two;
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }



9600,N,8,1 typical setting Flow control is None as well

metron9 Oct 7th, 2007 7:39 PM

Quote:

Originally Posted by Dameon (Post 134904)
If you wish to clean things up, and learn a little on the way, look in to the switch statement.

Ah yes like this:
:


 switch (parityComboBox.SelectedIndex)
            {
                case -1:
                MessageBox.Show("Unable to set parity");
                return;
                case 0:
                serialPort1.Parity = System.IO.Ports.Parity.Even;
                break;
                case 1:
                serialPort1.Parity = System.IO.Ports.Parity.Odd;
                break;
                case 2:
                serialPort1.Parity = System.IO.Ports.Parity.None;
                break;
                case 3:
                serialPort1.Parity = System.IO.Ports.Parity.Mark;
                break;
                case 4:
                serialPort1.Parity = System.IO.Ports.Parity.Space;
                break;
            }


DaWei Oct 7th, 2007 9:04 PM

I might have misinterpreted your post, but
Quote:

9600,N,8,1
Means 9600 baud, no parity, 8 bits, 1 stop bit.

You can get an exception if the port is in an invalid state or if the attempt to set failed for some reason, as well as for using an invalid value.

Have you read this, by any chance?


All times are GMT -5. The time now is 3:19 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC