/* * logical level switch I/O thread */ msg_t thdJbus485IO(void *arg) { (void)arg; chRegSetThreadName("thd JBUS 485 IO"); while (initJbus485 () != TRUE) { chThdSleepMilliseconds(1000); if (chThdShouldTerminate()) goto cleanAndExit; } chThdSleepMilliseconds(10); do { eMBPoll (); } while (!chThdShouldTerminate()); cleanAndExit: eMBDisable (); eMBClose (); DebugTrace ("thdJbusIO 485 Thread Is stopping"); return 0; }
static void *modbus_pollthread(void *pvarg) { eMBErrorCode mberr; int ret; /* Initialize the modbus */ ret = modbus_initialize(); if (ret != OK) { fprintf(stderr, "modbus_main: " "ERROR: modbus_initialize failed: %d\n", ret); return NULL; } srand(time(NULL)); /* Then loop until we are commanded to shutdown */ do { /* Poll */ mberr = eMBPoll(); if (mberr != MB_ENOERR) { break; } /* Generate some random input */ g_modbus.reginput[0] = (uint16_t)rand(); } while (g_modbus.threadstate != SHUTDOWN); /* Disable */ (void)eMBDisable(); /* Release hardware resources. */ (void)eMBClose(); /* Free/uninitialize data structures */ (void)pthread_mutex_destroy(&g_modbus.lock); g_modbus.threadstate = STOPPED; return NULL; }
/* ----------------------- Start implementation -----------------------------*/ int main( void ) { _SetupHardware( ); const UCHAR ucSlaveID[] = { 0xAA, 0xBB, 0xCC }; eMBErrorCode eStatus; for( ;; ) { if( MB_ENOERR != ( eStatus = eMBInit( MB_RTU, 0x0A, 1, 38400, MB_PAR_EVEN ) ) ) { /* Can not initialize. Add error handling code here. */ } else { if( MB_ENOERR != ( eStatus = eMBSetSlaveID( 0x34, TRUE, ucSlaveID, 3 ) ) ) { /* Can not set slave id. Check arguments */ } else if( MB_ENOERR != ( eStatus = eMBEnable( ) ) ) { /* Enable failed. */ } else { usRegHoldingBuf[0] = 1; do { ( void )eMBPoll( ); /* Here we simply count the number of poll cycles. */ usRegInputBuf[0]++; } while( usRegHoldingBuf[0] ); ( void )eMBDisable( ); ( void )eMBClose( ); } } } return 1; }
/** * Poll the modbus. * * @param pvParameter */ void * pvPollingThread( void *pvParameter ) { vSetPollingThreadState( RUNNING ); DEBUG_PUTSTRING("Thread started!"); if( eMBEnable( ) == MB_ENOERR ) { do { if( eMBPoll( ) != MB_ENOERR ) break; } while( eGetPollingThreadState( ) != SHUTDOWN ); } ( void )eMBDisable( ); vSetPollingThreadState( STOPPED ); DEBUG_PUTSTRING("Thread stopped!"); cmd_done(BOOT_EXIT); return 0; }
DWORD WINAPI dwPollingThread( LPVOID lpParameter ) { eSetPollingThreadState( RUNNING ); if( eMBEnable( ) == MB_ENOERR ) { do { if( eMBPoll( ) != MB_ENOERR ) break; } while( eGetPollingThreadState( ) != SHUTDOWN ); } ( void )eMBDisable( ); eSetPollingThreadState( STOPPED ); return 0; }
static void vTaskMODBUS( void *pvArg ) { const UCHAR ucSlaveID[] = { 0xAA, 0xBB, 0xCC }; eMBErrorCode eStatus; for( ;; ) { if( MB_ENOERR != ( eStatus = eMBInit( MB_ASCII, 0x0A, 1, 38400, MB_PAR_EVEN ) ) ) { /* Can not initialize. Add error handling code here. */ } else { if( MB_ENOERR != ( eStatus = eMBSetSlaveID( 0x34, TRUE, ucSlaveID, 3 ) ) ) { /* Can not set slave id. Check arguments */ } else if( MB_ENOERR != ( eStatus = eMBEnable( ) ) ) { /* Enable failed. */ } else { usRegHoldingBuf[0] = 1; do { ( void )eMBPoll( ); /* Here we simply count the number of poll cycles. */ usRegInputBuf[0]++; } while( usRegHoldingBuf[0] ); } ( void )eMBDisable( ); ( void )eMBClose( ); } vTaskDelay( 50 ); } }
int main( int argc, char *argv[] ) { int c; int option_index = 0; int received; int mq_id; struct sIPCMsg mymsg; char *ttydev = DEFAULT_TTY; char *parity_s = DEFAULT_PAR; int parity; int baud = DEFAULT_BAUD; USHORT result[255] = {0,}; int result_int = 0; USHORT usLen; eMBErrorCode err = MB_ENOERR; int reg_start = DEFAULT_REGISTER_START; int address = DEFAULT_ADDRESS; int regt = -1; int reg_action = -1; int reg_action_param = -1; int no_reg = -1; int run_forground = 0; int errorcount = 0; struct pidfile *pidfile = NULL; UCHAR *pucFrame; eMBException eException; UCHAR ucFuncType; /* Parse cmd-line options */ while ( (c = getopt_long (argc, argv, "hVd:s:p:r:t:c:a:f", long_options, &option_index)) != EOF ) { switch (c) { case 'h': fprintf(stderr, "%s", helptext); exit(EXIT_SUCCESS); case 'V': exit(EXIT_SUCCESS); case 'd': ttydev = strndup(optarg, MAXLEN); break; case 's': baud = atoi(optarg); break; case 'p': parity_s = strndup(optarg, MAXLEN); break; case 'f': // run in forground run_forground = 1; break; case '?': /* getopt_long already printed an error message. */ default: fprintf(stderr, "%s", helptext); exit(EXIT_SUCCESS); } } if(!run_forground){ fprintf(stderr, "Deamonizing...\n"); daemon(0, 0); } /* Setup signalhandler so that we shut down nicely on CTRL+C */ signal(SIGINT, int_handler); pidfile = pidfile_create(DEFAULT_PIDFILE, PMODE_RETURN, 0); mq_id = create_ipc(); /* Allocate space for an ADU frame */ pucFrame = eMBAllocateFrame(&usLen); while(outer_loop) { if(running == 1) printf("Restarting modbus layer\n"); else { printf("\n\n\n\n Restarting modbus layer\n\n\n\n"); running = 1; errorcount = 0; } /* Parse user inputs... */ if(strcmp(parity_s, "even") == 0) parity = MB_PAR_EVEN; else if(strcmp(parity_s, "odd") == 0) parity = MB_PAR_ODD; else parity = MB_PAR_NONE; PRINT_DBG(1, "init modbus at tty: %s parity %d baud %d", ttydev, parity, baud); /* mode, port, baud, parity */ eMBMasterInit(MB_RTU, ttydev, baud, parity); while(running) { /* Check message queue */ if((received = msgrcv(mq_id, &mymsg, sizeof(mymsg.text), 1, IPC_NOWAIT)) > 0) { /* Address */ address = parse_address(mymsg.text); /* Starting register */ reg_start = parse_reg_start(mymsg.text); /* Register type */ regt = parse_reg_type(mymsg.text); /* Register action */ reg_action_param = 0; reg_action = parse_reg_action(mymsg.text, ®_action_param); /* Optional: number of registers */ no_reg = parse_no_reg(mymsg.text); #ifdef DBG if(dbglev>=1){ printf("Got: %s\n", mymsg.text); printf("Parsing command:\n"); printf("Address: 0x%02x\n", address); printf("Register: %d\n", reg_start); printf("Register type: %d\n", regt); printf("Register action: %d Arg: %d\n", reg_action, reg_action_param); printf("Register count: %d\n", no_reg); } #endif switch(regt) { case MB_TYPE_COILS: switch(reg_action) { case ACTION_READ: err = build_eMBMasterReadCoils (pucFrame, reg_start, reg_action_param, &usLen); break; case ACTION_WRITE: err = build_eMBMasterWriteCoils (pucFrame, reg_start, reg_action_param ? 0xff : 0x0, &usLen); break; case ACTION_WRITEMULTIPLE: err = build_eMBMasterWriteMultipleCoils (pucFrame, reg_start, no_reg, reg_action_param ? 0xff : 0x0, &usLen); break; } break; case MB_TYPE_INPUT: err = build_eMBMasterReadInput (pucFrame, reg_start, reg_action_param, &usLen); break; case MB_TYPE_HOLDING: switch(reg_action) { case ACTION_READ: err = build_eMBMasterReadHolding (pucFrame, reg_start, reg_action_param, &usLen); break; case ACTION_WRITE: err = build_eMBMasterWriteSingleHolding (pucFrame, reg_start, reg_action_param, &usLen); break; } break; } #ifdef DBG if(dbglev){ int n; printf("Dumping built frame:\n"); for(n=0; n<10; n++) printf("%02x ",pucFrame[n]); printf("\n"); } #endif /* ? */ eMBEnable(); memset(result, 0, sizeof(result)); if(err == MB_EX_NONE) { /* Set the address of the device in question */ eMBSetSlaveAddress(address); /* Send the frame */ if( (err = eMBSendFrame(pucFrame, usLen)) == MB_ENOERR) { printf(" ---- done processing frame ----\n"); eException = MB_EX_ILLEGAL_FUNCTION; ucFuncType = pucFrame[MB_PDU_FUNC_OFF]; #ifdef DBG if(dbglev){ printf("%s():%d - after send...\n", __FUNCTION__, __LINE__); { int n; printf("Dumping received frame:\n"); for(n=0; n<10; n++) printf("%02x ",pucFrame[n]); printf("\n"); } } #endif if(ucFuncType & 0x80) { UCHAR ucExceptType = pucFrame[MB_PDU_FUNC_OFF+1]; /* We encountered some sort of error */ switch(ucExceptType) { case MB_EX_ILLEGAL_FUNCTION: fprintf(stderr, "Error! Illegal function!\n"); break; case MB_EX_ILLEGAL_DATA_ADDRESS: fprintf(stderr, "Error! Illegal data address!\n"); break; case MB_EX_ILLEGAL_DATA_VALUE: fprintf(stderr, "Error! Illegal data value!\n"); break; case MB_EX_SLAVE_DEVICE_FAILURE: fprintf(stderr, "Error! Slave device failure!\n"); break; default: fprintf(stderr, "Unknown error (%d)!\n", eException); break; } } else { switch(ucFuncType) { case MB_FUNC_READ_COILS: eException = parse_eMBMasterReadCoils(pucFrame, &usLen, (void *)result); #ifdef DBG if(dbglev) printf("Reply: %04x\n", result[0]); #endif break; case MB_FUNC_WRITE_SINGLE_COIL: eException = parse_eMBMasterWriteCoils(pucFrame, &usLen, (void *)result); #ifdef DBG if(dbglev) printf("Reply: %04x\n", result[0]); #endif break; case MB_FUNC_READ_INPUT_REGISTER: eException = parse_eMBMasterReadInput(pucFrame, &usLen, (void *)result); #ifdef DBG if(dbglev) printf("Reply (%d): %04x\n", eException, result[0]); #endif break; case MB_FUNC_READ_HOLDING_REGISTER: eException = parse_eMBMasterReadHolding(pucFrame, &usLen, (void *)result); #ifdef DBG printf("Reply: %04x %04x %d %d\n", result[0],result[1], result_int, reg_action_param); #endif break; case MB_FUNC_WRITE_REGISTER: eException = parse_eMBMasterWriteSingleHolding(pucFrame, &usLen, (void *)result); #ifdef DBG printf("Reply: %04x\n", result[0]); #endif break; case MB_FUNC_WRITE_MULTIPLE_COILS: usLen = 5; eException = parse_eMBMasterWriteMultipleCoils(pucFrame, &usLen, (void *)result); if(eException == MB_EX_NONE) printf("Write multiple coils SUCCESS!\n"); else printf("Write multiple coils FAIL!\n"); break; default: printf("Unknown function type : %x\n", ucFuncType); printf("%d : %s()\n", __LINE__, __FUNCTION__); { int n; for(n=0; n<10; n++) printf("%02x ",pucFrame[n]); printf("\n"); } break; } switch(ucFuncType) { case MB_FUNC_READ_HOLDING_REGISTER: case MB_FUNC_READ_INPUT_REGISTER: if(reg_action_param == 2){ printf("!!!!\n"); result_int = (result[0]<<16) | result[1]; }else { result_int = result[0]; } break; default: result_int = result[0]; break; } if(eException == MB_EX_NONE) { snprintf(mymsg.text, sizeof(mymsg.text), "%ld:REG:%d:RESULT:%d", time(NULL), reg_start, result_int); } else { snprintf(mymsg.text, sizeof(mymsg.text), "ERROR:Parsing frame"); } #ifdef DBG printf("%s():%d - reply: %s\n", __FUNCTION__, __LINE__, mymsg.text); #endif goto out; } } else { printf("Error sending frame. (%d)\n", err); snprintf(mymsg.text, sizeof(mymsg.text), "ERROR:Sending frame"); if(errorcount > 10) running = 0; else errorcount++; } } else { printf("Error in frame arguments.\n"); snprintf(mymsg.text, sizeof(mymsg.text), "ERROR:Wrong argument"); } out: /* Set the reply */ mymsg.type = 2; #ifdef DBG printf("%s():%d - reply: %s\n", __FUNCTION__, __LINE__, mymsg.text); #endif msgsnd(mq_id, &mymsg, sizeof(mymsg.text), IPC_NOWAIT); eMBDisable(); } else { //printf("No messages. Sleeping (%d - %s)\n", received, received < 0 ? strerror(errno) : ""); } usleep(1000); } // free(pucFrame); eMBClose(); } destroy_ipc(PROJ_ID); pidfile_delete(pidfile); return 0; }