#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
#include <windows.h>
#include <conio.h>
#ifdef __BORLANDC__
#pragma argsused
#endif
void Usage(int argc, char* argv[])
{
printf("\nCANLIB Thread Test 4\n");
printf("\n");
printf("\nUsage: thread [flags]\n");
printf(" -aX Use channel number X as source\n");
printf(" -bY Use channel number Y as destination\n");
printf(" (note: X must be different from Y. Default X=0, Y=1)\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(" -v Be verbose.\n");
printf(" -Lnnn Run nnn \"loops\". Default is 500.\n");
exit(1);
}
int Verbose = 0;
int MaxLoops = 500;
{
char buf[50];
buf[0] = '\0';
printf("%s: failed, stat=%d (%s)\n", id, (int)stat, buf);
}
}
{
if (hnd < 0) {
printf("canOpenChannel failed, stat=%d, ctrl=%d\n", hnd, ctrl);
exit(0);
}
if (stat < 0) {
printf("canSetBusParams failed, stat=%d\n", stat);
}
if (stat < 0) {
printf("canBusOn failed, stat=%d\n", stat);
}
return hnd;
}
CRITICAL_SECTION cs;
static long TimeToLeave;
static long TotalSent[4];
static long TotalReceived[4];
#define NR_MSGS 3
#define SRC_ID 300
#define P_NAP 50
#define C_NAP 0
HANDLE events[4];
int channels[4];
DWORD WINAPI SendAndReceive(PVOID p)
{
long id_read;
BYTE msg[8];
int i, count;
ULONG idx;
idx = PtrToUlong(p);
hnd = InitCtrl(channels[idx]);
TotalSent[idx] = 0;
count = 0;
WaitForSingleObject(events[idx], INFINITE);
while (TRUE) {
long id;
id = SRC_ID + idx;
for (i=0; i<NR_MSGS; i++) {
unsigned int dlc, flags;
memcpy(msg, &count, sizeof(count));
count++;
Check("canWrite A", stat);
TotalSent[idx]++;
do {
stat =
canRead(hnd, &id_read, NULL, &dlc, &flags, NULL);
if (Verbose > 1) printf("R");
TotalReceived[idx]++;
}
}
Check("canWriteSync A", stat);
Sleep(P_NAP);
if (TimeToLeave) break;
}
return 0;
}
DWORD WINAPI ReadOne(PVOID p)
{
ULONG idx;
idx = PtrToUlong(p);
hnd = InitCtrl(channels[idx]);
TotalReceived[idx] = 0;
WaitForSingleObject(events[idx], INFINITE);
while (TRUE) {
do {
if (Verbose > 1) printf("r");
} else {
if (Verbose > 1) printf("T");
}
while (
canRead(hnd, NULL, NULL, NULL, NULL, NULL) ==
canOK) {
TotalReceived[idx]++;
}
do {
printf("Whoops, canReadSyncSpecific failed");
}
if (TimeToLeave) break;
Sleep(C_NAP);
}
return 0;
}
DWORD WINAPI Sink(PVOID p)
{
ULONG idx;
idx = PtrToUlong(p);
hnd = InitCtrl(channels[idx]);
TotalReceived[idx] = 0;
WaitForSingleObject(events[idx], INFINITE);
while (TRUE) {
while (
canRead(hnd, NULL, NULL, NULL, NULL, NULL) ==
canOK) {
if (Verbose > 1) printf("x");
TotalReceived[idx]++;
}
if (TimeToLeave) break;
Sleep(C_NAP);
}
return 0;
}
void main(int argc, char* argv[])
{
int i;
DWORD tid;
HANDLE t1, t2, t3, t4;
for (i=0; i<4; i++) channels[i] = i;
for (i=1; i<argc; i++) {
int tmp;
char c;
if (sscanf(argv[i], "-a%d%c", &tmp, &c) == 1) {
channels[0] = tmp;
} else if (sscanf(argv[i], "-b%d%c", &tmp, &c) == 1) {
channels[1] = tmp;
} else if (sscanf(argv[i], "-c%d%c", &tmp, &c) == 1) {
channels[2] = tmp;
} else if (sscanf(argv[i], "-d%d%c", &tmp, &c) == 1) {
channels[3] = tmp;
} else if (sscanf(argv[i], "-B%d%c", &tmp, &c) == 1) {
switch (tmp) {
default : Usage(argc, argv);
}
} else if (strcmp(argv[i], "-v") == 0) {
Verbose++;
} else if (sscanf(argv[i], "-L%d%c", &tmp, &c) == 1) {
MaxLoops = tmp;
} else {
Usage(argc, argv);
}
}
printf("\nCANLIB Thread Test 4\n");
memset(TotalSent, 0, sizeof(TotalSent));
memset(TotalReceived, 0, sizeof(TotalReceived));
InitializeCriticalSection(&cs);
TimeToLeave = FALSE;
t1 = t2 = t3 = t4 = NULL;
if (channels[0] != 99) t1 = CreateThread(NULL, 0, SendAndReceive, (LPVOID)0, CREATE_SUSPENDED, &tid);
if (channels[1] != 99) t2 = CreateThread(NULL, 0, SendAndReceive, (LPVOID)1, CREATE_SUSPENDED, &tid);
if (channels[2] != 99) t3 = CreateThread(NULL, 0, ReadOne, (LPVOID)2, CREATE_SUSPENDED, &tid);
if (channels[3] != 99) t4 = CreateThread(NULL, 0, Sink, (LPVOID)3, CREATE_SUSPENDED, &tid);
printf("\n");
if (t1) printf("SendAndReceive on channel %d\n", channels[0]);
if (t2) printf("SendAndReceive on channel %d\n", channels[1]);
if (t3) printf("ReadOne on channel %d\n", channels[2]);
if (t4) printf("Sink on channel %d\n", channels[3]);
printf("\n");
for (i=0; i<4; i++) events[i] = CreateEvent(0, FALSE, FALSE, 0);
ResumeThread(t1);
ResumeThread(t2);
ResumeThread(t3);
ResumeThread(t4);
Sleep(100);
for (i=0; i<4; i++) SetEvent(events[i]);
while (TRUE) {
Sleep(200);
if (_kbhit()) {
_getch();
break;
}
if (--MaxLoops == 0) break;
}
TimeToLeave = TRUE;
WaitForSingleObject(t1, 2000);
WaitForSingleObject(t2, 2000);
WaitForSingleObject(t3, 2000);
WaitForSingleObject(t4, 2000);
printf("\nChannel Sent Received\n");
for (i=0; i<4; i++) {
printf("%7d%10ld%13ld\n", channels[i], TotalSent[i], TotalReceived[i]);
}
}