Hi I have written a program in c to communicate with /dev/ttyS0 to a device . when i do write i am able to do that but i get ERROR: Resource temporarily unavailable, -1 as return from read(). can you please check what i am doing wrong . the port is supposed to be a half duplex ,9600bps,8N1 , and a inter byte delay of 4ms . also can anyone tell me how to set inter byte delay in milliseconds. Below is my code.
#include<stdio.h>
#include<termios.h>
#include<fcntl.h>
#include<unistd.h>
#include<errno.h>
#define BAUDRATE B9600
#define PORT "/dev/ttyS0"
int main()
{
struct termios new_tio,old_new_tio;
int fd;
unsigned char msgS[5]="",msgR[10]="";
fd = open(PORT,O_RDWR|O_NOCTTY|O_NDELAY);
if(fd == -1)
printf("Failed to open PORT: %s \n\n",PORT);
perror("Error:");
tcgetattr(fd,&old_new_tio);
memset(&new_tio,0,sizeof(new_tio));
new_tio.c_cflag = BAUDRATE|CS8|CLOCAL|CREAD|CSIZE|PARENB;
new_tio.c_oflag = 0;
new_tio.c_iflag = 0;
new_tio.c_cc[VMIN] = 10;
new_tio.c_cc[VTIME] = 0.004;
cfsetispeed(&new_tio,BAUDRATE);
cfsetospeed(&new_tio,BAUDRATE);
new_tio.c_cc[VINTR] = 0;
new_tio.c_cc[VQUIT] = 0;
new_tio.c_cc[VERASE] = 0;
new_tio.c_cc[VKILL] = 0;
new_tio.c_cc[VEOF] = 4;
new_tio.c_cc[VSWTC] = 0; /* '\0' */
new_tio.c_cc[VSTART] = 0; /* Ctrl-q */
new_tio.c_cc[VSTOP] = 0; /* Ctrl-s */
new_tio.c_cc[VSUSP] = 0; /* Ctrl-z */
new_tio.c_cc[VEOL] = 0; /* '\0' */
new_tio.c_cc[VREPRINT] = 0; /* Ctrl-r */
new_tio.c_cc[VDISCARD] = 0; /* Ctrl-u */
new_tio.c_cc[VWERASE] = 0; /* Ctrl-w */
new_tio.c_cc[VLNEXT] = 0; /* Ctrl-v */
new_tio.c_cc[VEOL2] = 0;
tcflush(fd,TCIFLUSH);
tcsetattr(fd,TCSANOW,&new_tio);
msgS[0] = '*';
msgS[1] = 'G';
msgS[2] = 'U';
msgS[3] = 'S';
msgS[4] = '\r';
printf("Message to be sent : %s \n\n",msgS);
int i = write(fd,&msgS,5);
if(i != 5)
printf("error while writing to the port\n\n");
int j = read(fd,&msgR,10);
if(j != 10)
printf("error while reading from port\n\n");
printf("Message Recieved : %s\n\n",msgR);
return 0;
}