Hi,
Can someone help me with a problem I'm having reading from the
serial port.
I've set the port up so that anyone can read and write to it. The code
is fine initializing it i.e. no errors etc. I've also been successful
writing to it. When I was to read stuff from it things start going
wacky.
What I want to do is read 25 bytes from the port, into the array and
return. What end up happening is either nothing gets read or random
amounts of data. This is the code I use to read from the port:
...
nBytesToRead = 25;
bResult = 0;
bResult = read(FileHandle, SensorBuffer, nBytesToRead);
if (bResult == -1) {
}
cout << "Actual Char's Read : " << bResult << endl;
for(int Counter = 0; Counter <= 25; Counter++) {
cout << SensorBuffer[Counter] << endl;
}
...
My set up code is:
FileHandle = open("/dev/ttyS1",O_RDWR | O_NOCTTY | O_NDELAY);
if (FileHandle == -1)
cout << "An Error occured while opening Serial Port" << endl;
else
cout << "Serial Port opened succesfully" << endl;
/* Get Current Port Configuration */
tcgetattr(FileHandle, &options);
/* Set Baud Rate */
cfsetispeed(&options,B38400);
cfsetospeed(&options,B38400);
options.c_cflag |= (CLOCAL | CREAD);
/* No Parity, 8 Bits */
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
options.c_oflag &= OPOST;
/* RAW input */
options.c_lflag &= ~(ICANON | ECHO | ISIG);
options.c_cc[VMIN] = 25;
options.c_cc[VTIME] = 0;
/* Flush buffers */
tcflush(FileHandle, TCIFLUSH);
/* Update Port with new Configuration */
tcsetattr(FileHandle, TCSANOW, &options);
If someone code help me out I'd be most grateful.
Thanks
Mark