Hello. I can't get it work
My application receives data from em-marin reader only being active. Switching to another app makes the port become silent.
MarinReader works in another thread.
Port reading is going while the app is inactive (tested).
Env: Ubuntu 9.04, Gnome, Qt (i've tryed pure open and fopen - the same result)
Please, help anybody!
bool MarinReader::read_port(QFile* file)
{
char first_char = '\0';
file->getChar(&first_char);
if (first_char == ':')
{
QByteArray bres = file->readAll();
QString s(bres);
s.resize(10);
emit CardProcessed(s);
}
tcflush(file->handle(), TCIOFLUSH);
return true;
}
void MarinReader::run()
{
emit Message("opening port " + port_name);
struct termios Posix_CommConfig;
QFile* Posix_File = new QFile(port_name);
if (!Posix_File->open(QIODevice::ReadOnly))
{
emit Error("open file error"); exit(-1); return;
}
/* doesn't solve the problem
struct flock fl;
fl.l_type = F_RDLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 0;
fl.l_len = 0;
fl.l_pid = getpid();
fcntl(Posix_File->handle(), F_SETLK, &fl);
*/
tcgetattr(Posix_File->handle(), &Posix_CommConfig);
Posix_CommConfig.c_cflag |= CREAD|CLOCAL;
Posix_CommConfig.c_lflag &= (~(ICANON|ECHO|ECHOE|ECHOK|ECHONL|ISIG));
Posix_CommConfig.c_iflag &= (~(INPCK|IGNPAR|PARMRK|ISTRIP|ICRNL|IXANY));
Posix_CommConfig.c_oflag &= (~OPOST);
Posix_CommConfig.c_cc[VMIN] = 0;
Posix_CommConfig.c_cc[VINTR] = _POSIX_VDISABLE;
Posix_CommConfig.c_cc[VQUIT] = _POSIX_VDISABLE;
Posix_CommConfig.c_cc[VSTART] = _POSIX_VDISABLE;
Posix_CommConfig.c_cc[VSTOP] = _POSIX_VDISABLE;
Posix_CommConfig.c_cc[VSUSP] = _POSIX_VDISABLE;
//speed
cfsetispeed(&Posix_CommConfig, B9600);
cfsetospeed(&Posix_CommConfig, B9600);
//data bits
Posix_CommConfig.c_cflag&=(~CSIZE);
Posix_CommConfig.c_cflag|=CS8;
//parity
Posix_CommConfig.c_cflag&=(~PARENB);
//stop bits
Posix_CommConfig.c_cflag&=(~CSTOPB);
//flow control
Posix_CommConfig.c_cflag&=(~CRTSCTS);
Posix_CommConfig.c_iflag&=(~(IXON|IXOFF|IXANY));
//timeout
Posix_CommConfig.c_cc[VTIME] = 1;
if (tcsetattr(Posix_File->handle(), TCSAFLUSH, &Posix_CommConfig) == -1)
{
emit Error("tcsetattr error"); exit(-1); return;
}
is_open = true;
emit Message("port opened successfully");
do
{
if (!read_port(Posix_File)) break;
}
while (!needClose);
needClose = false;
//fl.l_type = F_UNLCK;
//fcntl(Posix_File->handle(), F_SETLK, &fl);
Posix_File->close();
delete Posix_File;
emit Message("port closed");
is_open = false;
}