Statistics
| Revision:

root / branches / diagnostics / computer-hub / comp-hub.c @ 1119

History | View | Annotate | Download (2.04 KB)

1
#include "comp-hub.h"
2

    
3
// ALSO DON'T FORGET TO CHECK OUT
4
// ATTRIBUTES ON LINES 39 ish TO 50 ish
5
#define PORT_DEFAULT "/dev/ttyUSB0"
6
#define BAUD_RATE 115200
7

    
8
static char* com_port = PORT_DEFAULT;
9
static int stream;
10

    
11
int main(void)
12
{
13
        if(lib_init() < 0)
14
                printf("Exiting...\n");
15
        
16
        //your code here
17
        listen();
18
        put_char('r');
19
        listen();
20
        put_char('b');
21
        listen();
22
        put_char('m');
23
        listen();
24
        
25
        return 0;
26
}
27

    
28
int lib_init()
29
{
30
        stream = open(com_port, O_RDWR);
31
        if (stream == -1)
32
        {
33
                printf("Failed to open connection on port ");
34
                printf("%i\n", *com_port);
35
                return -1;
36
        } else {
37
                  printf("Successfully opened connection on port ");
38
                printf("%i\n", (int)(*com_port));
39
        }
40
        // set baud rate, etc. correctly
41
        struct termios options;
42
        if(tcgetattr(stream, &options))
43
                printf("Error getting attributes");
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
51
        options.c_cflag &= ~CSIZE;
52
        options.c_cflag |= CS8; //8 bits
53
        //options.c_lflag &= ~ICANON;
54
        //options.c_cc[VMIN] = 1;
55
        //options.c_cc[VTIME] = 50;
56

    
57
        if (tcsetattr(stream, TCSANOW, &options))
58
        {
59
                printf("Error setting attributes.\n");
60
                return -1;
61
        }
62
        return 0;
63
}
64

    
65
/**
66
 * Listener function
67
 * At the moment, just prints out what it receives
68
 **/
69
static void* listen()
70
{
71
        char c;
72
        int count = 0;
73
        char buf[100];
74
        while (1)
75
        {
76
                if (read_char(&c) != 0) {
77
                        printf("Listen failed.\n");
78
                        return NULL;
79
                }
80
                
81
                printf("%c", c); fflush(stdout);
82
                
83
                if(c == '\0')
84
                        break;
85
        }
86

    
87
        return NULL;
88
}
89

    
90
static int read_char(char* buf)
91
{
92
        if (read(stream, buf, 1) == -1) {
93
                printf("Failed to read.\r\n");
94
                return -1;
95
        }
96
        return 0;
97
}
98

    
99
/**
100
 * Put functions
101
 **/
102
static int put_string(char *str)
103
{
104
        if (write(stream, str, strlen(str)) == -1) {
105
                printf("Failed to write.\r\n");
106
                return -1;
107
        }
108
        return 0;
109
}
110

    
111
static int put_char(char c)
112
{
113
        if (write(stream, &c, 1) == -1) {
114
                printf("Failed to write.\r\n");
115
                return -1;
116
        }
117
        return 0;
118
}