简体   繁体   English

进入AVR微控制器仅读取两个字符

[英]gets in AVR microcontroller just reads two characters

I am doing a proyect that involves communication between a GSM module and a ATMEGA328P. 我正在做一个涉及GSM模块和ATMEGA328P之间通信的保护。 I try to emulate through a terminal the GSM module before actually trying with the AVR to check if my program works correctly, however whenever I input a string that is read by a "gets" function, the string is not properly captured. 在实际尝试使用AVR来检查程序是否正常运行之前,我尝试通过终端模拟GSM模块,但是,每当我输入由“ gets”功能读取的字符串时,就无法正确捕获该字符串。 I checked by trying to display it with a "printf" after reading it and it only displays the first two characters. 我通过尝试在读取后用“ printf”显示它来进行检查,它仅显示前两个字符。 Any idea why is this happening? 知道为什么会这样吗? I try to empty the buffer after typing the string with this: "while ((ch = getchar()) != '\\n'); to avoid problems since I have to read multiple values and whenever I type I append CR+LF as the GSM module does to answer. 我使用以下命令键入字符串后,尝试清空缓冲区:“ while((ch = getchar())!='\\ n');为避免出现问题,因为我必须读取多个值并且每当我键入时都附加CR + LF就像GSM模块所做的那样。

Here is the part of the code: 这是代码的一部分:

writeSerial("AT+CMGF=1");
clear();
readSerial(status);
writeSerial(status);

The functions listed above are declared as the following: 上面列出的函数声明如下:

void clear(void){
    char ch;    
    while ((ch = getchar()) != '\n');
}

void readSerial(char *arr){
    gets(arr,sizeof(arr));
}

void writeSerial(char *arr){
    printf("%s\r\n", arr); //CR+LF
}    

This is wrong: 这是错误的:

void readSerial(char *arr) {
  gets(arr,sizeof(arr));
}

On your AVR pointers are 16-bits wide so sizeof(arr) == 2 . 在AVR上,指针的宽度为16位,因此sizeof(arr) == 2 Instead of using sizeof , perhaps pass in the byte length of arr as an additional parameter to readSerial() . 代替使用sizeof ,也许传入arr的字节长度作为readSerial()的附加参数。

Consider rewriting your readSerial function as: 考虑将您的readSerial函数重写为:

void readSerial(char *arr, size_t array_size ){
    gets(arr, array_size);
}

Function should be called like this: 函数应这样调用:

char a[20] = {0};
readSerial( a, sizeof(a) );

In your case size of operator returns size of pointer to a char ( char * ) not the size of an array. 在您的情况下,运算符的大小返回指向char(char *)的指针的大小,而不是数组的大小。

sizeof(arr) == 2 because arr is a char* not an array. sizeof(arr) == 2,因为arr是一个char*而不是数组。

Arrays in C are not first class data types and cannot be passed to a function; 用C语言编写的数组不是一流的数据类型 ,不能传递给函数。 instead a pointer is passed and the array size information is lost. 而是传递了一个指针,并且数组大小信息丢失了。 You need to pass the length separately. 您需要分别传递长度。

void readSerial(char *arr, size_t len )
{
    gets( arr, len ) ;
}

Note that the standard library gets() does not take a size parameter in any case. 请注意,在任何情况下,标准库gets()都不会采用size参数。 I am assuming this is a non-standard implementation. 我假设这是一个非标准的实现。

As an aside, your clear() function can be simplified: 顺便说一句,您的clear()函数可以简化:

void clear(void)
{
    while( getchar() != '\n' ) ;
}

Though note that if the buffer is already empty, it will wait indefinitely for a new-line, so is probably not what you want in any case. 但是请注意,如果缓冲区已经为空,它将无限期地等待换行,因此无论如何都不是您想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM