简体   繁体   中英

C# 'SerialPort' does not contain a constructor that takes 6 arguments

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

On here 在此输入图像描述

What is the problem here? If anyone knows please help me.

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;

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;

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;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM