2

I am writing a c++ code for letter recognition using OpenCv and tesseract, then I want to send a command to serial port ( Arduino) but the computer fezzes and I have to restart it.

I did some image processing in the code : convert to gray then blur the image then threshold and crop

finally I pass the image to Tesseract API to get the letter and save the letter in a text file.

I open the saved file and print the content of the file. then I open serial port connected with arduino and send a letter to the serial port.

when I compile the code the image processing part is done, the file is opened and the content of the file is printed, and it sends to serial port but then the code crashes and computer fezzes.

here is the code :

int fd;
char *buff;



int open_port(void)
{

 fd = open("/dev/ttyACM0", O_RDWR | O_NOCTTY | O_NDELAY);
 if (fd == -1)
 {
  perror("open_port: Unable to open /dev/kittens ");
 }
  else
   fcntl(fd, F_SETFL, 0);

 return (fd);
}

int main( int argc, char** argv )
{
open_port();
int const max_BINARY_value = 255;
 char path[255];



//capture image
  //gray 
  //Gaussian blur image
      //threshold
//crop 



system("tesseract crop.jpg txt -psm 10");//pass it to tesseract API and get the result in file
    ifstream inFile;

  inFile.open ("txt.txt", ofstream::in );//open file contains the letter


char singleCharacter;


    inFile.get(singleCharacter);


    while (!inFile.eof())
    {
         cout << singleCharacter;
         inFile.get(singleCharacter);
    }



int wr,rd;


char msg[]="W";


do 
{

wr = write(fd, msg, 1);
printf("debugging - Wrote to port\n");   
usleep(10000);

if (wr < 0) {
    fputs("write() to port /dev/ttyACM0 failed!\n", stderr);
    break;
            }
 } 
while (buff != msg);

if (buff==msg)
printf(buff, "String Found! Now do the work.");

close(fd);

waitKey(0);

 return 0;
} 
berak
  • 39,159
  • 9
  • 91
  • 89

1 Answers1

0

Here's a shot at trying to guide you towards the right direction. Comments in line.

Sources: http://en.wikibooks.org/wiki/Serial_Programming/Serial_Linux#Serial_I.2FO_via_Terminal_I.2FO Linux C Serial Port Reading/Writing

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h> 

int fd;
char *buff;
struct termios tty, tty_old;

int open_port(void)
{
    fd = open("/dev/ttyACM0", O_RDWR | O_NOCTTY);
    if (fd == -1)
    {
        perror("open_port: Unable to open /dev/kittens ");
    }
    else
    fcntl(fd, F_SETFL, 0);

    return 0;
}

int setup_port(void)
{
    memset (&tty, 0, sizeof tty);
    if ( tcgetattr ( fd, &tty ) != 0 )
    {
        perror("setup_port: Unable to GET attributes of /dev/kittens ");
    }
    /* Save old tty parameters */
    tty_old = tty;

    /* Set Baud Rate */
    cfsetospeed (&tty, (speed_t)B115200);
    cfsetispeed (&tty, (speed_t)B115200);

    /* Set other params */
    tty.c_cflag     &=  ~PARENB;        // Make 8n1
    tty.c_cflag     &=  ~CSTOPB;
    tty.c_cflag     &=  ~CSIZE;
    tty.c_cflag     |=  CS8;
    tty.c_cflag     &=  ~CRTSCTS;       // no flow control
    tty.c_cc[VMIN]      =   1;                  // read doesn't block
    tty.c_cc[VTIME]     =   5;                  // 0.5 seconds read timeout
    tty.c_cflag     |=  CREAD | CLOCAL;     // turn on READ & ignore ctrl lines

    /* Make raw */
    cfmakeraw(&tty);

    /* Flush Port, then applies attributes */
    tcflush( fd, TCIFLUSH );
    if ( tcsetattr ( fd, TCSANOW, &tty ) != 0)
    {
        perror("setup_port: Unable to SET attributes of /dev/kittens ");
    }
    return 0;
}

int close_port(void)
{
    close(fd);
    if ( tcsetattr ( fd, TCSANOW, &old_tty ) != 0)
    {
        perror("close_port: Failed to close /dev/kittens ");
    }
    return 0;
}

int main( int argc, char** argv )
{
    int const max_BINARY_value = 255; // what is this here for?
    char path[255]; //what is this here for?
    /* for printing to screen */
    char singleCharacter;
    ifstream inFile;
    /* for reading/writing serial port */
    int wr,rd;
    char msg[]="W";

    /* open & setup serial port */
    open_port();
    setup_port();

    /* other stuff */
    //capture image
    //gray 
    //Gaussian blur image
    //threshold
    //crop 
    system("tesseract crop.jpg txt -psm 10");
    //pass it to tesseract API and get the result in file

    /* writing to screen */
    inFile.open ("txt.txt", ofstream::in );//open file contains the letter
    inFile.get(singleCharacter);
    while (!inFile.eof())
    {
        cout << singleCharacter;
        inFile.get(singleCharacter);
    }

    //do // see below
    //{
    wr = write(fd, &msg, sizeof(msg));
    printf("debugging - Wrote to port\n");   
    //usleep(10000);
    if (wr < 0) {
        fputs("write() to port /dev/ttyACM0 failed!\n", stderr);
        break;
    }
    //while(buff != msg); // what is buff doing?

    /* just don't know why this is here */
    //if (buff==msg)
    //printf(buff, "String Found! Now do the work.");

    close_port();

    waitKey(0);

    return 0;
} 
Community
  • 1
  • 1
James
  • 71
  • 1
  • 5