Reputation: 83
I'm working on a C# Windows application. I'm using Serial USB port to listening for data from the selected COM port
SerialPort sp;
string t;
void Serial(string port_name)
{
sp = new SerialPort(port_name, 9600, Parity.None, 8, StopBits.One,Handshake.None);
sp.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
sp.Open();
}
private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string list = sp.ReadLine();
listBox1.Items.Add(list);
}
private void Form1_Load(object sender, EventArgs e)
{
t = "COM5";
Serial(t);
}
But I get an error
'SerialPort' does not contain a constructor that takes 6 arguments
What is the problem here? If anyone knows please help me.
Upvotes: 3
Views: 697
Reputation: 21
Handshake is not one of the parameter is SerialPort constructor. There is a property "Handshake" in SerialPort class which can be set. Default value is none. https://msdn.microsoft.com/en-us/library/system.io.ports.serialport.handshake(v=vs.110).aspx
Upvotes: 0
Reputation: 5764
From MSDN SerialPort class haven't construcotr with 6 params.
SerialPort(String, Int32, Parity, Int32, StopBits)
- Initializes a new instance of the SerialPort class using the specified port name, baud rate, parity bit, data bits, and stop bit.
Handshake - you cannot set it in constructor. You can set it in this way:
sp.Handshake = Handshake.None;
Upvotes: 1
Reputation: 35378
Well there are only those constructors
SerialPort()
SerialPort(IContainer)
SerialPort(String)
SerialPort(String, Int32)
SerialPort(String, Int32, Parity)
SerialPort(String, Int32, Parity, Int32)
SerialPort(String, Int32, Parity, Int32, StopBits)
So you probably want to change your initialization statement from
sp = new SerialPort(port_name, 9600, Parity.None, 8, StopBits.One,Handshake.None);
to
sp = new SerialPort(port_name, 9600, Parity.None, 8, StopBits.One);
sp.Handshake = Handshake.None;
Upvotes: 4
Reputation:
There is no Handshake in Constructor, you must do it like this :
sp = new SerialPort(port_name, 9600, Parity.None, 8, StopBits.One);
sp.Handshake = Handshake.None;
Upvotes: 4