Should I queue a Nmodbus4 RTU connection by Mutex?

杀马特。学长 韩版系。学妹 提交于 2021-01-28 07:26:00

问题


I use library NModbus4 and create RTU connection.

public static IModbusSerialMaster Master { get; set; }
Master = ModbusSerialMaster.CreateRtu(SerialPort);

I have method GetClient()which return Master and Method Registers() which look like:

    public static ushort[] Registers(Func<IModbusSerialMaster, ushort[]> action)
    {
        ushort[] registers = new ushort[0];
        var client = GetClient();
        if (client == null)
            return registers;
        //mutex.WaitOne();
        try
        {                
            registers = action.Invoke(client);
        }
        catch (Exception e)
        {
            log.Error("error");
        }
        finally
        {
            //mutex.ReleaseMutex();
        }
        return registers;
    }

I was trying to use System.Threading.Mutexto be sure that only one method will send frames at a time. But after about minute when In loop are run 2/3 task it locks on mutex.WaitOne(); and stop program.

If I not using mutex. I do not see any mistakes. Does NModbus itself ensure that the program does not crash in this way? Or I should find why is this happening, leave a mutex and fix the error?

来源:https://stackoverflow.com/questions/63521366/should-i-queue-a-nmodbus4-rtu-connection-by-mutex

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!