apache / nuttx

Apache NuttX is a mature, real-time embedded operating system (RTOS)
https://nuttx.apache.org/
Apache License 2.0
2.85k stars 1.17k forks source link

Modbus Master example was broken by PR #6038 #11666

Closed acassis closed 8 months ago

acassis commented 9 months ago

While helping @JorgeGzm to get Modbus Master working on his board we discovered that the mailing example was not working. Then I did some tests with different release versions and discovered it was broken between 10.3 and 11.0!

Then we did a git bisect and discovered that this PR https://github.com/apache/nuttx/pull/6038 was responsible for generating the issue.

Jorge discovered that adding a usleep() before acquiring the semaphore on FreeModbus helped to fix the issue, but we still investigation why that commit cause this issue. Hi @anjiahao1 could you please take a look? Maybe you can have some idea why this issue is happening. If you want to test the Modbus Example, I have a tutorial here: https://www.youtube.com/watch?v=FmJLrG4Ldow

JorgeGzm commented 9 months ago

Hi,

Testing the modbus master example: nuttx-apps/examples/modbusmaster/mbmaster_main.c

Whe check, if we add a delay before call the function eMBMasterReqReadHoldingRegister the problem stop and the the aplication back to work.

printf("Sending %d requests to slave %d\n",
         MBMASTER_REQUESTS_COUNT, SLAVE_ID);

  usleep(1000); // Delay ADD

  /* modbus is initialized and polling thread is running */
  while (reqcounter < MBMASTER_REQUESTS_COUNT)
    {
      g_mbmaster.statistics.reqcount++;
      mberr = eMBMasterReqReadHoldingRegister(SLAVE_ID,
                                              SLAVE_STATUS_REG, 1, 1000);
      if (mberr != MB_MRE_NO_ERR)
        {
          fprintf(stderr, "mbmaster_main: "
                  "ERROR: holding reg %d read failed: %d\n",
                  SLAVE_STATUS_REG, mberr);

          g_mbmaster.statistics.errcount++;
        }

      reqcounter++;
    }
xiaoxiang781216 commented 9 months ago

look like eMBMasterPoll can't be called before eMBMasterReqReadHoldingRegister? https://github.com/apache/nuttx/pull/6038 optimize the speed of thread creation, the new thread may run before pthread_create return.