0

I have connected an HC-05 bluetooth module to microZed board and trying to send and receive data via uart in Linux, Now the the send code is working when I send data from my board to the app it works perfectly given below is my code for sending

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <pthread.h> /* for threads */
#include <termios.h> /* uart */
#include <fcntl.h> /* uart */
#include <unistd.h> /* uart */
#define MODEMDEVICE "/dev/ttyPS1"
int main()
{
 printf("Opening %s\n", MODEMDEVICE);
 int portfd = open(MODEMDEVICE, O_RDWR | O_NOCTTY);
 if (portfd < 0) {
 printf("ERROR coultn't open %s\n", MODEMDEVICE);
 return -1;
 }
 /* set terminal settings */
 struct termios tty;
 tcgetattr(portfd, &tty);

 cfsetospeed(&tty, (speed_t)B9600);
 cfsetispeed(&tty, (speed_t)B9600);
 tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;
 tty.c_iflag = IGNBRK;
 tty.c_lflag = ICANON;
 tty.c_oflag = 0;
 tty.c_cflag |= CLOCAL | CREAD;
 tty.c_cc[VTIME] = 0;
 tty.c_cc[VMIN] = 1;
 tty.c_iflag &= ~(IXON | IXOFF | IXANY);
 tty.c_cflag &= ~(PARENB | PARODD);
 tty.c_cflag |= PARENB;
 tty.c_cflag &= ~CSTOPB;
 tcsetattr(portfd, TCSANOW, &tty);
 /* sleep a bit */
 usleep(200000);
 /* flush possible characters in the input buffer */
 tcflush(portfd, TCIOFLUSH);
 char buf;

 int i;

 while(1) {

    buf++;
    write(portfd, &buf, 1);
    write(portfd, "\r\n", 2);
    usleep(200000);

 }
 return 0;
}

Now the problem arises when I try send data from the app to the Bluetooth module , sometimes the program stops and says "random nonblocking pool initialized" or it gets stuck at i = read(portfd, buf, 20); in the code given below

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <pthread.h> /* for threads */
#include <termios.h> /* uart */
#include <fcntl.h> /* uart */
#include <unistd.h> /* uart */
#define MODEMDEVICE "/dev/ttyPS1"
int main()
{
 printf("Opening %s\n", MODEMDEVICE);
 int portfd = open(MODEMDEVICE, O_RDWR | O_NOCTTY);
 if (portfd < 0) {
 printf("ERROR coultn't open %s\n", MODEMDEVICE);
 return -1;
 }

    printf("hello1\n\r");
 /* set terminal settings */
 struct termios tty;
 tcgetattr(portfd, &tty);

 cfsetospeed(&tty, (speed_t)B9600);
 cfsetispeed(&tty, (speed_t)B9600);
 tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;
 tty.c_iflag = IGNBRK;
 tty.c_lflag = ICANON;
 tty.c_oflag = 0;
 tty.c_cflag |= CLOCAL | CREAD;
 tty.c_cc[VTIME] = 0;
 tty.c_cc[VMIN] = 1;
 tty.c_iflag &= ~(IXON | IXOFF | IXANY);
 tty.c_cflag &= ~(PARENB | PARODD);
 tty.c_cflag |= PARENB;
 tty.c_cflag &= ~CSTOPB;
 tcsetattr(portfd, TCSANOW, &tty);
 /* sleep a bit */
    printf("hello2\n\r");
 usleep(200000);
 /* flush possible characters in the input buffer */
 tcflush(portfd, TCIOFLUSH);
 char buf[20];
        printf("hello3\n\r");
 int i;

 while(1) {
    i = read(portfd, buf, 20); 
        printf("hello\n\r");

    buf[i] = 0;
    printf("%s", buf);
    printf("\n\r");

 }
 return 0;
}

Any suggestions how can I fix this ?

Novice_Developer
  • 1,432
  • 2
  • 19
  • 33
  • Do you expect the 2nd program to read the answer to what the 1st program sent? – alk Jul 31 '16 at 17:59
  • Also the sending code sends the uninitialised content of `buf`. – alk Jul 31 '16 at 18:01
  • 1
    You need to do a better job of checking return codes of all syscalls. Your termios settings are not rational. First you disable PARENB, but then restore it. And PARENB without ISTRIP is unusual. See [Setting Terminal Modes Properly](http://www.chemie.fu-berlin.de/chemnet/use/info/libc/libc_12.html#SEC237) and [Serial Programming Guide for POSIX Operating Systems](http://www.cmrr.umn.edu/~strupp/serial.html). Are you positive that the data received is canonical? – sawdust Aug 01 '16 at 07:46

1 Answers1

0

I found the solution , first mistake was pointed out by this post . I should have set time to wait for character read to greater than 0 and minimum number of characters to read to zero

 tty.c_cc[VTIME] = 5;
 tty.c_cc[VMIN] = 0;

and I needed to set the tty.c_lflag flag to 0 to enable raw input instead of canonical, now the code is reading the data constantly

Community
  • 1
  • 1
Novice_Developer
  • 1,432
  • 2
  • 19
  • 33
  • 1
    *"... I needed to set the tty.c_lflag flag to 0"* -- I never suggested that. That's bad code. See http://stackoverflow.com/questions/37944461/linux-reading-data-from-uart/37956065#37956065 – sawdust Aug 03 '16 at 17:24