Skip to content

com.serotonin.modbus4j.exception.IllegalFunctionException: Function code: 0x7e,读取寄存器问题 #20

Description

@SummerMineJack

作者你好,请问使用modbus4j 读取寄存器时发生异常com.serotonin.modbus4j.exception.IllegalFunctionException: Function code: 0x7e,我的modbus协议中03是读取寄存器的,总共有29个寄存器,我需要开启service后台轮询

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        CoroutineScope(Dispatchers.IO).launch {
            delay(2000)
            while (true) {
                for (i in 1..batteriesRepo.model.batteryNumber) {
                    try {
                        val sendReadTemperatureCommand = appMaintenanceRepo.sendReadCommandData(i)
                        val data = sendReadTemperatureCommand.data
                        batteriesInfoModel = appMaintenanceRepo.parseFinallyDataToUseAllBatteriesInfo(data, sendReadTemperatureCommand.slaveId)
                    } catch (e: ModbusRespException) {
                        Tracker.serialPortErrorLog("通道:" + i + " 读取电池信息失败:" + SerialPortResultCode.getErrorMessage(e.exceptionCode.toInt()) + " 寄存器开始地址:" + startRegister0x1000 + " 寄存器数量:" + register0x1000Count)
                        batteriesInfoModel = ReadOnlyRegisterModel().apply { batteriesStorageIndex = i }
                    } catch (e: ModbusTransportException) {
                        LogUtils.e("读取信息失败:" + e.slaveId)
                        batteriesInfoModel = ReadOnlyRegisterModel().apply { batteriesStorageIndex = i }
                    }
                   //这里需要将原始数据进行保存
                    Realm.getDefaultInstance().apply {
                        beginTransaction()
                        copyToRealmOrUpdate(batteriesInfoModel)
                        commitTransaction()
                        close()
                    }
                }
                delay(100)
            }
        }
        return START_STICKY
    }

我看了其他 的issues,你说不能一次读太多的寄存器,那最大能承受多少

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions