Browse Source

Added Upload Utility.

Thomas Buck 12 years ago
parent
commit
d993b7da9b
4 changed files with 364 additions and 0 deletions
  1. 180
    0
      UploadTest/main.c
  2. 19
    0
      UploadTest/makefile
  3. 139
    0
      UploadTest/serial.c
  4. 26
    0
      UploadTest/serial.h

+ 180
- 0
UploadTest/main.c View File

@@ -0,0 +1,180 @@
1
+#include <stdio.h>
2
+#include <stdlib.h>
3
+#include <strings.h>
4
+
5
+#include "serial.h"
6
+
7
+#define OK 0x42
8
+#define ERROR 0x23
9
+
10
+void readAck(void);
11
+int serialWriteString(char *s);
12
+int serialReadTry(char *data, size_t length);
13
+int serialWriteTry(char *data, size_t length);
14
+void intHandler(int dummy);
15
+
16
+volatile int keepRunning = 1;
17
+
18
+#define suicide intHandler(0)
19
+
20
+int main(int argc, char *argv[]) {
21
+	char c;
22
+	int i;
23
+	
24
+	if (argc != 2) {
25
+		printf("Usage: %s /dev/port\n", argv[0]);
26
+		return 0;
27
+	}
28
+
29
+	if (serialOpen(argv[1]) != 0) {
30
+		printf("Could not open port: %s\n", argv[1]);
31
+		return 0;
32
+	}
33
+
34
+	signal(SIGINT, intHandler);
35
+	signal(SIGQUIT, intHandler);
36
+
37
+	printf("Port opened. Sending test data:\n");
38
+
39
+	printf("\tSending command 's'...");
40
+	c = 's';
41
+	if (serialWriteTry(&c, 1) != 0) {
42
+		printf(" Could not send it!\n");
43
+		suicide;
44
+	}
45
+	printf(" Success!\n");
46
+
47
+	readAck();
48
+
49
+	printf("\tSending anim count (1)...");
50
+	c = 1;
51
+	if (serialWriteTry(&c, 1) != 0) {
52
+		printf(" Could not send it!\n");
53
+		suicide;
54
+	}
55
+	printf(" Success!\n");
56
+
57
+	readAck();
58
+
59
+	printf("\tSending frame count (1)...");
60
+	c = 1;
61
+	if (serialWriteTry(&c, 1) != 0) {
62
+		printf(" Could not send it!\n");
63
+		suicide;
64
+	}
65
+	printf(" Success!\n");
66
+
67
+	readAck();
68
+
69
+	printf("\tSending duration (1)...");
70
+	c = 1;
71
+	if (serialWriteTry(&c, 1) != 0) {
72
+		printf(" Could not send it!\n");
73
+		suicide;
74
+	}
75
+	printf(" Success!\n");
76
+
77
+	readAck();
78
+
79
+	printf("\tSending data");
80
+	for(i = 0; i < 64; i++) {
81
+		c = (char)i; // Some test data
82
+		if (serialWriteTry(&c, 1) != 0) {
83
+			printf(" Error while sending!\n");
84
+			suicide;
85
+		} else {
86
+			printf(".");
87
+		}
88
+	}
89
+	printf(" Success!\n");
90
+
91
+	readAck();
92
+
93
+	printf("\tSending end sequence");
94
+	for (i = 0; i < 4; i++) {
95
+		// Send 4 OKs
96
+		c = OK;
97
+		if (serialWriteTry(&c, 1) != 0) {
98
+			printf(" Error while sending!\n");
99
+			suicide;
100
+		} else {
101
+			printf(".");
102
+		}
103
+	}
104
+	printf(" Success!\n");
105
+
106
+	readAck();
107
+
108
+	printf("Finished. Success!\n");
109
+
110
+	serialClose();
111
+	return 0;
112
+}
113
+
114
+void readAck() {
115
+	char c;
116
+	printf("Awaiting acknowledge...");
117
+	if (serialReadTry(&c, 1) != 0) {
118
+		printf(" Could not read!\n");
119
+		suicide;
120
+	}
121
+	if (c != OK) {
122
+		printf(" Was not OK!\n");
123
+		suicide;
124
+	}
125
+	printf(" OK\n");
126
+}
127
+
128
+int serialWriteString(char *s) {
129
+	return serialWriteTry(s, strlen(s));
130
+}
131
+
132
+// 1 on error, 0 on success
133
+int serialReadTry(char *data, size_t length) {
134
+	int i = 0;
135
+	int written = 0;
136
+	int ret;
137
+	while (keepRunning) {
138
+		ret = serialRead((data + written), (length - written));
139
+		if (ret == -1) {
140
+			i++;
141
+		} else {
142
+			written += ret;
143
+		}
144
+		if (i > 10) {
145
+			return 1;
146
+		}
147
+		if (written == length) {
148
+			break;
149
+		}
150
+	}
151
+	return 0;
152
+}
153
+
154
+// 1 on error, 0 on success
155
+int serialWriteTry(char *data, size_t length) {
156
+	int i = 0;
157
+	int written = 0;
158
+	int ret;
159
+	while (keepRunning) {
160
+		ret = serialWrite((data + written), (length - written));
161
+		if (ret == -1) {
162
+			i++;
163
+		} else {
164
+			written += ret;
165
+		}
166
+		if (i > 10) {
167
+			return 1;
168
+		}
169
+		if (written == length) {
170
+			break;
171
+		}
172
+	}
173
+	return 0;
174
+}
175
+
176
+void intHandler(int dummy) {
177
+	keepRunning = 0;
178
+	serialClose();
179
+	exit(0);
180
+}

+ 19
- 0
UploadTest/makefile View File

@@ -0,0 +1,19 @@
1
+CC = gcc
2
+SRC = main.c
3
+SRC += serial.c
4
+
5
+TARGET = uploadTest
6
+
7
+OBJ = $(SRC:.c=.o)
8
+
9
+all: $(TARGET)
10
+
11
+$(TARGET): $(OBJ)
12
+	$(CC) -o $(TARGET) $(OBJ)
13
+
14
+%.o: %.c
15
+	$(CC) -x c -c $< -o $@
16
+
17
+clean:
18
+	$(RM) *.o
19
+	$(RM) $(TARGET)

+ 139
- 0
UploadTest/serial.c View File

@@ -0,0 +1,139 @@
1
+/*
2
+ * POSIX compatible serial port library
3
+ * Uses 8 databits, no parity, 1 stop bit, no handshaking
4
+ * By: Thomas Buck <taucher.bodensee@gmail.com>
5
+ * Visit: www.xythobuz.org
6
+ */
7
+
8
+#include <stdio.h>
9
+#include <stdlib.h>
10
+#include <string.h>
11
+#include <unistd.h>
12
+#include <fcntl.h>
13
+#include <termios.h>
14
+#include <dirent.h>
15
+#include <errno.h>
16
+
17
+#include "serial.h"
18
+
19
+int fd = -1;
20
+
21
+// Open the serial port
22
+int serialOpen(char *port) {
23
+	struct termios options;
24
+
25
+	if (fd != -1) {
26
+		close(fd);
27
+	}
28
+	fd = open(port, O_RDWR | O_NOCTTY | O_NDELAY);
29
+	if (fd == -1) {
30
+		return -1;
31
+	}
32
+	
33
+	fcntl(fd, F_SETFL, FNDELAY); // read() isn't blocking'
34
+	tcgetattr(fd, &options);
35
+	cfsetispeed(&options, BAUD); // Set speed
36
+	cfsetospeed(&options, BAUD);
37
+	options.c_cflag |= (CLOCAL | CREAD);
38
+	
39
+	options.c_cflag &= ~PARENB; // 8N1
40
+	options.c_cflag &= ~CSTOPB;
41
+	options.c_cflag &= ~CSIZE;
42
+	options.c_cflag |= CS8;
43
+	
44
+	options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // Raw input
45
+	options.c_oflag &= ~OPOST; // Raw output
46
+	options.c_iflag &= ~(IXON | IXOFF | IXANY); // No flow control
47
+
48
+	tcsetattr(fd, TCSANOW, &options);
49
+
50
+	return 0;
51
+}
52
+
53
+// Write to port. Returns number of characters sent, -1 on error
54
+ssize_t serialWrite(char *data, size_t length) {
55
+	return write(fd, data, length);
56
+}
57
+
58
+// Read from port. Return number of characters read, 0 if none available, -1 on error
59
+ssize_t serialRead(char *data, size_t length) {
60
+	ssize_t temp = read(fd, data, length);
61
+	if ((temp == -1) && (errno == EAGAIN)) {
62
+		return 0;
63
+	} else {
64
+		return temp;
65
+	}
66
+}
67
+
68
+// Close the serial Port
69
+void serialClose(void) {
70
+	close(fd);
71
+}
72
+
73
+char** namesInDev(int *siz) {
74
+	DIR *dir;
75
+	struct dirent *ent;
76
+	int i = 0, size = 0;
77
+	char **files = NULL;
78
+	dir = opendir("/dev/");
79
+	while ((ent = readdir(dir)) != NULL) {
80
+		size++;
81
+	}
82
+	files = (char **)malloc((size + 1) * sizeof(char *));
83
+	files[size++] = NULL;
84
+	closedir(dir);
85
+	dir = opendir("/dev/");
86
+	while ((ent = readdir(dir)) != NULL) {
87
+		files[i] = (char *)malloc((strlen(ent->d_name) + 1) * sizeof(char));
88
+		files[i] = strcpy(files[i], ent->d_name);
89
+		i++;
90
+	}
91
+	closedir(dir);
92
+
93
+	char *tmp = NULL;
94
+	// Fix every string, addin /dev/ in front of it...
95
+	for (i = 0; i < (size - 1); i++) {
96
+		tmp = (char *)malloc((strlen(files[i]) + 6) * sizeof(char));
97
+		tmp[0] = '/';
98
+		tmp[1] = 'd';
99
+		tmp[2] = 'e';
100
+		tmp[3] = 'v';
101
+		tmp[4] = '/';
102
+		files[i] = strncat(tmp, files[i], strlen(files[i]));
103
+	}
104
+
105
+	*siz = size;
106
+	return files;
107
+}
108
+
109
+char** getSerialPorts() {
110
+	int size;
111
+	char** files = namesInDev(&size);
112
+	char **fin = NULL, **finish = NULL;
113
+	int i = 0, j = 0, f, g;
114
+
115
+	fin = (char **)malloc(size * sizeof(char *));
116
+	// Has space for all files in dev!
117
+
118
+	while (files[i] != NULL) {
119
+		// Filter for SEARCH and if it is a serial port
120
+		if (strstr(files[i], SEARCH) != NULL) {
121
+			fin[j++] = files[i];
122
+		} else {
123
+			free(files[i]);
124
+		}
125
+		i++;
126
+	}
127
+	free(files);
128
+
129
+	// Copy in memory with matching size, NULL at end
130
+	finish = (char **)malloc((j + 1) * sizeof(char *));
131
+	finish[j] = NULL; 
132
+	for (i = 0; i < j; i++) {
133
+		finish[i] = fin[i];
134
+	}
135
+
136
+	free(fin);
137
+
138
+	return finish;
139
+}

+ 26
- 0
UploadTest/serial.h View File

@@ -0,0 +1,26 @@
1
+/*
2
+ * POSIX compatible serial port library
3
+ * Uses 8 databits, no parity, 1 stop bit, no handshaking
4
+ * By: Thomas Buck <taucher.bodensee@gmail.com>
5
+ * Visit: www.xythobuz.org
6
+ */
7
+
8
+// Use POSIX Baud constants (B2400, B9600...)
9
+#define BAUD B38400
10
+// Searchterm for ports in unix
11
+#define SEARCH "tty."
12
+
13
+// Open the serial port. Return 0 on success, -1 on error
14
+int serialOpen(char *port);
15
+
16
+// Write to port. Returns number of characters sent, -1 on error
17
+ssize_t serialWrite(char *data, size_t length);
18
+
19
+// Read from port. Return number of characters read, 0 if none available, -1 on error
20
+ssize_t serialRead(char *data, size_t length);
21
+
22
+// Close the serial Port
23
+void serialClose(void);
24
+
25
+// String array with serial port names
26
+char** getSerialPorts(void);

Loading…
Cancel
Save