read from serial port
Solution 1
The while loop does not necessarily read character by character, but it might return you one character at a time for each read based on the serial port device and the rate of transmission.
I've made some changes to fix few bugs in your while loop:
while(1)
{
char buffer[100];
ssize_t length = read(fd, &buffer, sizeof(buffer));
if (length == -1)
{
printf("Error reading from serial port\n");
break;
}
else if (length == 0)
{
printf("No more data\n");
break;
}
else
{
buffer[length] = '\0'
printf("%s", buffer);
}
}
List of changes:
- Check the return value from
read
- I'm assuming when read fails or returns 0, it means no more data to read and breaks the while loop execution. Modify this behavior as per your needs.
- Append a
'\0'
character before printing, otherwiseprintf
would be printing garbage values in the buffer.
Comments:
- Do not worry about lines, the read should return a
\n
character in the buffer whichprintf
would interpret as a newline when you print it out. - If you're actually only interested in grabbing a line and storing it somewhere, you need to read and append to another buffer until you get a
\n
in the buffer, and also need to handle multiple\n
within the same buffer implying multiple lines.
Solution 2
You can't guarantee that a single call to read
will give you exactly one line of text. You need to buffer the data. The easiest way to do this is to read exactly one character at a time, and stop when you reach a newline character. If you want to read as many characters as possible each time, the buffering code becomes more complicated.
Try this to start:
char buffer[100] = {0};
int pos = 0;
while( pos < 99 ) {
read(fd, buffer+pos, 1); // Note you should be checking the result
if( buffer[pos] == '\n' ) break;
pos++;
}
// Normally you would null-terminate string, but noticed I initialised the
// buffer to all zeroes at the beginning. So this is not strictly necessary.
// However, in this case it will remove the newline character.
buffer[pos] = 0;
Cacheing
Updated on June 04, 2022Comments
-
Cacheing almost 2 years
This is the code given by http://www.gravitech.us/7segmentshield.html.
void SerialMonitorPrint (byte Temperature_H, int Decimal, bool IsPositive) { Serial.print("The temperature is "); if (!IsPositive) { Serial.print("-"); } Serial.print(Temperature_H, DEC); Serial.print("."); Serial.print(Decimal, DEC); Serial.print(" degree C"); Serial.print("\n\n"); }
But when I try to read data from serial port, I found that I read data character by character.
UPDATE
while(1) { char buffer[100]; int chars_read = read(fd, &buffer, sizeof(buffer)); buffer[chars_read] = '\0'; printf("%s", buffer); }
So how can I read line by line?
-
skinnedKnuckles about 5 yearsReading one byte at a time can create a bottleneck depending on your data throughput requirements. Most implementations of the serial port have a termination character property. This property is usually set to the linefeed character (ascii "\n", hex 0xA, dec 10 or Oct 012) but can be assigned any valid char value. The termination character is used by a function often called something like ReadLine() which returns all available bytes up to the next termination character and is much faster than reading one character at a time.