![]() |
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);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 |
Something along the lines of
:
if (parityBox.SelectedIndex == -1) //Nothing selected, add an error messageIt can be a lot cleaner by using more advanced language features which you probably aren't familiar with just yet. |
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. |
:
if (parityBox.SelectedIndex == -1) //Nothing selected, add an error message |
Ahhh I see now, Thankyou very much. that will help me with many potential questions I won't have to ask in the future.
|
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; |
If you wish to clean things up, and learn a little on the way, look in to the switch statement.
|
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)9600,N,8,1 typical setting Flow control is None as well |
Quote:
:
|
I might have misinterpreted your post, but
Quote:
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