tags:

views:

31

answers:

2

What is the equivalent of the following command in C#? comm.Settings = "2400,N,8,1"

+1  A: 

For the SerialPort class, that's Baudrate = 2400, Parity = None, DataBits = 8, StopBits = One. Only the Baudrate has a non-default setting.

Hans Passant
Strange downvote. Anything factually wrong with this answer, love to hear what it might be.
Hans Passant
Parity = None and StopBits = One... that's factually wrong. Should be Parity = Parity.None and StopBits = StopBits.One, this and the fact you don't provide any info that directly references the SerialPort Class in anything but the loosest terms. OP asked for C#...
slomojo
Thanks for you feedback @slo, there certainly was an implicit assumption that the OP would use the designer to set these properties. The Properties window can be quite handy for programmers that haven't quite mastered the skill of translating a setting string to code. Of course, it doesn't bother with enum types. Clearly the OP was happy with the answer, not quite sure if it behooves you to disagree with that. It is also quite gauche to downvote answers in a thread that you posted to. Your answer should stand on its own without manipulation. It certainly doesn't look like it needs help.
Hans Passant
@Hans, the OP just asked for C# command equivalent. I marked your answer based on how well you addressed the question, I then decided to answer comprehensively. Obviously the OP is happy with your response anyway. If you edit the answer, I'll re-mark it.
slomojo
+4  A: 

Use the following example...:

You will require this using:

using System.IO.Ports;

And in your code...

SerialPort comPort = new SerialPort("port", 2400, Parity.None, 8, StopBits.One); 
// This is one of 7 possible method overloads.

You can also change the settings of the SerialPort instance using these properties.

comPort.PortName = "port"; //PortName (string)
comPort.DataBits = 8; //DataBits (int) 5..8

comPort.BaudRate = 2400; //BaudRate (int) 
                   // Default is 9600, can be up to the maximum permitted by the hardware.

comPort.StopBits = StopBits.One; //StopBits 
                   // (StopBits.One, StopBits.None, StopBits.None, StopBits.Two, StopBits.OnePointFive)

comPort.Parity = Parity.None; //Parity 
                 // (Parity.Odd, Parity.Even, Parity.None, Parity.Mark, Parity.Space)
slomojo