├── README ├── Makefile └── canonicalarduinoread.c /README: -------------------------------------------------------------------------------- 1 | www.chrisheydrick.com 2 | 3 | June 23 2012 4 | 5 | CanonicalArduinoRead write a byte to an Arduino, and then 6 | receives a serially transmitted string in response. 7 | 8 | The call/response Arduino sketch is here: 9 | https://gist.github.com/2980344 10 | 11 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # Generic GNUMakefile found on Wikipedia 2 | # Just a snippet to stop executing under other make(1) commands 3 | # that won't understand these lines 4 | ifneq (,) 5 | This makefile requires GNU Make. 6 | endif 7 | 8 | PROGRAM = canonicalarduinoread 9 | C_FILES := $(wildcard *.c) 10 | OBJS := $(patsubst %.c, %.o, $(C_FILES)) 11 | CC = cc 12 | CFLAGS = 13 | LDFLAGS = 14 | 15 | all: $(PROGRAM) 16 | 17 | $(PROGRAM): .depend $(OBJS) 18 | $(CC) $(CFLAGS) $(OBJS) $(LDFLAGS) -o $(PROGRAM) 19 | 20 | depend: .depend 21 | 22 | .depend: cmd = gcc -MM -MF depend $(var); cat depend >> .depend; 23 | .depend: 24 | @echo "Generating dependencies..." 25 | @$(foreach var, $(C_FILES), $(cmd)) 26 | @rm -f depend 27 | 28 | -include .depend 29 | 30 | # These are the pattern matching rules. In addition to the automatic 31 | # variables used here, the variable $* that matches whatever % stands for 32 | # can be useful in special cases. 33 | %.o: %.c 34 | $(CC) $(CFLAGS) -c $< -o $@ 35 | 36 | %: %.c 37 | $(CC) $(CFLAGS) -o $@ $< 38 | 39 | clean: 40 | rm -f .depend *.o 41 | 42 | .PHONY: clean depend 43 | 44 | 45 | -------------------------------------------------------------------------------- /canonicalarduinoread.c: -------------------------------------------------------------------------------- 1 | /* www.chrisheydrick.com 2 | 3 | June 23 2012 4 | 5 | CanonicalArduinoRead write a byte to an Arduino, and then 6 | receives a serially transmitted string in response. 7 | 8 | The call/response Arduino sketch is here: 9 | https://gist.github.com/2980344 10 | 11 | Arduino sketch details at www.chrisheydrick.com 12 | */ 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | #define DEBUG 1 25 | 26 | int main(int argc, char *argv[]) 27 | { 28 | int fd, n, i; 29 | char buf[64] = "temp text"; 30 | struct termios toptions; 31 | 32 | /* open serial port */ 33 | fd = open("/dev/ttyACM0", O_RDWR | O_NOCTTY); 34 | printf("fd opened as %i\n", fd); 35 | 36 | /* wait for the Arduino to reboot */ 37 | usleep(3500000); 38 | 39 | /* get current serial port settings */ 40 | tcgetattr(fd, &toptions); 41 | /* set 9600 baud both ways */ 42 | cfsetispeed(&toptions, B9600); 43 | cfsetospeed(&toptions, B9600); 44 | /* 8 bits, no parity, no stop bits */ 45 | toptions.c_cflag &= ~PARENB; 46 | toptions.c_cflag &= ~CSTOPB; 47 | toptions.c_cflag &= ~CSIZE; 48 | toptions.c_cflag |= CS8; 49 | /* Canonical mode */ 50 | toptions.c_lflag |= ICANON; 51 | /* commit the serial port settings */ 52 | tcsetattr(fd, TCSANOW, &toptions); 53 | 54 | /* Send byte to trigger Arduino to send string back */ 55 | write(fd, "0", 1); 56 | /* Receive string from Arduino */ 57 | n = read(fd, buf, 64); 58 | /* insert terminating zero in the string */ 59 | buf[n] = 0; 60 | 61 | printf("%i bytes read, buffer contains: %s\n", n, buf); 62 | 63 | if(DEBUG) 64 | { 65 | printf("Printing individual characters in buf as integers...\n\n"); 66 | for(i=0; i