Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <termios.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- //Raspberry Pi to Arduino UART
- //Takes one argument and sends it over the UART as a string
- //Uart /dev/ttyAMA0 is setup and settings are not restored.
- int main(int argc, char ** argv) {
- if(argc == 2){
- printf("send: %s\n", argv[1]);
- } else {
- perror("wrong number of args");
- return -1;
- }
- int fd;
- // Open the Port. We want read/write, no "controlling tty" status, and open it no matter what state DCD is in
- fd = open("/dev/ttyAMA0", O_WRONLY | O_NOCTTY | O_NDELAY | O_NONBLOCK);
- if (fd == -1) {
- perror("open_port: Unable to open /dev/ttyAMA0 - ");
- return(-1);
- }
- // Turn off blocking for reads, use (fd, F_SETFL, FNDELAY) if you want that
- //fcntl(fd, F_SETFL, 0);
- //Set the baud rate
- struct termios options;
- tcgetattr(fd, &options);
- cfsetispeed(&options, B9600);
- cfsetospeed(&options, B9600);
- //Set flow control and all that
- options.c_cflag = B9600 | CS8 | CLOCAL | CREAD;
- options.c_iflag = IGNPAR | ICRNL;
- options.c_oflag = 0;
- //Flush line and set options
- tcflush(fd, TCIFLUSH);
- tcsetattr(fd, TCSANOW, &options);
- //set characters
- char* strOut;
- int len = asprintf(&strOut, "%s\n", argv[1]);
- // Write to the port
- int n = write(fd, strOut, len);
- if (n < 0) {
- perror("error writing");
- return -1;
- }
- // Don't forget to clean up
- close(fd);
- return 0;
- }
- //END OF FILE
Advertisement
Add Comment
Please, Sign In to add comment