Skip to content

Commit

Permalink
examples/modbus: Fix modbus example to keep running
Browse files Browse the repository at this point in the history
  • Loading branch information
acassis committed Dec 3, 2023
1 parent 6a8344b commit 26e6966
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions examples/modbus/modbus_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ static inline int modbus_initialize(void)

/* Verify that we are in the stopped state */

if (g_modbus.threadstate != STOPPED)
if (g_modbus.threadstate == RUNNING)
{
fprintf(stderr, "modbus_main: "
"ERROR: Bad state: %d\n", g_modbus.threadstate);
Expand Down Expand Up @@ -432,20 +432,22 @@ int main(int argc, FAR char *argv[])

/* Handle command line arguments */

g_modbus.quit = false;
g_modbus.quit = true;

while ((option = getopt(argc, argv, "desqh")) != ERROR)
{
switch (option)
{
case 'd': /* Disable protocol stack */
pthread_mutex_lock(&g_modbus.lock);
g_modbus.threadstate = SHUTDOWN;
pthread_mutex_unlock(&g_modbus.lock);
break;

case 'e': /* Enable the protocol stack */
{
/* Keep running, otherwise the thread will die */

g_modbus.quit = false;

ret = modbus_create_pollthread();
if (ret != OK)
{
Expand Down Expand Up @@ -481,7 +483,6 @@ int main(int argc, FAR char *argv[])
break;

case 'q': /* Quit application */
g_modbus.quit = true;
pthread_kill(g_modbus.threadid, 9);
break;

Expand All @@ -497,6 +498,13 @@ int main(int argc, FAR char *argv[])
}
}

/* Don't exit until the thread finishes */

if (!g_modbus.quit)
{
pthread_join(g_modbus.threadid, NULL);
}

return EXIT_SUCCESS;
}

Expand Down

0 comments on commit 26e6966

Please sign in to comment.