Project

General

Profile

Revision 1116

Added by Emily Hart about 15 years ago

Added server code.

View differences:

branches/diagnostics/computer-hub/comp-hub.h
10 10
static void* listen();
11 11
static int read_char(char* buf);
12 12
static int put_string(char *str);
13
static int put_char(char* c);
13
static int put_char(char c);
branches/diagnostics/computer-hub/comp-hub.c
1 1
#include "comp-hub.h"
2 2

  
3
// THESE ARE NOT SET CORRECTLY!
4 3
// ALSO DON'T FORGET TO CHECK OUT
5 4
// ATTRIBUTES ON LINES 39 ish TO 50 ish
6 5
#define PORT_DEFAULT "/dev/ttyUSB0"
7
#define INPUT_BAUD_RATE B9600
8
#define OUTPUT_BAUD_RATE B9600
6
#define BAUD_RATE 115200
9 7

  
10 8
static char* com_port = PORT_DEFAULT;
11 9
static int stream;
......
16 14
		printf("Exiting...\n");
17 15
	
18 16
	//your code here
17
	listen();
18
	put_char('r');
19
	listen();
20
	put_char('b');
21
	listen();
22
	put_char('m');
23
	listen();
19 24
	
20 25
	return 0;
21 26
}
......
36 41
	struct termios options;
37 42
	if(tcgetattr(stream, &options))
38 43
		printf("Error getting attributes");
39
	cfsetispeed(&options, INPUT_BAUD_RATE);
40
	cfsetospeed(&options, OUTPUT_BAUD_RATE);
41
	options.c_iflag &= ~ICRNL;
42
	options.c_oflag &= ~OCRNL;
43
	options.c_cflag |= (CLOCAL | CREAD);
44
	options.c_cflag &= ~PARENB;
45
	options.c_cflag &= ~CSTOPB;
44
	cfsetispeed(&options, BAUD_RATE);
45
	cfsetospeed(&options, BAUD_RATE);
46
	//options.c_iflag &= ~ICRNL;
47
	//options.c_oflag &= ~OCRNL;
48
	//options.c_cflag |= (CLOCAL | CREAD);
49
	options.c_cflag &= ~PARENB; //no parity
50
	options.c_cflag &= ~CSTOPB; //one stop bit
46 51
	options.c_cflag &= ~CSIZE;
47
	options.c_cflag |= CS8;
48
	options.c_lflag &= ~ICANON;
49
	options.c_cc[VMIN] = 1;
50
	options.c_cc[VTIME] = 50;
52
	options.c_cflag |= CS8; //8 bits
53
	//options.c_lflag &= ~ICANON;
54
	//options.c_cc[VMIN] = 1;
55
	//options.c_cc[VTIME] = 50;
51 56

  
52 57
	if (tcsetattr(stream, TCSANOW, &options))
53 58
	{
......
64 69
static void* listen()
65 70
{
66 71
	char c;
72
	int count = 0;
73
	char buf[100];
67 74
	while (1)
68 75
	{
69 76
		if (read_char(&c) != 0) {
70 77
			printf("Listen failed.\n");
71 78
			return NULL;
72 79
		}
73

  
74
		printf("%c\n", c);
75
		usleep(1000);
80
		
81
		printf("%c", c); fflush(stdout);
82
		
83
		if(c == '\0')
84
			break;
76 85
	}
77 86

  
78 87
	return NULL;
......
99 108
	return 0;
100 109
}
101 110

  
102
static int put_char(char* c)
111
static int put_char(char c)
103 112
{
104
	if (write(stream, c, 1) == -1) {
113
	if (write(stream, &c, 1) == -1) {
105 114
		printf("Failed to write.\r\n");
106 115
		return -1;
107 116
	}

Also available in: Unified diff