Project

General

Profile

Revision 1085

Added by Emily Hart about 15 years ago

Added code for communication between a computer and the USB hub.

View differences:

branches/diagnostics/computer-hub/comp-hub.h
1
#include <termios.h>
2
#include <fcntl.h>
3
#include <stdio.h>
4
#include <stdlib.h>
5
#include <string.h>
6
#include <unistd.h>
7

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

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

  
10
static char* com_port = PORT_DEFAULT;
11
static int stream;
12

  
13
int main(void)
14
{
15
	if(lib_init() < 0)
16
		printf("Exiting...\n");
17
	
18
	//your code here
19
	
20
	return 0;
21
}
22

  
23
int lib_init()
24
{
25
	stream = open(com_port, O_RDWR);
26
	if (stream == -1)
27
	{
28
		printf("Failed to open connection on port ");
29
		printf("%i\n", *com_port);
30
		return -1;
31
	} else {
32
	  	printf("Successfully opened connection on port ");
33
		printf("%i\n", (int)(*com_port));
34
	}
35
	// set baud rate, etc. correctly
36
	struct termios options;
37
	if(tcgetattr(stream, &options))
38
		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;
46
	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;
51

  
52
	if (tcsetattr(stream, TCSANOW, &options))
53
	{
54
		printf("Error setting attributes.\n");
55
		return -1;
56
	}
57
	return 0;
58
}
59

  
60
/**
61
 * Listener function
62
 * At the moment, just prints out what it receives
63
 **/
64
static void* listen()
65
{
66
	char c;
67
	while (1)
68
	{
69
		if (read_char(&c) != 0) {
70
			printf("Listen failed.\n");
71
			return NULL;
72
		}
73

  
74
		printf("%c\n", c);
75
		usleep(1000);
76
	}
77

  
78
	return NULL;
79
}
80

  
81
static int read_char(char* buf)
82
{
83
	if (read(stream, buf, 1) == -1) {
84
		printf("Failed to read.\r\n");
85
		return -1;
86
	}
87
	return 0;
88
}
89

  
90
/**
91
 * Put functions
92
 **/
93
static int put_string(char *str)
94
{
95
	if (write(stream, str, strlen(str)) == -1) {
96
		printf("Failed to write.\r\n");
97
		return -1;
98
	}
99
	return 0;
100
}
101

  
102
static int put_char(char* c)
103
{
104
	if (write(stream, c, 1) == -1) {
105
		printf("Failed to write.\r\n");
106
		return -1;
107
	}
108
	return 0;
109
}
branches/diagnostics/computer-hub/Makefile
1
all:
2
	gcc comp-hub.c -o comp-hub
3

  
4
clean:
5
	rm comp-hub

Also available in: Unified diff