-
Notifications
You must be signed in to change notification settings - Fork 0
/
tx.c
40 lines (29 loc) · 981 Bytes
/
tx.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
// Code derived from: https://blog.mbedded.ninja/programming/operating-systems/linux/linux-serial-ports-using-c-cpp/
// C library headers
char port_name[] = "/dev/ttySC2";
//char port_name[] = "/dev/ttyAMA0";
//char port_name[] = "/dev/ttyUSB0";
#include "serial.h"
const int packet_size = 256;
int main() {
int serial_port = serial_open(port_name);
// Allocate memory for read buffer, set size according to your needs
unsigned int total_bytes = 0;
int num_bytes = 0;
// Write to serial port
unsigned char msg[1];
for(int i = 0; i < packet_size; i++)
{
if(total_bytes % 0x10 == 0)
printf("\nTotal written 0x%04x:", total_bytes);
msg[0] = i & 0xff;
write(serial_port, &msg, sizeof(msg));
printf("[%02x]", msg[0]);
//nanosleep((const struct timespec[]){{0, 2000000L}}, NULL);
total_bytes++;
}
printf("\n");
printf("\nTotal bytes transmitted: %i.\n", total_bytes);
serial_close(serial_port);
return 0; // success
}