#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
#include <windows.h>
#include <conio.h>
#include <time.h>
#ifdef __BORLANDC__
#pragma argsused
#endif
void Usage(int argc, char* argv[])
{
printf("\nCANLIB Echo Server\n");
printf("(Part of the CANLIB SDK from KVASER AB - http://www.kvaser.se)\n");
printf("\n");
printf("This is a sample program which acts as an \"Echo Server\".\n");
printf("If you send a message to this little program, it will respond with\n");
printf("the same message where the identifier is increased by one.\n");
printf("\n");
printf("It requires a CAN interface supported by CANLIB, and runs\n");
printf("under most versions of Windows.\n");
printf("\nUsage: tx [flags]\n");
printf(" -X Use channel number X as source. (Default 0.)\n");
printf(" -B<value> Set the bitrate. Value is any of 1000,500,250,125.\n");
printf(" Default bitrate is 125 kbit/s.\n");
printf(" -Sbbb,t1,t2 Set the bitrate using specified values for tseg1 and tseg2.\n");
printf(" bbb=bitrate in bps.\n");
printf(" t1=# of quanta before the sampling point, not including\n");
printf(" the sync segment.\n");
printf(" t2=# of quanta after the sampling point.\n");
printf(" -q Be quiet.\n");
exit(1);
}
int Tseg1, Tseg2;
int Channel = 0;
int Quiet = 0;
{
if (stat != canOK) {
char buf[50];
buf[0] = '\0';
printf("%s: failed, stat=%d (%s)\n", id, (int)stat, buf);
}
}
int InitCtrl(int ctrl)
{
printf("canOpenChannel, channel %d... ", ctrl);
if (hnd < 0) {
exit(1);
}
printf("OK.\n");
printf("Setting the bus speed...");
if (stat < 0) {
printf("canSetBusParams failed, stat=%d\n", stat);
}
printf("OK.\n");
printf("Go bus-on...");
if (stat < 0) {
printf("canBusOn failed, stat=%d\n", stat);
}
printf("OK.\n");
return hnd;
}
void IncreaseId(long *id, int flags)
{
unsigned long i = *(unsigned long*)id;
i++;
if (flags & canMSG_STD) {
if (i > 2031) i = 0;
}
else if (flags & canMSG_EXT) {
if ((i & 0x03F0) == 0x03F0) {
i = (i & 0xFFFFFC00) + 0x800 + (i & 0x0F);
}
i &= 0x1FFFFFFF;
}
*id = i;
}
void EchoLoop(void)
{
long id;
BYTE msg[8];
int Ready;
unsigned int i, dlc, flags;
unsigned long time;
hnd = InitCtrl(Channel);
Ready = FALSE;
do {
do {
stat =
canReadWait(hnd, &
id, msg, &dlc, &flags, &time, 250);
if (stat != canOK) break;
IncreaseId(&id, flags);
flags & (canMSG_STD | canMSG_EXT | canMSG_RTR));
Check("canWrite", stat);
if (!Quiet) {
printf("%8ld%c%c%c %u ",
id,
(flags & canMSG_EXT) ? 'x' : ' ',
(flags & canMSG_RTR) ? 'R' : ' ',
(flags & canMSGERR_OVERRUN) ? 'o' : ' ',
dlc);
if ((flags & canMSG_RTR) == 0) {
for (i = 0; i < dlc; i++) printf("%3u ", msg[i]);
for (; i < 8; i++) printf(" ");
}
printf("%08lu\n", time);
}
} while (stat == canOK);
if (_kbhit()) {
int c = _getch();
switch (c) {
case 27:
Ready = TRUE;
default:
;
}
}
} while (!Ready);
}
void main(int argc, char* argv[])
{
int i;
for (i = 1; i < argc; i++) {
int tmp;
char c;
int tmp1, tmp2;
if (sscanf(argv[i], "-%d%c", &tmp, &c) == 1) {
Channel = tmp;
}
else if (sscanf(argv[i], "-B%d%c", &tmp, &c) == 1) {
switch (tmp) {
default: Usage(argc, argv);
}
}
else if (sscanf(argv[i], "-S%d,%d,%d%c", &tmp, &tmp1, &tmp2, &c) == 3) {
Bitrate = tmp;
Tseg1 = tmp1;
Tseg2 = tmp2;
}
else if (strcmp(argv[i], "-q") == 0) {
Quiet++;
}
else {
Usage(argc, argv);
}
}
if (!Quiet) printf("Starting...\n");
EchoLoop();
printf("\nThat's all for today!\n");
}