Skip to content

Instantly share code, notes, and snippets.

@nitheeshkl
Last active July 15, 2020 14:42
Show Gist options
  • Save nitheeshkl/6fdb0ba66691ef1636228a720c74d481 to your computer and use it in GitHub Desktop.
Save nitheeshkl/6fdb0ba66691ef1636228a720c74d481 to your computer and use it in GitHub Desktop.
serial port write in c
#include <stdio.h> /* Standard input/output definitions */
#include <string.h> /* String function definitions */
#include <unistd.h> /* UNIX standard function definitions */
#include <fcntl.h> /* File control definitions */
#include <errno.h> /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */
#include <stdlib.h> /* Int to string conversion */
int open_port(char *port)
{
int fd;
fd = open(port, O_RDWR | O_NOCTTY | O_NDELAY);
if(fd == -1){
//could not open port
perror("open_port: unable to open port");
} else {
fcntl(fd, F_SETFL, 0);
}
return (fd);
}
int write_data(int fd, char *data, int bytes)
{
int n;
n = write(fd, data, bytes);
if(n < 0){
perror("write() of byte failed!\n");
}
return n;
}
void close_port(int fd)
{
close(fd);
return;
}
/* open the port specified by port. return file handle */
int open_port(char *port);
/* write data of size bytes to fd. return number of bytes written */
int write_data(int fd, char* data, int bytes);
/* close port specified by fd */
void close_port(int fd);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment