rossmann-engineering / EasyModbusTCP.NET

Modbus TCP, Modbus UDP and Modbus RTU client/server library for .NET implementations
928 stars 402 forks source link

Cannot set Parity to None #38

Closed darkmansad closed 4 years ago

darkmansad commented 4 years ago

Cannot set the parity to 'None' . If we make the following code:

var _modbusClient=new ModbusClient(); _modbusClient.Port = 6; _modbusClient.Parity = System.IO.Ports.Parity.None; _modbusClient.StopBits = System.IO.Ports.StopBits.Two; _modbusClient.Connect();

this code will attempt to connect with parity Even, i think this is a problem of the setter where the parity cannot be set if the 'if (serialport != null)' If the constructor is adapted and is passed also the 'COM6' parameter the setter for paritity parameter will work.

Padanian commented 4 years ago

Isn't it Port the TCP/IP port? Shouldn't it be Serialport?

darkmansad commented 4 years ago

Isn't it Port the TCP/IP port? Shouldn't it be Serialport?

It's for serial port.

Padanian commented 4 years ago

No, it isn't. port

darkmansad commented 4 years ago

What I wanted to say : 1) If you're making var _modbusClient=new ModbusClient(); 2) and you want to use setter for parity in the following line _modbusClient.Parity = System.IO.Ports.Parity.None; in your code you have public Parity Parity { get { if (serialport != null) return parity; else return Parity.Even; } set { if (serialport != null) parity = value; } } - it's allowing you to set the parity just if the serialport variable is not null. In my scenario i wanted to use generic piece of code at the start and based on the configuration that i have in database(Serial or TCP) i set the parameters according to connection type. After this i'm calling Connect() function. Take a look on following example var _modbusClient=new ModbusClient(); if(_databaseTypeIsTCP){ //set serial data _modbusClient.Port = 6; _modbusClient.Parity = System.IO.Ports.Parity.None; // this line will not set parity because 'serialport' -variable is null _modbusClient.StopBits = System.IO.Ports.StopBits.Two; } else{ //set tcp data _modbusClient.IPAddress= "192.168.1.10"; _modbusClient.Port ="9800"; } _modbusClient.Connect();

Padanian commented 4 years ago

Your example works if amended like this:

`if !(_databaseTypeIsTCP){ //set serial data _modbusClient.SerialPort = "COM6";

//etc etc }