| ioPAC RTU Controllers C/C++ Sample Code Programming Guide

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

Functions
can.c File Reference

Can More...

`#include <libmoxa_rtu.h>`

## Functions
int | main (int argc, char *argv[])

## Detailed Description

Can

Copyright
  Moxa Inc.

Date
  02-04-2016

Author
  TJ Tai

Version
  V1.0
  CAN

    Introduction:
    This is an can example. The controller will use canopen protocol to connect with can slave.
    Example:
    1. Using default: ./can
    2. Setting slot, port and baud rate only: ./can -s 1 -p 1 -b 125000 -i 1
    Default:
    Slot = 1
    Port = 1
    Baud Rate = 125000
    SlaveID = 1
    Help:

    root@Moxa:/tmp#./can -h
    Can.

    Usage: ./can [OPTIONS]

    Options:
        -s      slot [0-9]. Default slot = 0
        -p      port [0-3]. Default port = 0
        -b      Baud Rate [10k-1000k]. Default Baud Rate = 125000
        -i      SlaveID [0-99]. Default SlaveID = 1

Library:
Can APIs

## Function Documentation

int main | ( | int | argc,
|  | char * | argv[]
| ) |  |

/*******************************************************************************
* Copyright Moxa Inc.
*
* Can
*
* Date Author Comment
* 02-04-2016 TJ Tai Created.
******************************************************************************/
#include <libmoxa_rtu.h>
int main(int argc, char *argv[])
{
CAN_ERR_CODE rc = 0;
int ch = 0;
int handle = 0;
UINT8 slot;
UINT8 port;
UINT32 baudrate;
UINT32 slaveID = 1;
UINT16 guardTime = 3000;
UINT8 lifeTimeFactor = 3;
UINT16 producerTime = 3000;
UINT16 consumerTime = 6000;
CAN_NMT_ERROR_STATUS NMTStatus;
CAN_NMT_NODE_GUARDING_STATE GUARDINGStatus;
UINT32 SyncCOBID = 128;
UINT32 CANstatus;
UINT16 timer = 3000;
UINT32 syncTimes = 0;
UINT32 pdoTimes = 5;
UINT8 wData[CAN_MAX_PAYLOAD_LEN];
UINT8 rData[CAN_MAX_PAYLOAD_LEN];
UINT32 wLen = 0;
UINT32 rLen = 0;
UINT32 abortCode = 0;
UINT8 pdoNumber = 1;
while(-1 != (rc = getopt(argc, argv, "hs:p:b:i:")))
{
switch(rc)
{
case 's':
slot = atoi(optarg);
if (slot < 1 || slot > 9) {
printf("error slot\n");
return 0;
}
break;
case 'p':
port = atoi(optarg);
if (port!=0 && port!= 1) {
printf("error port\n");
return 0;
}
break;
case 'b':
baudrate = atoi(optarg);
break;
case 'i':
slaveID = atoi(optarg);
break;
case 'h':
case '?':
default:
printf("%s.\n\n", "CANopen");
printf("Usage: ./can [OPTIONS]\n\n");
printf("Options:\n");
printf("\t%-8s Slot.\n", "-s");
printf("\t%-8s Port.\n", "-p");
printf("\t%-8s Baudrate.\n", "-b");
printf("\t%-8s Slave ID.\n", "-i");
printf("\t%-8s Help\n", "-h");
printf("\n");
return 0;
}
}
printf("Setting: slot = %d, port = %d, baudrate = %d, slaveID = %d\n", slot, port, baudrate, slaveID);
rc = MX_RTU_CanGetStatus(slot, port, &CANstatus);
rc = MX_RTU_CanSetBaudrate(slot, port, baudrate);
rc = MX_RTU_CanGetBaudrate(slot, port, &baudrate);
printf("Get baudrate = %d\n", baudrate);
printf("CANstatus = %d(0x%x)\n", CANstatus, CANstatus);
rc = MX_RTU_CanOpen(slot, port, &handle);
if (rc != CAN_ERR_OK) {
printf("MX_RTU_CanOpen handle = %d, rc = %d\r\n", handle, rc);
}
printf("CANOpen handle = %d\n", handle);
printf("Set NMT Start.\n");
//Set NMT Start
rc = MX_RTU_CanNMTSetState(handle, slaveID, CAN_NODE_START);
if(rc != 0)
printf("CAN NMT Start Fail!\n");
else {
printf("MX_RTU_CanNMTSetState: handle = %d, rc = %d\r\n", handle, rc);
}
sleep(5);
rc = MX_RTU_CanNMTHeartbeat(handle, slaveID, producerTime, consumerTime);
printf("MX_RTU_CanNMTHeartbeat: handle = %d, rc = %d\r\n", handle, rc);
//Send SYNC
rc = MX_RTU_CanCyclicSYNCSend(handle, 0x80, timer, syncTimes);
if(rc != 0)
{
printf("MX_RTU_CanCyclicSYNCSend error: handle = %d, rc = %d\r\n", handle, rc);
}
sleep(5);
//Set Heartbeat
producerTime = 5000;
consumerTime = 20000;
rc = MX_RTU_CanNMTHeartbeat(handle, slaveID, producerTime, consumerTime);
if(rc != 0)
printf("Set Heartbeat P/C = %d/%d Fail!\n", producerTime, consumerTime);
memset(wData, 0, sizeof(wData));
wData[0] = 0xF0;
wLen = 1;
rc = MX_RTU_CanCyclicPDOSend(handle, pdoNumber, slaveID, wData, wLen, timer, pdoTimes);
printf("MX_RTU_CanCyclicPDOSend: handle = %d, rc = %d\r\n", handle, rc);
if(rc != 0)
printf("MX_RTU_CanCyclicPDOSend error: handle = %d, rc = %d\r\n", handle, rc);
rLen = 0;
abortCode = 0;
memset(rData, 0, sizeof(rData));
rc = MX_RTU_CanSDORead(handle, slaveID, 0x6200, 1, rData, &rLen, &abortCode);
printf("MX_RTU_CanSDORead: handle = %d, rc = %d, rLen = %d, abortCode = 0x%x\r\n", handle, rc, rLen, abortCode);
if(rc != 0)
printf("MX_RTU_CanSDORead error: handle = %d, rc = %d\r\n", handle, rc);
//printf("rData[0] = 0x%x\n", rData[0]);
while(1)
{
rc = MX_RTU_CanGetNMTError(handle, slaveID, &NMTStatus, &GUARDINGStatus);
printf("\tNMTStatus = %d(0x%x)\n", NMTStatus, NMTStatus);
printf("\tGUARDINGStatus = %d(0x%x)\n", GUARDINGStatus, GUARDINGStatus);
usleep(1000000);
}
//Set NMT Stop
rc = MX_RTU_CanNMTSetState(handle, slaveID, CAN_NODE_STOP);
if(rc != 0)
printf("Can NMT stop Fail!\n");
rc = MX_RTU_CanClose(handle);
printf("Can CLOSE! \n");
return 0;
}