LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   serail port read function blocked (https://www.linuxquestions.org/questions/linux-newbie-8/serail-port-read-function-blocked-4175525635/)

amitbpl5 11-17-2014 06:48 AM

serail port read function blocked
 
#include <iostream>
#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 <string.h>
#include <string>
using namespace std;


//Initialize serial port
int initport(int fd)
{
int portstatus = 0;

struct termios options;
// Get the current options for the port...
tcgetattr(fd, &options);
// Set the baud rates to 115200...
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
// Enable the receiver and set local mode...
options.c_cflag |= (CLOCAL | CREAD);

options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag = CS8;
//options.c_cflag |= SerialDataBitsInterp(8); /* CS8 - Selects 8 data bits */
options.c_cflag &= ~CRTSCTS; // disable hardware flow control
options.c_iflag &= ~(IXON | IXOFF | IXANY); // disable XON XOFF (for transmit and receive)
//options.c_cflag |= CRTSCTS; /* enable hardware flow control */


options.c_cc[VMIN] = 1; //min carachters to be read
options.c_cc[VTIME] = 10; //Time to wait for data (tenths of seconds)


// Set the new options for the port...
//tcsetattr(fd, TCSANOW, &options);


//Set the new options for the port...
tcflush(fd, TCIFLUSH);
if (tcsetattr(fd, TCSANOW, &options)==-1)
{
perror("On tcsetattr:");
portstatus = -1;
}
else
portstatus = 1;


return portstatus;
}


/*
* 'open_port()' - Open serial port 1.
*
* Returns the file descriptor on success or -1 on error.
*/
int open_port(void)
{
int fd; /* File descriptor for the port */
fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK);

if (fd == -1)
{
/*
* Could not open the port.
*/
perror("open_port: Unable to open /dev/ttyS0 --- \n");
}
else
fcntl(fd, F_SETFL, 0);

return (fd);
}

int main(void)
{

int serial_fd = open_port();

if(serial_fd == -1)
printf("Error opening serial port /dev/ttyS0 \n");
else
{
printf("Serial Port /dev/ttyS0 is now open \n");

if(initport(serial_fd) == -1)
{
printf("Error Initializing port");
close(serial_fd);
return 0;
}

sleep(.5);
//usleep(500000);
//printf("size of data being sent = %ld", sizeof("~ver~\n\r"));
unsigned char linkpack[2];
string str = "@R5-ON#";
char str1[8] = {'@','R','6','-','O','N','#'};
linkpack[0] = '@';
linkpack[1] = 'T';
linkpack[2] = '5';
linkpack[3] = '-';
linkpack[4] = 'O';
linkpack[5] = 'N';
linkpack[6] = '#';
//linkpack[7] = '@';
int n;
//write(serial_fd,str1, 8);
for(int i=0; i <8; i++)
{
n = write(serial_fd,&linkpack[i], 1);
usleep(5000);
printf("hell ");
}


if (n < 0)
fputs("write() of 8 bytes failed!\n", stderr);
else
{
printf("Successfully wrote 8 bytes\n");



//sleep(1);
char buffer[32];


int n = read(serial_fd, buffer, sizeof(buffer));
usleep(10000);
if (n < 0)
fputs("read failed!\n", stderr);
else
{
printf("Successfully read from serial port -- %s\n", buffer);

}


}

sleep(1);


printf("\n\nNow closing Serial Port /dev/ttyS0 \n\n");

close(serial_fd);
}


return 0;
}

rtmistler 11-17-2014 12:10 PM

Post code within [code][/code] tags please. There's an option in Advanced mode to assist with that.

Check your ERRNO value after the call to open(). You likely need to be root. I usually only use O_RDWR and O_NONBLOCK. Also you might want to check and see what O_NDELAY is defined to be, it's possible that O_NONBLOCK equals O_NDELAY.


All times are GMT -5. The time now is 07:33 PM.