/
usbSerial_benchmark.c
111 lines (93 loc) · 2.33 KB
/
usbSerial_benchmark.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*--------------------------------------------------------------------
/ This program reads a serial stream from a Virtual Com Port device.
/ It calculates the transfer speed in kbytes/sec. Also, it assumes
/ that the data stream consists of incremental values and checks the
/ integrity of that data stream by comparison.
/--------------------------------------------------------------------
/ This program is based on the PJRC's serial_listen benchmark app.
/--------------------------------------------------------------------
/ <ihsan@kehribar.me>
/-------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#define TEST_SIZE 2500*100
int main(int argc, char **argv)
{
int q;
long n;
int port;
int i = 0;
double diff;
long elapsed;
long sum = 0;
int index = 0;
int errCount = 0;
unsigned char checkVal;
unsigned char buf[8192];
unsigned char bigBuffer[3200*1000];
struct timeval t1;
struct timeval t0;
struct termios settings;
if (argc < 2)
{
fprintf(stderr, "Usage: serial_listen <port>\n");
return 1;
}
port = open(argv[1], O_RDONLY);
if (port < 0)
{
fprintf(stderr, "Unable to open %s\n", argv[1]);
return 1;
}
tcgetattr(port, &settings);
cfmakeraw(&settings);
tcsetattr(port, TCSANOW, &settings);
FILE* fd = fopen("log.txt","w+");
printf("Reading from %s\n", argv[1]);
gettimeofday(&t0,0);
while (1)
{
n = read(port, buf, sizeof(buf));
if (n < 1)
{
fprintf(stderr, "error reading from %s\n", argv[1]);
break;
}
sum += n;
i = 0;
while(n)
{
bigBuffer[index++] = buf[i++];
n--;
}
if(sum > TEST_SIZE)
{
break;
}
}
gettimeofday(&t1,0);
checkVal = bigBuffer[0];
printf("First element: %d\r\n",checkVal);
for(q=1;q<sum;q++)
{
checkVal++;
if(bigBuffer[q] != checkVal)
{
errCount++;
fprintf(fd,"Err e:%d r:%d\r\n",checkVal,bigBuffer[q]);
}
}
printf("Err count :%d\r\n",errCount);
printf("Err percentage: %%%f\r\n",(double)errCount/(double)sum*100.0);
elapsed = (t1.tv_sec-t0.tv_sec)*1000000 + t1.tv_usec-t0.tv_usec;
diff = (double)elapsed / 1000000.0;
printf("Total bytes read: %ld\n", sum);
printf("Speed %.2f kbytes/sec\n", (float)sum / diff / 1000);
return 0;
}