0

Dear Colleagues. I'm trying write C program for Linux to write serial port (Arduino) and wait for answer. I know, there was many Q about it on forum, but all I tried have the same problem - successfully write - but not read answer. For example, here and here. I have found two separate files for write and for read.

I'm compile and run read file in one terminal window, and write file in other. Works great. But I can't merge them in one file to write and then wait for answer. The same problem - write, but not read.

Here is like I tried:

#include <stdio.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#include <errno.h>
void main(void)
{
int fd;
fd = open("/dev/ttyS0",O_RDWR | O_NOCTTY | O_SYNC); 
if(fd == -1)
       printf("\n  Error! in Opening ttyUSB0  ");
else
       printf("\n  ttyS0 Opened Successfully ");
struct termios SerialPortSettings;  
tcgetattr(fd, &SerialPortSettings);
cfsetispeed(&SerialPortSettings,B9600);
cfsetospeed(&SerialPortSettings,B9600);
        /* 8N1 Mode */
SerialPortSettings.c_cflag &= ~PARENB; 
SerialPortSettings.c_cflag &= ~CSTOPB;
SerialPortSettings.c_cflag &= ~CSIZE;
SerialPortSettings.c_cflag |=  CS8;
SerialPortSettings.c_cflag &= ~CRTSCTS; 
SerialPortSettings.c_cflag |= CREAD | CLOCAL;
SerialPortSettings.c_iflag &= ~(IXON | IXOFF | IXANY);
SerialPortSettings.c_iflag &= ~(ICANON | ECHO | ECHOE | ISIG);
SerialPortSettings.c_oflag &= ~OPOST;/*No Output Processing*/
        /* Setting Time outs */
SerialPortSettings.c_cc[VMIN] = 10; /* Read at least 10 characters */
SerialPortSettings.c_cc[VTIME] = 0; /* Wait indefinetly   */
if((tcsetattr(fd,TCSANOW,&SerialPortSettings)) != 0)
    printf("\n  ERROR ! in Setting attributes");
else
    printf("\n  BaudRate = 9600 \n  StopBits = 1 \n  Parity   = none");

char write_buffer[] = "Hello/n";
int  bytes_written  = 0;
printf("\n  %s written to ttyUSB0",write_buffer);
printf("\n  %d Bytes written to ttyUSB0", bytes_written);
printf("\n +----------------------------------+\n\n");          
//tcflush(fd, TCIFLUSH); /* Discards old data in the rx buffer            */
char read_buffer[32];
int  bytes_read = 0;
int i = 0;

write(fd,write_buffer,sizeof(write_buffer));/* use write() to send data to port   */
usleep ((8 + 25) * 100);            /* Delay */
read(fd,&read_buffer,32);            /* Read the data                   */
printf("\n\n  Bytes Rxed -%d", bytes_read); /* Print the number of bytes read */
printf("\n\n  ");
for(i=0;i<bytes_read;i++)    /*printing only the received characters*/
        printf("%c",read_buffer[i]);
printf("\n +----------------------------------+\n\n\n");
close(fd); /* Close the serial port */
}

Thanks for your help, Have a nice day.

Community
  • 1
  • 1
fapw
  • 185
  • 1
  • 2
  • 15
  • 3
    You have a variable called `bytes_read` that is initialized to `0`. Nothing in the code changes that value. – user3386109 Dec 01 '15 at 22:51
  • "It doesn't work" - what happens instead? – user253751 Dec 01 '15 at 22:56
  • 1
    Don't ignore the result from system calls. Especially, don't ignore the result returned by read(). – Martin James Dec 01 '15 at 23:09
  • 1
    The statement `read(fd,&read_buffer,32)` should be corrected to `bytes_read = read(fd, read_buffer, 32)` per comment by @user3386109, and to remove indirection of array. – sawdust Dec 02 '15 at 01:24
  • Don't wait answer, check message ending delimiter. waste system resources but gained maximum connectivity. – dsgdfg Dec 02 '15 at 11:35
  • Your code has the same initialization bug as in: https://stackoverflow.com/questions/37944461/linux-reading-data-from-uart – sawdust Oct 24 '18 at 05:36

1 Answers1

0

You have a variable called bytes_read that is initialized to 0. Nothing in the code changes that value. – user3386109

The statement read(fd,&read_buffer,32) should be corrected to bytes_read = read(fd, read_buffer, 32) … – sawdust

Armali
  • 18,255
  • 14
  • 57
  • 171