我正在使用 OpenCv 和 tesseract 编写用于字母识别的 c++ 代码,然后我想向串行端口(Arduino)发送命令,但计算机出现故障,我必须重新启动它。
我在代码中做了一些图像处理:转换为灰色
然后模糊图像
然后是阈值和裁剪
最后,我将图像传递给 Tesseract API 以获取字母并将字母保存在文本文件中。
我打开保存的文件并打印文件的内容。
然后我打开与arduino连接的串口并向串口发送一封信。
当我编译代码时,图像处理部分完成,打开文件并打印文件内容,并将其发送到串行端口,但随后代码崩溃并且计算机出现故障。
这是代码:
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;
}
最佳答案
这是一个尝试引导您朝着正确方向前进的镜头。评论在行。
资料来源:
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;
}
关于使用arduino发送到串口时ubuntu崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23539754/