#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
int main()
{
int fd;
int wd=0;
unsigned char buff[] = { 0x02, 0xFA, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x23, 0x01, 0x21, 0x03};

fd = open("/dev/ttyS0", O_RDONLY | O_NOCTTY | O_NDELAY );
if (fd == -1)
{printf("error opening");}
else
{
printf("hi1");
fcntl(fd, F_SETFL, 0);


wd=write(fd, buff, 13 );
printf("hi");
}
printf("%d", wd);
//i get fd to be 3 and wd to be -1 so what is the problem??
close(fd);
}

.

fd = open("/dev/ttyS0", O_RDONLY | O_NOCTTY | O_NDELAY );

it not gonna work if you use O_RDONLY tag,
because you tell the fd that I can use to read stuff, not to write into it.

so try this

fd = open("/dev/ttyS0", O_NOCTTY | O_NDELAY );
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.