CAN-200: test-write.cpp

CAN-200 1.3.0

test-write.cpp
/******************************************************************************
*
* Пример передачи кадров по шине CAN
*
* Электронная компания "ЭЛКУС", Санкт-Петербург, 2013г.
*****************************************************************************/
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include "../can200-api/can200-api.h"
int main(int argc, char *argv[])
{
int device; /* Номер платы */
int channel; /* Номер канала */
int type; /* Тип выдаваемых кадров */
int speed; /* Скорость */
int count; /* Количество плат в системе */
HANDLE hCan; /* Хэндл платы */
pFilter_t filter; /* Фильтр для режима PeliCAN */
TCAN_VPDData conf; /* Информация о плате */
int result; /* Статус завершения операции */
int i; /* Счетчик цикла */
RX_TX_Buffer SendFrame; /* Выдаваемый кадр */
int status; /* Состояние платы */
int cnt; /* Счетчик принятых кадров */
int num_channel;/* Ко-во каналов на плате */
if (argc < 5)
{
printf("Use receive <number_device> <channel> <speed> <type>\n");
return -1;
}
/* Определяем кол-во плат с системе */
result = CAN200_GetNumberDevice(&count);
if ( CAN200_OK != result)
{
printf("Error CAN200_GetNumberDevice (%d, last error = %u)\n", result, GetLastError());
return -1;
}
/* Номер платы */
device = atoi(argv[1]);
if (device < 0 || device >= count)
{
printf("CAN-200PCI(e) #%d not found\n", device);
return -1;
}
/* Номер канала */
channel = atoi(argv[2]);
/* Скорость */
speed = atoi(argv[3]);
if ( CAN_SPEED_1000 != speed && CAN_SPEED_800 != speed &&
CAN_SPEED_500 != speed && CAN_SPEED_250 != speed &&
CAN_SPEED_125 != speed && CAN_SPEED_50 != speed &&
CAN_SPEED_20 != speed && CAN_SPEED_10 != speed)
{
printf("Unexpected speed #%d (expected %d or %d or %d or %d or %d or %d or %d or %d)\n", speed,
CAN_SPEED_1000, CAN_SPEED_800, CAN_SPEED_500, CAN_SPEED_250,
CAN_SPEED_125, CAN_SPEED_50, CAN_SPEED_20, CAN_SPEED_10);
return -1;
}
/* Тип кадров */
result = strcmp(argv[4], "BasicCAN");
if (0 == result)
{
type = BasicCAN;
}
else
{
result = strcmp(argv[4], "PeliCAN");
if (0 == result)
{
type = PeliCAN;
}
else
{
printf("Unexpected type %s (expected PeliCAN or BaicCAN)\n", argv[3]);
return -1;
}
}
/* Открываем плату */
hCan = CAN200_Open(device);
if (INVALID_HANDLE_VALUE == hCan)
{
printf("Error open CAN-200 device # %d (last error = %u)\n", device, GetLastError());
exit(-1);
}
/* Читаем конфигурацию */
result = CAN200_GetConfig(hCan, &conf);
if (CAN200_OK != result)
{
printf("Error CAN200_GetConfig (%d, last error = %u)\n", result, GetLastError());
CAN200_Close(hCan);
return -1;
}
/* Кол-во каналов */
result = CAN200_GetNumChannel(hCan, &num_channel);
if (CAN200_OK != result)
{
printf("Error CAN200_GetNumChannel (%d, last error = %u)\n", result, GetLastError());
CAN200_Close(hCan);
return -1;
}
if ((channel < 1) || (channel > num_channel))
{
printf("Unexpected channel #%d (expected %d-%d)\n", channel, 1, num_channel);
CAN200_Close(hCan);
return -1;
}
printf("Board: %s\n", conf. szName);
printf("Serial num.: %s\n", conf. szSN);
printf("Channel: %Xh\n", conf. wPorts[channel - 1]);
printf("IRQ: %.2d\n\n", conf. wIRQ);
/* Режим работы канала - PeliCAN */
result = CAN200_SetWorkMode(hCan, channel, PeliCAN);
if (CAN200_OK != result)
{
printf("Error CAN200_SetWorkMode (%d, last error = %u)\n", result, GetLastError());
CAN200_Close(hCan);
return -1;
}
/* Настройка входного фильтра на прием всех кадров */
filter. Mode = 0;
filter. Filter[0] = filter.Filter[1] = filter.Filter[2] = filter.Filter[3] = 0xff;
filter. Mask[0] = filter.Mask[1] = filter.Mask[2] = filter.Mask[3] = 0xff;
result = CAN200_P_SetInputFilter(hCan, channel, &filter);
if (CAN200_OK != result)
{
printf("Error CAN200_P_SetInputFilter (%d, last error = %u)\n", result, GetLastError());
CAN200_Close(hCan);
return -1;
}
/* Настройка скорости */
result = CAN200_SetCANSpeed(hCan, channel, speed);
if (CAN200_OK != result)
{
printf("Error CAN200_SetCANSpeed (%d, last error = %u)\n", result, GetLastError());
CAN200_Close(hCan);
return -1;
}
/* Запрешаем все прерывания */
result = CAN200_SetInterruptSource(hCan, channel, 0x00);
if (CAN200_OK != result)
{
printf("Error CAN200_SetInterruptSource (%d, last error = %u)\n", result, GetLastError());
CAN200_Close(hCan);
return -1;
}
/* Записываем счетчик ошибок */
result = CAN200_P_SetErrorWarningLimit(hCan, channel, 40);
if (CAN200_OK != result)
{
printf("Error CAN200_P_SetErrorWarningLimit (%d, last error = %u)\n", result, GetLastError());
CAN200_Close(hCan);
return -1;
}
/* Настройка выходного формирователя */
result = CAN200_SetDriverMode(hCan, channel, 0x1B);
if (CAN200_OK != result)
{
printf("Error CAN200_SetDriverMode (%d, last error = %u)\n", result, GetLastError());
CAN200_Close(hCan);
return -1;
}
SendFrame. FF = type;
SendFrame. DLC = 0;
SendFrame. RTR = 0;
SendFrame. extID = 0;
SendFrame. sID = 0;
for (i = 0; i < 8; i++)
{
SendFrame. DataByte[i] = 0;
}
cnt = 0;
while (!_kbhit())
{
SendFrame.DLC = (SendFrame.DLC + 1) % 9;
if (PeliCAN == type)
{
SendFrame.extID = (SendFrame.extID + 1) % 0x20000000;
}
else
{
SendFrame.sID = (SendFrame.sID + 1) % 0x800;
}
for (i = 0; i < 8; i++)
{
SendFrame.DataByte[i] = (SendFrame.DataByte[i] + 1) % 0x100;
}
/* Ждем освобождения буфера выдачи выдачи */
while (1)
{
result = CAN200_GetStatus(hCan, channel, &status);
if (CAN200_OK != result)
{
printf("Error CAN200_GetStatus (%d, last error = %u)\n", result, GetLastError());
CAN200_Close(hCan);
return -1;
}
if (0 != (status & 0x04))
{
break;
}
}
/* Запись структуры в буфер передачи канала */
result = CAN200_SetTxBuffer(hCan, channel, &SendFrame);
if (CAN200_OK != result)
{
printf("Error CAN200_SetTxBuffer (%d, last error = %u)\n", result, GetLastError());
CAN200_Close(hCan);
return -1;
}
/* Ждем окончания выдачи */
while (1)
{
result = CAN200_GetStatus(hCan, channel, &status);
if (CAN200_OK != result)
{
printf("Error CAN200_GetStatus (%d, last error = %u)\n", result, GetLastError());
CAN200_Close(hCan);
return -1;
}
if (0 != (status & 0x08))
{
break;
}
}
/* Выдача */
result = CAN200_SetCommand(hCan, channel, 0x01);
if (CAN200_OK != result)
{
printf("Error CAN200_SetCommand (%d, last error = %u)\n", result, GetLastError());
CAN200_Close(hCan);
return -1;
}
cnt++;
printf("Send %d\r", cnt);
}
CAN200_Close(hCan);
return 0;
}

CAN-200 documentation. Copyright (c) 2012 - 2017 Elcus Corporation. All rights reserved.