Before adding O_NOCTTY
option, my process was killed once by unknown per every booting. I don't know why the process was killed :( and I thought the initializing has some problems. so that I added O_NOCTTY
option, and the process was not killed. But it cannot read any data from the buffer until restarted by other process. Please help me :(
The following is code about initializing and reading.
void Init() {
mFd = open("/dev/ttyS2", O_RDWR | O_NOCTTY);
if (mFd > 0)
{
(void)tcgetattr(mFd, &mTermios_p);
speed = B115200;
mTermios_p.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
mTermios_p.c_oflag &= ~OPOST;
mTermios_p.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
mTermios_p.c_cflag &= ~(CSIZE | PARENB);
mTermios_p.c_cflag |= CS8;
mTermios_p.c_cc[VMIN] = 0U;
mTermios_p.c_cc[VTIME] = 0U;
(void)cfsetispeed(&mTermios_p, speed);
(void)cfsetospeed(&mTermios_p, speed);
(void)tcflush(mFd, TCIOFLUSH);
(void)tcsetattr(mFd, TCSANOW, &mTermios_p);
}
else
{
LOGE("uart open failed %s", strerror(errno));
}
}
int32_t Read() {
int32_t bytes = -1;
if (mFd > 0)
{
(void)pthread_mutex_lock(&mMutexLock);
bytes = static_cast<int32_t>(read(mFd, buf, nMaxRead));
(void)pthread_mutex_unlock(&mMutexLock);
}
if (bytes < 0)
{
LOGE("read failed");
}
return bytes;
}