Skip to content

Instantly share code, notes, and snippets.

@aborilov
Last active December 18, 2015 01:29
Show Gist options
  • Save aborilov/5703954 to your computer and use it in GitHub Desktop.
Save aborilov/5703954 to your computer and use it in GitHub Desktop.
write to serial port
#include <sys/types.h>
#include <sys/stat.h>
#include <termios.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <unistd.h>
#define BAUDRATE B9600
#define DEVICE "/dev/ttySP2"
main(){
printf("hello");
int fd;
struct termios opt;
fd = open(DEVICE, O_RDWR | O_NOCTTY | O_NDELAY);
if ( fd < 0 ){
perror(DEVICE);
}
unsigned char data[200] = "Hellodkslnkfndkslakdjaskldjaskldjasasdasdasd";
opt.c_cflag = B9600 | CS8 | CLOCAL | CREAD;
opt.c_iflag = IGNPAR;
opt.c_lflag = 0;
opt.c_oflag = 0;
opt.c_cc[VMIN] = 0; //Minimum bytes available
opt.c_cc[VTIME] = 0;
tcflush(fd, TCIFLUSH);
tcsetattr (fd, TCSANOW, &opt);
while(1){
printf("Sending\n");
int sended = write(fd, &data, 20);
tcflush(fd, TCIOFLUSH);
sleep(1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment