Sfoglia il codice sorgente

Added unix helper, makefile, deleted jfm

Thomas Buck 13 anni fa
parent
commit
93543e6175
5 ha cambiato i file con 179 aggiunte e 1033 eliminazioni
  1. 2
    1
      .gitignore
  2. 0
    1032
      Cube Control/frame.jfm
  3. 62
    0
      Cube Control/helper/unixSerial.c
  4. 17
    0
      Cube Control/makefile
  5. 98
    0
      Cube Control/unixHelper.c

+ 2
- 1
.gitignore Vedi File

@@ -1,2 +1,3 @@
1 1
 ./.DS_Store
2
-Cube Control/.DS_Store
2
+Cube Control/.DS_Store
3
+Cube Control/helper/.DS_Store

+ 0
- 1032
Cube Control/frame.jfm
File diff soppresso perché troppo grande
Vedi File


+ 62
- 0
Cube Control/helper/unixSerial.c Vedi File

@@ -0,0 +1,62 @@
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 <unistd.h>
10
+#include <fcntl.h>
11
+#include <termios.h>
12
+
13
+#define BAUD B19200
14
+
15
+int fd = -1;
16
+
17
+// Open the serial port
18
+int serialOpen(char *port) {
19
+	struct termios options;
20
+
21
+	if (fd != -1) {
22
+		close(fd);
23
+	}
24
+	fd = open(port, O_RDWR | O_NOCTTY | O_NDELAY);
25
+	if (fd == -1) {
26
+		return -1;
27
+	}
28
+	
29
+	fcntl(fd, F_SETFL, FNDELAY); // read() isn't blocking'
30
+	tcgetattr(fd, &options);
31
+	cfsetispeed(&options, BAUD); // Set speed
32
+	cfsetospeed(&options, BAUD);
33
+	options.c_cflag |= (CLOCAL | CREAD);
34
+	
35
+	options.c_cflag &= ~PARENB; // 8N1
36
+	options.c_cflag &= ~CSTOPB;
37
+	options.c_cflag &= ~CSIZE;
38
+	options.c_cflag |= CS8;
39
+	
40
+	options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // Raw input
41
+	options.c_oflag &= ~OPOST; // Raw output
42
+	options.c_iflag &= ~(IXON | IXOFF | IXANY); // No flow control
43
+
44
+	tcsetattr(fd, TCSANOW, &options);
45
+
46
+	return 0;
47
+}
48
+
49
+// Write to port. Returns number of characters sent, -1 on error
50
+ssize_t serialWrite(char *data, size_t length) {
51
+	return write(fd, data, length);
52
+}
53
+
54
+// Read from port. Return number of characters read, 0 if none available, -1 on error
55
+ssize_t serialRead(char *data, size_t length) {
56
+	return read(fd, data, length);
57
+}
58
+
59
+// Close the serial Port
60
+void serialClose(void) {
61
+	close(fd);
62
+}

+ 17
- 0
Cube Control/makefile Vedi File

@@ -0,0 +1,17 @@
1
+JAVAC = javac
2
+CC = gcc
3
+TARGET = unix
4
+#TARGET = win
5
+
6
+
7
+all: java
8
+
9
+java: helper
10
+	$(JAVAC) *.java
11
+
12
+helper:
13
+	$(CC) -o serialHelper $(TARGET)Helper.c
14
+
15
+clean:
16
+	rm -f *.class
17
+	rm -f serialHelper

+ 98
- 0
Cube Control/unixHelper.c Vedi File

@@ -0,0 +1,98 @@
1
+/*
2
+ * By: Thomas Buck <taucher.bodensee@gmail.com>
3
+ * Visit: www.xythobuz.org
4
+ */
5
+#include <stdlib.h>
6
+#include <stdio.h>
7
+#include "helper/unixSerial.c"
8
+
9
+char *fileData = NULL;
10
+
11
+void removeFromBeginning(size_t size, size_t remove);
12
+size_t readFile(char *path);
13
+size_t getFileSize(FILE *fp);
14
+
15
+/*
16
+Return values:
17
+0: Success
18
+1: Usage error
19
+2: Serial Port Error
20
+3: Data File Error
21
+*/
22
+int main(int argc, char *argv[]) {
23
+	size_t length, written;
24
+	
25
+	if (argc < 3) {
26
+		printf("Usage:\n%s /dev/SerialPort /file/to/send\n", argv[0]);
27
+		return 1;
28
+	} else {
29
+		if (serialOpen(argv[1]) != 0) {
30
+			printf("Error: Could not open %s\n", argv[1]);
31
+			return 2;
32
+		}
33
+
34
+		length = readFile(argv[2]);
35
+		if (length == 0) {
36
+			printf("Error while reading %s\n", argv[2]);
37
+			return 3;
38
+		}
39
+
40
+		written = serialWrite(fileData, length);
41
+		while (written < length) {
42
+			removeFromBeginning(length, written);
43
+			length -= written;
44
+			written = serialWrite(fileData, length);
45
+		}
46
+
47
+		free(fileData);
48
+		fileData = NULL;
49
+		serialClose();
50
+		return 0;
51
+	}
52
+}
53
+
54
+void removeFromBeginning(size_t size, size_t remove) {
55
+	size_t i;
56
+	char *tmp = (char *)malloc((size - remove) * sizeof(char));
57
+
58
+	for (i = 0; i < (size - remove); i++) {
59
+		tmp[i] = fileData[i + remove];
60
+	}
61
+	free(fileData);
62
+	fileData = tmp;
63
+}
64
+
65
+size_t readFile(char *path) {
66
+	size_t size, i;
67
+	FILE *fp;
68
+
69
+	fp = fopen(path, "r");
70
+	if (!fp) {
71
+		return 0;
72
+	}
73
+
74
+	size = getFileSize(fp);
75
+	fileData = (char *)malloc(size * sizeof(char));
76
+	for (i = 0; i < size; i++) {
77
+		fileData[i] = fgetc(fp);
78
+	}
79
+
80
+	fclose(fp);
81
+	return size;
82
+}
83
+
84
+size_t getFileSize(FILE *fp) {
85
+	size_t size = 0;
86
+	int c;
87
+
88
+	fseek(fp, 0, 0); // Set file pointer to beginning
89
+	
90
+	do { // Calculate size
91
+		c = fgetc(fp);
92
+		size++;
93
+	} while (c != EOF);
94
+	
95
+	fseek(fp, 0, 0);
96
+
97
+	return size;
98
+}

Loading…
Annulla
Salva