Pico W 读取串行输入

huangapple go评论47阅读模式
英文:

Pico W Read Serial Input

问题

I'm trying to read the input sent through the serial monitor but scanf does nothing and i can't figure out how to get getchar to do the same thing.

Here's a piece of code that I've tried to use that does not work on the pico. It does work on my pc.

char *readInput() {
    int i = 0;
    char ch;
    char *str = malloc(sizeof(char) * 1024);

    while((ch = getchar()) != '\n' && ch != EOF) {
        if (i < 1024) {
            str[i++] = ch;
            printf("%c\n", ch); // For debugging. Returns the correct character.
        }
    }
    printf("%s\n", str); // For debugging. Returns nothing.
    str[i] = '
char *readInput() {
    int i = 0;
    char ch;
    char *str = malloc(sizeof(char) * 1024);

    while((ch = getchar()) != '\n' && ch != EOF) {
        if (i < 1024) {
            str[i++] = ch;
            printf("%c\n", ch); // For debugging. Returns the correct character.
        }
    }
    printf("%s\n", str); // For debugging. Returns nothing.
    str[i] = '\0';

    return str;
}
'
;
return str; }

It looks like it just tries to read until it gets to the 1024th character.

Also I've read somewhere that scanf doesn't work on the Pico but some people say it works so i don't even know anymore.

英文:

I'm trying to read the input sent through the serial monitor but scanf does nothing and i can't figure out how to get getchar to do the same thing.

Here's a piece of code that I've tried to use that does not work on the pico. It does work on my pc.

char *readInput() {
    int i = 0;
    char ch;
    char *str = malloc(sizeof(char) * 1024);

    while((ch = getchar()) != &#39;\n&#39; &amp;&amp; ch != EOF) {
	    if (i &lt; 1024) {
		    str[i++] = ch;
		    printf(&quot;%c\n&quot;, ch); // For debugging. Returns the correct character.
	    }
    }
    printf(&quot;%s\n&quot;, str); // For debugging. Returns nothing.
    str[i] = &#39;
char *readInput() {
int i = 0;
char ch;
char *str = malloc(sizeof(char) * 1024);
while((ch = getchar()) != &#39;\n&#39; &amp;&amp; ch != EOF) {
if (i &lt; 1024) {
str[i++] = ch;
printf(&quot;%c\n&quot;, ch); // For debugging. Returns the correct character.
}
}
printf(&quot;%s\n&quot;, str); // For debugging. Returns nothing.
str[i] = &#39;\0&#39;;
return str;
}
&#39;; return str; }

It looks like it just tries to read until it gets to the 1024th character.

Also I've read somewhere that scanf doesn't work on the Pico but some people say it works so i don't even know anymore.

答案1

得分: 4

malloc()的结果被丢弃:

char *str = malloc(sizeof(char) * 1024);

malloc()返回一个NULL指针以指示失败,它可以并且确实这样做。其返回值应始终进行检查。如果它返回NULL,随后的操作将会对NULL指针进行解引用和写入。

另外:sizeof(char)被定义为1,所以您可以省略它。

char *str = malloc(1024);
/* 添加 */
if (!str) {
   /* malloc()无法分配内存。
    * 在这里处理错误。
    */
}

一位偏差错误:

if (i &lt; 1024)

没有留出空间来存放空字节。


getchar()返回一个int

char ch;

while((ch = getchar()) != '\n' && ch != EOF)

如果getchar()返回的整数值被存储在类型为char的变量中,然后与整数常量EOF进行比较,比较可能永远不会成功,因为将char类型的变量扩展为整数时,其符号扩展是实现定义的。


未定义行为:

printf("%s\n", str); // 用于调试。不返回任何内容。

%s格式说明符期望一个字符串。str不是一个字符串。调用printf()将引发未定义行为。

在调用printf()之前移动

str[i] = '
str[i] = '\0';
';

写入越界内存:

    str[i] = '
    str[i] = '\0';
';

while循环退出时,i的值为1024。您仅为1023个字符分配了内存。


小问题:

char *readInput()

表示readInput接受未指定数量和类型的参数。

char *readInput(void)

不接受。

英文:

malloc()'s result is discarded:

char *str = malloc(sizeof(char) * 1024);

malloc() returns a NULL pointer to indicate failure, which it can and does. Its return value should always be checked. If it returns NULL, subsequent operations would be dereferencing and writing to a NULL pointer.

Aside: sizeof (char) is defined to be 1, so you can leave that out.

char *str = malloc(1024);
/* Add */
if (!str) {
   /* malloc() failed to allocate memory.
    * Handle error here.
    */
}

Off-by-one error:

if (i &lt; 1024)

doesn't leave room for the null-byte.


getchar() returns an int:

char ch;

while((ch = getchar()) != &#39;\n&#39; &amp;&amp; ch != EOF)

> If the integer value returned by getchar() is stored into a
> variable of type char and then compared against the integer
> constant EOF, the comparison may never succeed, because sign-
> extension of a variable of type char on widening to integer is
> implementation-defined.


Undefined behaviour:

printf(&quot;%s\n&quot;, str); // For debugging. Returns nothing.

The %s format specifier expects a string. str is not a string. The call to printf() would invoke undefined behaviour.

Move

str[i] = &#39;
str[i] = &#39;\0&#39;;
&#39;;

before the call to printf().


Writing to out of bounds memory:

    str[i] = &#39;
    str[i] = &#39;\0&#39;;
&#39;;

The value of i is 1024 when the while loop exits. You only allocated memory for 1023 characters.


Minor:

char *readInput()

indicates that readInput takes an unspecified number and type of arguments.

char *readInput (void)

doesn't.

答案2

得分: 0

串行监视器我之前使用的有问题,它不能正确显示/发送数据。将其更改为Arduino的串行监视器解决了问题,现在我可以使用scanf了。

英文:

The serial monitor I was using was just broken and it didn't display/send things correctly. Changing it to arduino's one fixed the problem and I can use scanf now.

huangapple
  • 本文由 发表于 2023年2月18日 01:47:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75487648.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定