The following example shows how to start the CANopen FD Master library under Linux. The program runs until CTRL-C
is pressed. After transmission of the boot-up message (i.e. identifier 77Fh in this example) the application will check for another instance of a CANopen Master on the same bus be calling ComNmtMasterDetection(). A hearbeat with a cycle time of 2 seconds is configured by calling ComNmtSetHbProdTime().
int main(int argc, char *argv[])
{
uint32_t ulTickOneSecondT = 0;
ubProgRunS = 1;
printf("\n\n");
printf("###############################################################################\n");
printf("# uMIC.200 CANopen Master Example #\n");
printf("###############################################################################\n");
printf("\n");
printf("Use CTRL-C to quit this demo.\n");
printf("\n");
init_signal_handler();
while(ubProgRunS)
{
while(ulTickOneSecondT == ulCounterOneSecondS)
{
sleep(10);
}
ulTickOneSecondT = ulCounterOneSecondS;
}
printf("\n");
printf("Quit CANopen Master demo.\n");
return(0);
}
Signal handler for timing
After initialisation of the CANopen FD Master library, the function ComMgrTimerEvent() must be called periodically. The timer period can be configured to the application via ComTmrSetPeriod().
Under Linux, we use a simple alarm handler for that purpose. The alarm handler is initialised inside the function init_signal_handler()
.
void init_signal_handler(void)
{
struct sigaction tsSigActionT;
struct itimerval tsTimerValT;
tsSigActionT.sa_handler = sig_handler_quit;
sigemptyset(&tsSigActionT.sa_mask);
tsSigActionT.sa_flags = 0;
sigaction(SIGINT, &tsSigActionT, 0);
tsSigActionT.sa_handler = sig_handler_time;
sigemptyset(&tsSigActionT.sa_mask);
tsSigActionT.sa_flags = 0;
sigaction(SIGALRM, &tsSigActionT, 0);
tsTimerValT.it_value.tv_sec = 1;
tsTimerValT.it_value.tv_usec = 0;
tsTimerValT.it_interval.tv_sec = 0;
tsTimerValT.it_interval.tv_usec = 10000;
setitimer(ITIMER_REAL, &tsTimerValT, NULL);
}
The signal SIGALRM
will call the function sig_handler_time()
within a 10 ms period.
void sig_handler_time(int slSignalV)
{
static uint32_t ulCounterTenMillisecondsS = 0;
if(slSignalV == SIGALRM)
{
ulCounterTenMillisecondsS++;
if(ulCounterTenMillisecondsS == 100)
{
ulCounterOneSecondS++;
ulCounterTenMillisecondsS = 0;
}
}
}