I can't open serial port to start communication in linux ubuntu. I've tried this:
int OpenPort(void) {
int fd;
fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
if(fd == -1)
{
printf("Failed to open port.\n");
}
else
{
fcntl(fd, F_SETFL, 0);
printf("Opened!\n");
}
return(fd);
}
int main()
{
int x = OpenPort();
printf("%i\n", x);
exit(0);
}
I'm new in linux and found this code online, but it doesn't work for me.
printf("Failed to open port.\n")toprintf("Failed to open port: %s.\n", strerror(errno))orperror("Failed to open port.").errnowhich contains the error code. You can get a nice printable string with thestrerrorfunction. You can also use theperrorfunction to print the message directly.