#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <termios.h>
#include <getopt.h>
#define EOK 0
int main(int argc, char *argv[])
{
int device = -1;
int speed = -1;
int type = -1;
int hCan;
int result;
int i;
char name[256];
fd_set wfd;
struct timeval tv;
int status;
int len;
while (-1 != (result = getopt(argc, argv, "d:s:t:")))
{
switch (result)
{
case 'd':
{
device = atoi(optarg);
break;
}
case 's':
{
speed = atoi(optarg);
}
case 't':
{
if (0 == strcmp(optarg, "BasicCAN"))
{
}
if (0 == strcmp(optarg, "PeliCAN"))
{
}
}
default:
{
break;
}
}
}
if ((device < 0) || (speed < 0) || (type < 0))
{
printf("Use test_write -d <number_device> -s <speed (10, 20, 50, 125, 250, 500, 8000, 1000)> -t <type (BasicCAN, PeliCAN)>\n");
return EXIT_FAILURE;
}
{
printf("Unexpected speed #%d (expected %d or %d or %d or %d or %d or %d or %d or %d)\n", speed,
return -1;
}
sprintf(name, "/dev/can200_%d", device);
hCan = open(name, O_RDWR | O_NONBLOCK);
if (-1 == hCan)
{
printf("Error open /dev/can200_%d (%s)\n", device, strerror(errno));
return EXIT_FAILURE;
}
if (EOK != result)
{
printf("Error GetConfig (%d, %s).\n", result, strerror(errno));
close(hCan);
return EXIT_FAILURE;
}
printf(
"Board: %s\n", conf.
szName);
printf(
"Serial num.: %s\n", conf.
szSN);
printf(
"Base port: %Xh\n", conf.
wPorts);
printf(
"IRQ: %.2d\n\n", conf.
wIRQ);
if (EOK != result)
{
printf("Error CAN200_SetWorkMode (%d, %s).\n", result, strerror(errno));
close(hCan);
return EXIT_FAILURE;
}
if (EOK != result)
{
printf("Error SetCANSpeed (%d, %s).\n", result, strerror(errno));
close(hCan);
return EXIT_FAILURE;
}
if (EOK != result)
{
printf("Error SetDriverMode (%d, %s).\n", result, strerror(errno));
close(hCan);
return EXIT_FAILURE;
}
frame.
type = (
unsigned char)type;
for (i = 0; i < 8; i++)
{
}
while (1)
{
FD_ZERO(&wfd);
FD_SET(hCan, &wfd);
tv.tv_sec = 0;
tv.tv_usec = 100000;
result = select(1 + hCan, NULL, &wfd, NULL, &tv);
if (-1 == result)
{
printf("Error select (%s)\n", strerror(errno));
close(hCan);
return EXIT_FAILURE;
}
if (0 == result)
{
continue;
}
if (FD_ISSET(hCan, &wfd))
{
if (EOK != result)
{
printf("Error SetDriverMode (%d, %s).\n", result, strerror(errno));
close(hCan);
return EXIT_FAILURE;
}
{
printf("Error transmit, status = %d\n", status);
close(hCan);
return EXIT_FAILURE;
}
if (-1 == write(hCan, &frame,
sizeof(
CAN_data)))
{
printf("Error write (%s)\n", strerror(errno));
close(hCan);
return EXIT_FAILURE;
}
frame.
dlc = (
unsigned char)((frame.
dlc + 1) % 9);
{
frame.
id = (frame.
id + 1) % 0x20000000;
}
else
{
frame.
id = (frame.
id + 1) % 0x800;
}
for (i = 0; i < 8; i++)
{
frame.
data[i] = (
unsigned char)((frame.
data[i] + 1) % 0x100);
}
sleep(1);
}
}
result = close(hCan);
if (-1 == result)
{
printf("Error close (%s)\n", strerror(errno));
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}