简体   繁体   English

通过用户输入读取和使用字符串? C#(Arduino Uno)

[英]Reading and using a string of characters through user input? C# (Arduino Uno)

I'm sure the answer is very simple, but I haven't found it anyway. 我敢肯定答案很简单,但是我还是没有找到答案。 Actually, this bit of code almost does what I want it to do... It waits for input, then once the user has entered something, it checks whether or not the first letter begins with a vowel. 实际上,这段代码几乎可以完成我想要的工作……它等待输入,然后在用户输入内容后,它检查第一个字母是否以元音开头。

For yes and no different messages are displayed. 如果是,则不会显示任何其他消息。 It will do this fine... but if I add Serial.println(input) to check what was actually read, I get the first letter only. 它将做得很好...但是如果我添加Serial.println(input)来检查实际读取的内容,则只会得到第一个字母。 It seems as though it's only actually saving the first character into the first memory slot, and nothing else. 似乎实际上只是将第一个字符保存到第一个内存插槽中,而没有其他任何事情。

I want to keep the whole word, be able to print it out and use it later. 我想保留整个单词,可以打印出来并在以后使用。 Serial.println(input[0]) prints the same thing as the previously mentioned statement, and Serial.println(input[1]) (or anything where 1 is higher) does not print anything, so I'm assuming that the issue is in reading the characters in the first place. Serial.println(input[0])打印与前面提到的语句相同的东西,而Serial.println(input[1]) (或Serial.println(input[1])任何东西)不打印任何东西,所以我假设问题出在首先是阅读字符。

Serial.println("Enter a word: ");
while (!Serial.available()) {
    ;
}
char input[100] = {Serial.read()};
if (input[0] == 'a' || input[0] == 'e' || input[0] == 'i' || input[0] == 'o' || input[0] == 'u'){
    Serial.println("the word begins with a vowel\n");
}
else{
    Serial.println("the word does not begin with a vowel");
}

according to Arduino::Serial.Read() documentation: 根据Arduino :: Serial.Read()文档:

Returns 退货

the first byte of incoming serial data available (or -1 if no data is available) - int 可用的传入串行数据的第一个字节(如果没有可用数据,则为-1)-int

So it appears to me, you simply use the wrong function here. 在我看来,您在这里只是使用了错误的功能。

You probably need to use Arduino::Serial.ReadBytes() 您可能需要使用Arduino :: Serial.ReadBytes()

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

相关问题 C#通过serialPort将大文件流式传输到Arduino UNO - C# streaming large file through serialPort to an Arduino UNO 通过ac#服务器使用Android应用程序控制arduino UNO - control arduino UNO with android app through a c# server C#:使用数组将字符串(或用户输入)拆分为单个字符? - C#: Split a string (or user input) into individual characters withOUT using an array? 使用C#使用Start和Stop字符读取String作为SerialPort的缓冲区 - Reading String as buffer for SerialPort using Start and Stop characters with C# 使用 Arduino 输入触发 C# 函数 - Triggering a C# function using Arduino input 使用Arduino Uno从winfroms C#应用程序控制两个伺服器 - Control two servos from winfroms C# application with Arduino Uno 如何使用USB端口将数字从C#发送到Arduino Uno? - How to send numbers from C# to Arduino Uno with a usb port? 有没有办法让我在 Arduino Uno 上实现我的 c# 代码? - Is there a way for me to implement my c# code on an Arduino Uno? 我如何使用 c# 在 windows iot 中使用 USB 电缆作为串行端口将 arduino uno 连接到 rasberry pi 3 - how can i connect arduino uno to rasberry pi 3 using usb cable as serial port in windows iot using c# Arduino通过C#通过UDP脚本统一通信 - Arduino Communication with unity using C# through UDP scripting
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM