Hi,
I have a bit of a problem reading a device from the usb port that sends data every second. I do receive data but i'm confused with one little thing:
The data length is usually 25 bytes (the nr between ::XX:

but SOMETIMES it gets broken into parts like this:
Code:
::25::7E 42 08 00 00 00 00 00 7D 5E 00 01 7D 5D 01 00 92 0B 00 00 00 00 47 7C 7E
::25::7E 42 08 00 00 00 00 00 7D 5E 00 01 7D 5D 01 00 92 0B 00 00 00 00 47 7C 7E
::25::7E 42 08 00 00 00 00 00 7D 5E 00 01 7D 5D 01 00 8F 0B 00 00 00 00 80 28 7E
::25::7E 42 08 00 00 00 00 00 7D 5E 00 01 7D 5D 01 00 92 0B 00 00 00 00 47 7C 7E
::25::7E 42 08 00 00 00 00 00 7D 5E 00 01 7D 5D 01 00 92 0B 00 00 00 00 47 7C 7E
::25::7E 42 08 00 00 00 00 00 7D 5E 00 01 7D 5D 01 00 93 0B 00 00 00 00 E7 39 7E
::4::7E 42 08 00
::21::00 00 00 00 7D 5E 00 01 7D 5D 01 00 92 0B 00 00 00 00 47 7C 7E
::25::7E 42 08 00 00 00 00 00 7D 5E 00 01 7D 5D 01 00 92 0B 00 00 00 00 47 7C 7E
::7::7E 42 08 00 00 00 00
::18::00 7D 5E 00 01 7D 5D 01 00 92 0B 00 00 00 00 47 7C 7E
::25::7E 42 08 00 00 00 00 00 7D 5E 00 01 7D 5D 01 00 92 0B 00 00 00 00 47 7C 7E
::25::7E 42 08 00 00 00 00 00 7D 5E 00 01 7D 5D 01 00 8F 0B 00 00 00 00 80 28 7E
::25::7E 42 08 00 00 00 00 00 7D 5E 00 01 7D 5D 01 00 92 0B 00 00 00 00 47 7C 7E
I'm not very good in serial communication. I've been reading and trying some example codes resulting to this. The settings are as following
Code:
//Get the current options for the port...
tcgetattr(fd, &options);
//Set the baud rates for input and output (same))
cfsetispeed(&options, type);
cfsetospeed(&options, type);
//Reset control flags
;
//Enable the receiver and set local mode.
options.c_cflag |= (CLOCAL | CREAD);
//8 bit char mode, No parity, 1 stop bit
options.c_cflag |= (options.c_cflag & ~CSIZE) | CS8;
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
//Minimum caracters to read
options.c_cc[VMIN] = MIN_CHAR_READ; // MIN_CHAR_READ = 0
//Time to wait for data
options.c_cc[VTIME] = WAIT_DATA; //WAIT_DATA = 20 (2 s)
//Set hardware handshake
options.c_cflag &= ~CRTSCTS;
//No software flow control
options.c_iflag &= ~(IXON | IXOFF | IXANY);
//Input character management (raw or canonical)
options.c_lflag = IRAW_MODE;
//options.c_lflag = ICANON_MODE;
//Output filtering options (raw or processed)
options.c_oflag = ORAW_MODE;
//options.c_oflag = OPROCESS_MODE;
//Set the new options for the port...
tcsetattr(fd, TCSANOW, &options);
and then I just read the buffer like this:
Code:
//Empty the recieved buffer
tcflush(fd, TCIFLUSH);
nBytes = read(fd, &data[0], MAX_DATA_SIZE) //(MAX_DATA_SIZE = 255)
Is it because the serial buffer is full for some reason?? If so, is there anyway I can determine the the tranmitted data is only a part of it and not complete so that I can read data again till it's complete?
Thanks a million
Indy