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?
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, otherwise printf would be printing garbage values in the buffer.
Comments:
Do not worry about lines, the read should return a \n character in the buffer which printf 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.
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;
Probably you have to implement that by yourself. Just keep reading character by character until you reached '\n', and return what you have read.
I didn't check, but I suspect that's how 'readline' is usually implemented.
Related
I am writing an application to read serial data, one byte at a time, then parse this segment upon finding a '\n' character in this receive buffer. Everything seems to work until I attempt to printf the resulting character array (which I null-terminate after upon finding \n or the max of the char array.) I don't know why this is happening.
I have tried:
void check_uart(void)
{
char ch;
bytesRecv = read(serial_port, &ch, 1); // Reads character fine.
printf("bytes received: %d\t content: %c\n",bytesRecv,ch); // Still good here.
if (bytesRecv)
{
if (ch != '\n') // Check for newline from the host, still good here.
{
rbuf[rbuf_index] = ch; // If NOT newline char, add char to buffer.
if (rbuf_index++ < (sizeof(rbuf) - 1))
return;
}
// Process the command when user hits return OR the buffer is full.
rbuf[rbuf_index] = '\0'; // Add null terminator to buffer, use as string
printf("-----------------------\n"); // Prints the line
printf("%s\n",rbuf); // **NOTHING**
printf("-----------------------\n"); // Prints the line
rbuf_index = 0; // Reset rbuf's index now
}
and, a la the Serial Programming Guide:
void check_uart(void)
{
char ch;
bytesRecv = read(serial_port, &ch, 1); // Reads character fine.
printf("bytes received: %d\t content: %c\n",bytesRecv,ch); // Still good here.
if (bytesRecv)
{
bufptr += bytesRecv;
if (ch != '\n') // Check for newline from the host, still good here.
{
rbuf[rbuf_index] = ch; // If NOT newline char, add char to buffer.
if (rbuf_index++ < (sizeof(rbuf) - 1))
return;
}
// Process the command when user hits return OR the buffer is full.
*bufptr='\0';
printf("---------------------\n"); // Prints the line
printf("%s\n",rbuf); // **NOTHING**
printf("---------------------\n"); // Prints the line
rbuf_index = 0; // Reset rbuf's index now
}
General declarations and program structure, with these globals declared prior:
char rbuf[255]; // Receive buffer
char tbuf[255]; // Transmit buffer
char *bufptr; // Receive buffer pointer
uint8_t rbuf_index=0; // Receive buffer index
int serial_port; // fd
int main(void)
{
serial_port = open(SERIAL_PRIMARY, O_RDWR | O_NOCTTY | O_NDELAY);
struct termios tty;
memset(&tty, 0, sizeof(tty));
while(TRUE)
{
usleep(1000);
memset(&rbuf, '\0' sizeof(rbuf));
bufptr=rbuf;
check_uart();
}
...
}
I feel like I'm missing something very basic here. The program loops great, seems to run indefinitely and receives data successfully (accurately) from the host it reads. I just can't do anything with the data at this point. I know there is quite a lot to acquaint with when it comes to configuring a serial application, as outlined in the Serial Programming Guide, but my hunch is that it's more something in my character array syntax or overall approach there.
Thanks
So, I'm working on a simple hangman game in C, and I have the function read_guess, shown below.
void read_guess(char *guesses, char *p_current_guess)
{
int valid_guess = 0;
// Repeatedly takes input until guess is valid
while (valid_guess == 0)
{
printf(">>> ");
fgets(p_current_guess, 2, stdin);
if (!isalpha(*p_current_guess)) printf("Guesses must be alphabetic. Please try again.\n\n");
else
{
valid_guess = 1;
// Iterates over array of guesses and checks if letter has already been guessed
for (int i = 0; guesses[i] != '\0'; i++)
{
if (guesses[i] == *p_current_guess)
{
printf("You have already guessed this letter. Please try again.\n\n");
valid_guess = 0;
break;
}
}
}
}
}
I've tried all the standard input functions (including getchar), but with all of them, when an input larger than one character is supplied, instead of taking just the first character and moving on (or asking again), the rest of the input is "pushed back", and the next time input is requested, whether it be because the input contained a non-alphabetic character or the next round begins, the rest of the input is automatically processed. This repeats for each character of the input.
How can I avoid this?
You are using fgets which is good, but unfortunately not the right way...
fgets reads up to an end of line or at most 1 less the the number of character asked. And of course remaining characters are left for the next read operation...
The idiomatic way would be to ensure reading up to the end of line, whatever the length, or at least up to a much larger length.
Simple but could fail in more than SIZE characters on input:
#define SIZE 64
...
void read_guess(char *guesses, char *p_current_guess)
{
char line[SIZE];
int valid_guess = 0;
// Repeatedly takes input until guess is valid
while (valid_guess == 0)
{
printf(">>> ");
fgets(line, SiZE, stdin); // read a line of size at most SIZE-1
p_current_guess[0] = line[0]; // keep first character
p_current_guess[1] = '\0';
...
Robust but slightly more complex
/**
* Read a line and only keep the first character
*
* Syntax: char * fgetfirst(dest, fd);
*
* Parameters:
* dest: points to a buffer of size at least 2 that will recieve the
* first character followed with a null
* fd : FILE* from which to read
*
* Return value: dest if one character was successfully read, else NULL
*/
char *readfirst(dest, fd) {
#define SIZE 256 // may be adapted
char buf[SIZE];
char *cr = NULL; // return value initialized to NULL if nothing can be read
for (;;) {
if(NULL == fgets(buff, sizeof(buff), fd)) return cr; // read error or end of file
if (0 == strcspn(buff, "\n")) return cr; // end of file
if (cr == NULL) { // first read:
cr = dest; // prepare to return first char
dest[0] = buff[0];
dest[1] = 0;
}
}
}
You can then use it simply in your code:
void read_guess(char *guesses, char *p_current_guess)
{
int valid_guess = 0;
// Repeatedly takes input until guess is valid
while (valid_guess == 0)
{
printf(">>> ");
fgetfirst(p_current_guess, stdin);
You can discard all input until end-of-line, each time you want to ask for input.
void skip_to_eol(FILE* f, int c)
{
while (c != EOF && c != '\n')
c = fgetc(f);
}
...
char c = getchar(); // instead of fgets
skip_to_eol(stdin, c);
You can use getch() function on windows to get single character. and this is linux equivalent
What is the equivalent to getch() & getche() in Linux?
I was trying to take a full line input in C. Initially I did,
char line[100] // assume no line is longer than 100 letters.
scanf("%s", line);
Ignoring security flaws and buffer overflows, I knew this could never take more than a word input. I modified it again,
scanf("[^\n]", line);
This, of course, couldn't take more than a line of input. The following code, however was running into infinite loop,
while(fscanf(stdin, "%[^\n]", line) != EOF)
{
printf("%s\n", line);
}
This was because, the \n was never consumed, and would repeatedly stop at the same point and had the same value in line. So I rewrote the code as,
while(fscanf(stdin, "%[^\n]\n", line) != EOF)
{
printf("%s\n", line);
}
This code worked impeccably(or so I thought), for input from a file. But for input from stdin, this produced cryptic, weird, inarticulate behavior. Only after second line was input, the first line would print. I'm unable to understand what is really happening.
All I am doing is this. Note down the string until you encounter a \n, store it in line and then consume the \n from the input buffer. Now print this line and get ready for next line from the input. Or am I being misled?
At the time of posting this question however, I found a better alternative,
while(fscanf(stdin, "%[^\n]%*c", line) != EOF)
{
printf("%s\n", line);
}
This works flawlessly for all cases. But my question still remains. How come this code,
while(fscanf(stdin, "%[^\n]\n", line) != EOF)
{
printf("%s\n", line);
}
worked for inputs from file, but is causing issues for input from standard input?
Use fgets(). #FredK
char buf[N];
while (fgets(buf, sizeof buf, stdin)) {
// crop potential \n if desired.
buf[strcspn(buf, "\n")] = '\0';
...
}
There are to many issues trying to use scanf() for user input that render it prone to mis-use or code attacks.
// Leaves trailing \n in stdin
scanf("%[^\n]", line)
// Does nothing if line begins with \n. \n remains in stdin
// As return value not checked, use of line may be UB.
// If some text read, consumes \n and then all following whitespace: ' ' \n \t etc.
// Then does not return until a non-white-space is entered.
// As stdin is usually buffered, this implies 2 lines of user input.
// Fails to limit input.
scanf("%[^\n]\n", line)
// Does nothing if line begins with \n. \n remains in stdin
// Consumes 1 char after `line`, even if next character is not a \n
scanf("%99[^\n]%*c", line)
Check against EOF is usual the wrong check. #Weather Vane The following, when \n is first entered, returns 0 as line is not populated. As 0 != EOF, code goes on to use an uninitialized line leading to UB.
while(fscanf(stdin, "%[^\n]%*c", line) != EOF)
Consider entering "1234\n" to the following. Likely infinite loop as first fscanf() read "123", tosses the "4" and the next fscanf() call gets stuck on \n.
while(fscanf(stdin, "%3[^\n]%*c", line) != EOF)
When checking the results of *scanf(), check against what you want, not against one of the values you do not want. (But even the following has other troubles)
while(fscanf(stdin, "%[^\n]%*c", line) == 1)
About the closest scanf() to read a line:
char buf[100];
buf[0] = 0;
int cnt = scanf("%99[^\n]", buf);
if (cnt == EOF) Handle_EndOfFile();
// Consume \n if next stdin char is a \n
scanf("%*1[\n]");
// Use buf;
while(fscanf(stdin, "%[^\n]%*c", line) != EOF)
worked for inputs from file, but is causing issues for input from standard input?
Posting sample code and input/data file would be useful. With modest amount of code posted, some potential reasons.
line overrun is UB
Input begins with \n leading to UB
File or stdin not both opened in same mode. \r not translated in one.
Note: The following fails when a line is 100 characters. So meeting the assumption cal still lead to UB.
char line[100] // assume no line is longer than 100 letters.
scanf("%s", line);
Personally, I think fgets() is badly designed. When I read a line, I want to read it in whole regardless of its length (except filling up all RAM). fgets() can't do that in one go. If there is a long line, you have to manually run it multiple times until it reaches the newline. The glibc-specific getline() is more convenient in this regard. Here is a function that mimics GNU's getline():
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
long my_getline(char **buf, long *m_buf, FILE *fp)
{
long tot = 0, max = 0;
char *p;
if (*m_buf == 0) { // empty buffer; allocate
*m_buf = 16; // initial size; could be larger
*buf = (char*)malloc(*m_buf); // FIXME: check NULL
}
for (p = *buf, max = *m_buf;;) {
long l, old_m;
if (fgets(p, max, fp) == NULL)
return tot? tot : EOF; // reach end-of-file
for (l = 0; l < max; ++l)
if (p[l] == '\n') break;
if (l < max) { // a complete line
tot += l, p[l] = 0;
break;
}
old_m = *m_buf;
*m_buf <<= 1; // incomplete line; double the buffer
*buf = (char*)realloc(*buf, *m_buf); // check NULL
max = (*m_buf) - old_m;
p = (*buf) + old_m - 1; // point to the end of partial line
}
return tot;
}
int main(int argc, char *argv[])
{
long l, m_buf = 0;
char *buf = 0;
while ((l = my_getline(&buf, &m_buf, stdin)) != EOF)
puts(buf);
free(buf);
return 0;
}
I usually use my own readline() function. I wrote this my_getline() a moment ago. It has not been thoroughly tested. Please use with caution.
I try to use read() to get some characters from file just for learning this API. I have create a file called "file" in the same directory and it is content:
1:2:ab:cd:ef
Here is the code:
#include <stdio.h>
#include <error.h>
int read_indent(int sockfd){
int sport, cport;
char user[3], rtype[3], addinfo[3];
char buffer[4+4+3+3+3+1];
if(read(sockfd, buffer, sizeof(buffer)) <= 0) {
perror("read: %m");
return -1;
}
buffer[sizeof(buffer)-1] = '\0';
sscanf(buffer, "%d:%d:%s:%s:%s", &sport, &cport, rtype, user, addinfo);
printf("%d:%d:%s:%s:%s", sport, cport, rtype, user, addinfo);
return 0;
}
int main(){
FILE *file_pt = fopen("file", "r");
if(file_pt == NULL) { printf("fopen error\n"); return -1;}
char buf[128];
int a = read_indent(fileno(file_pt));
fclose(file_pt);
return 0;
}
My printf returns me
1:2:ab:cd:ef::xPvx
where x is some garbage character I cannot recognize. What is the reason for this? int is 4 bytes in my system.
One issue is that you didn't specify a width for the %s parameters. This means that it matches up until the first whitespace character. There are no whitespace characters in your string, so the first %s matches until the end, leaving only garbage data after your string to fill the other variables.
Try this:
sscanf(buffer, "%d:%d:%2s:%2s:%2s", &sport, &cport, rtype, user, addinfo);
The other issue is that you don't null-terminate your buffer properly, read returns the number of characters read - add a null after that.
char buffer[4+4+3+3+3+1];
The buffer is bigger than what you plan to read and that's ok, but:
buffer[sizeof(buffer)-1] = '\0';
This is wrong, add the \0 at size+1 , where size is what you get back with read(), the actual number of bytes read.
See here:
The value returned may be less than nbyte if the number of bytes left in the file is less than nbyte, if the read() request was interrupted by a signal, or if the file is a pipe or FIFO or special file and has fewer than nbyte bytes immediately available for reading.
char buffer[4+4+3+3+3+1];
if(read(sockfd, buffer, sizeof(buffer)) <= 0) {
//....
}
buffer[sizeof(buffer)-1] = '\0';
The read function does not add \0 to the buffer after reading. But you read just 12 bytes, and your buffer size is 18. So you still have 5 bytes of garbage in your buffer. This gets added to the last string you read.
What would be the best way to imitate the functionality of gets with scanf?
Here is my current attempt
int main()
{
char cvalue[20]; //char array to store input string
int iloop=0; //integer variable for loop
for(iloop=0;iloop<20;iloop++) // for loop to get the string char by char
{
scanf("%c",&cvalue[iloop]); //getting input
if(cvalue[iloop]=='\n') //if input is newline skip further looping
break;
} // end of loop
cvalue[iloop]='\0'; //set end of the character for given input
printf("%s",cvalue); //printing the given string
return 0;
}
You could use scanf this way to work like gets
scanf("%[^\n]",&a);
You need to observe the usually dangers of gets().
The challenge to using scanf() is
1) Insuring that \n is consumed. scanf("%[^\n]",... does not do this.
2) Insuring the str gets a \0 if only a \n is read.
3) Dealing with EOF and I/O errors and return 0.
4) Insure leading whitespace are read into str as scanf("%s" skips them.
#include <stdio.h>
// On success, the gets() returns str.
// If EOF encountered, the eof indicator is set (feof).
// If this happens before any characters could be read,
// pointer returned is a null pointer.
// If a read error occurs, the error (ferror) is set
// and a null pointer is also returned.
char *gets_via_scanf( char * str ) {
// Reads characters from stdin & saves them into str until \n or the end-of-file.
// \n, if found, is not copied into str.
int retval = scanf("%[^\n]",str); // %[ does not skip leading whitespace
if (retval == EOF) return 0;
if (retval == 0) {
*str = '\0'; // Happens when users only types in \n
}
char ch;
scanf("%c",&ch); // Consume leftover \n, could be done with getc()
return str;
}
Your attempt doesn't really imitate gets(), since gets() just keeps putting bytes into the supplied buffer until the end of line is reached. You should realize then that gets() is dangerous and should be avoided. It does not offer any protection from buffer overflow. So, it is also questionable to imitate it.
Given that, your attempt has a couple flaws that I see. First, it loops to the complete size of the input buffer. This doesn't leave you any room to store the NUL terminator if the input line is 20 bytes or longer. This means that you may attempt to store the \0 at cvalue[20], which outside the array boundary. You can fix this by shortening your for loop by one:
for(iloop=0;iloop<19;iloop++) // for loop to get the string char by char
The second flaw is that you do not check to see if the scanf() call succeeds. If you detect failure, you should also leave the loop:
if (scanf("%c",&cvalue[iloop]) != 1) { //getting input
break;
}
Below was my attempt at creating a safer version of gets() implemented with scanf().
char *getsn (char *s, size_t sz) {
char c;
char fmt[sizeof(sz) * CHAR_BIT + sizeof("[^\n]")];
if (sz == 0) return 0;
if (sz == 1) {
s[0] = '\0';
return s;
}
s[sz-2] = '\0';
snprintf(fmt, sizeof(fmt), "%%%lu%s", (unsigned long)sz-1, "[^\n]");
switch (scanf(fmt, s)) {
case 0: s[0] = '\0';
scanf("%c", &c);
return s;
case 1: scanf("%c", &c);
if (s[sz-2] != '\0' && c != '\n') {
ungetc(c, stdin);
}
return s;
default: break;
}
return 0;
}
The safer version uses snprintf() to create a format string that limits how many characters should be stored by the scanf(). So if the provided sz parameter was 100, the resulting format string would be "%99[^\n]". Then, it makes sure to only strip out the \n from the input stream if it was actually encountered.