Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

examples/modbus: Fix modbus example to keep running #2217

Merged
merged 1 commit into from
Dec 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions examples/modbus/modbus_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@
#endif
pthread_t threadid;
pthread_mutex_t lock;
volatile bool quit;
};

/****************************************************************************
Expand Down Expand Up @@ -160,12 +159,12 @@

static inline int modbus_initialize(void)
{
eMBErrorCode mberr;

Check failure on line 162 in examples/modbus/modbus_main.c

View workflow job for this annotation

GitHub Actions / check

Mixed case identifier found
int status;

/* 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 @@ -193,7 +192,7 @@
* CONFIG_EXAMPLES_MODBUS_PARITY = parity, default=MB_PAR_EVEN
*/

mberr = eMBInit(MB_RTU, 0x0a, CONFIG_EXAMPLES_MODBUS_PORT,

Check failure on line 195 in examples/modbus/modbus_main.c

View workflow job for this annotation

GitHub Actions / check

Mixed case identifier found
CONFIG_EXAMPLES_MODBUS_BAUD,
CONFIG_EXAMPLES_MODBUS_PARITY);
if (mberr != MB_ENOERR)
Expand All @@ -211,7 +210,7 @@
* 3 = Length of additional values (in bytes)
*/

mberr = eMBSetSlaveID(0x34, true, g_slaveid, 3);

Check failure on line 213 in examples/modbus/modbus_main.c

View workflow job for this annotation

GitHub Actions / check

Mixed case identifier found
if (mberr != MB_ENOERR)
{
fprintf(stderr, "modbus_main: "
Expand All @@ -221,7 +220,7 @@

/* Enable FreeModBus */

mberr = eMBEnable();

Check failure on line 223 in examples/modbus/modbus_main.c

View workflow job for this annotation

GitHub Actions / check

Mixed case identifier found
if (mberr != MB_ENOERR)
{
fprintf(stderr, "modbus_main: "
Expand All @@ -238,7 +237,7 @@

/* Release hardware resources. */

eMBClose();

Check failure on line 240 in examples/modbus/modbus_main.c

View workflow job for this annotation

GitHub Actions / check

Mixed case identifier found

errout_with_mutex:

Expand All @@ -260,7 +259,7 @@

static void *modbus_pollthread(void *pvarg)
{
eMBErrorCode mberr;

Check failure on line 262 in examples/modbus/modbus_main.c

View workflow job for this annotation

GitHub Actions / check

Mixed case identifier found
int ret;
#ifdef CONFIG_EXAMPLES_MODBUS_REG_COILS_USERLEDS
int i;
Expand Down Expand Up @@ -314,7 +313,7 @@
{
/* Poll */

mberr = eMBPoll();

Check failure on line 316 in examples/modbus/modbus_main.c

View workflow job for this annotation

GitHub Actions / check

Mixed case identifier found
if (mberr != MB_ENOERR)
{
break;
Expand Down Expand Up @@ -354,11 +353,11 @@

/* Disable */

eMBDisable();

Check failure on line 356 in examples/modbus/modbus_main.c

View workflow job for this annotation

GitHub Actions / check

Mixed case identifier found

/* Release hardware resources. */

eMBClose();

Check failure on line 360 in examples/modbus/modbus_main.c

View workflow job for this annotation

GitHub Actions / check

Mixed case identifier found

/* Free/uninitialize data structures */

Expand All @@ -379,7 +378,7 @@
{
int ret;

if (g_modbus.threadstate == STOPPED)
if (g_modbus.threadstate != RUNNING)
{
ret = pthread_create(&g_modbus.threadid, NULL,
modbus_pollthread, NULL);
Expand Down Expand Up @@ -427,25 +426,26 @@

int main(int argc, FAR char *argv[])
{
bool quit = true;
int option;
int ret;

/* Handle command line arguments */

g_modbus.quit = false;

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 */

quit = false;

ret = modbus_create_pollthread();
if (ret != OK)
{
Expand Down Expand Up @@ -481,7 +481,6 @@
break;

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

Expand All @@ -497,6 +496,13 @@
}
}

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

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

return EXIT_SUCCESS;
}

Expand All @@ -508,7 +514,7 @@
*
****************************************************************************/

eMBErrorCode eMBRegInputCB(uint8_t *buffer, uint16_t address, uint16_t nregs)

Check failure on line 517 in examples/modbus/modbus_main.c

View workflow job for this annotation

GitHub Actions / check

Mixed case identifier found
{
eMBErrorCode mberr = MB_ENOERR;
int index;
Expand Down
Loading