我正在尝试使用NModbus库创建一个Modbus阅读器应用程序,但是当连接到端口时,它会引发以下错误:
System.NotImplementedException: Function code 123 not supported.
at Modbus.IO.ModbusRtuTransport.ResponseBytesToRead(Byte[] frameStart)
at Modbus.IO.ModbusRtuTransport.ReadResponse[T]()
at Modbus.IO.ModbusTransport.UnicastMessage[T](IModbusMessage message)
at Modbus.Device.ModbusMaster.PerformReadRegisters(ReadHoldingInputRegistersReques t request)
at Modbus.Device.ModbusMaster.ReadHoldingRegisters(Byte slaveAddress, UInt16
startAddress, UInt16 numberOfPoints)
at ModbusReaderSnippet.Program.Main(String[] args).
这是我的代码的摘要:
static void Main(string[] args)
{
Console.WriteLine("Please enter the com port :");
string comPort = Console.ReadLine();
Console.WriteLine("Please enter slave id :");
byte slave = Convert.ToByte(Console.ReadLine());
Console.WriteLine("Please enter starting register number :");
ushort startRegister = Convert.ToUInt16(Console.ReadLine());
Console.WriteLine("Please enter no. of registers to read :");
ushort noOfRegisters = Convert.ToUInt16(Console.ReadLine());
bool flag = true;
Console.WriteLine("Please enter baud rate : ");
int baud = Convert.ToInt32(Console.ReadLine());
try
{
while (flag)
{
SerialPort port = new SerialPort(comPort.ToUpper());
port.BaudRate = baud;
port.DataBits = 8;
port.Parity = Parity.None;
port.StopBits = StopBits.One;
port.ReadTimeout = 500000000;
port.WriteTimeout = 500000000;
Console.WriteLine("Opening port");
if (port.IsOpen == true)
{
port.Close();
port.Open();
}
else
{
port.Open();
}
Console.WriteLine("Opened port : {0}", comPort.ToUpper());
IModbusSerialMaster master = ModbusSerialMaster.CreateRtu(port);
byte slaveId = slave;
ushort startAddress = startRegister;
ushort numRegisters = noOfRegisters;
ushort[] registers = master.ReadHoldingRegisters(slaveId, startAddress, numRegisters);
for (int j = 0; j < numRegisters; j++)
{
Console.WriteLine("Register {0} = {1} of Slave{2}", startAddress + j, registers[j], slaveId);
}
port.Close();
Console.WriteLine("Do you want to continue ? Y/N");
string perm = Console.ReadLine();
flag = perm == "Y" || perm == "y" ? true : false;
if (perm == "Y" || perm == "y")
{
Console.WriteLine("Please enter slave id : ");
slave = Convert.ToByte(Console.ReadLine());
}
}
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
}
Console.ReadLine();
}
当我尝试读取寄存器3961
至3971
或任何其他寄存器时,会出现上述错误。
我在Nmodbus库中查找,发现在switch case
方法的Modbus.IO.ModbusRtuTransport.ResponseBytesToRead(Byte[] frameStart)
语句中没有以9600波特率从电表接收到的响应,该方法读取响应并将寄存器值发回。
问题:如何解决该问题。以2400波特率打开端口,但不轮询任何值。