c - 如何使用这个 Arduino 演示串行读取更大的数据类型?
问题描述
我很难理解我的数据类型的大小与通过 Arduino mega2560 解析串行信息的关系。我正在尝试通过串行监视器输入大整数,例如整数 8000000,但正在返回负数或明显更小的数字。
下面的代码是我修改的一个demo。用户必须继续并结束小于和大于符号 (<'NUMBER>) 之间的串行输入:
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars]; // temporary array for use by strtok() function
// variables to hold the parsed data
char messageFromPC[numChars] = {0};
int integerFromPC = 0;
float floatFromPC = 0.0;
boolean newData = false;
//============
void setup() {
Serial.begin(9600);
// Serial.println("This demo expects 3 pieces of data - text, an integer and a floating point value");
// Serial.println("Enter data in this style <text,12,24.7> ");
Serial.println();
}
//============
void loop() {
recvWithStartEndMarkers();
if (newData == true) {
strcpy(tempChars, receivedChars);
// this temporary copy is necessary to protect the original data
// because strtok() replaces the commas with \0
parseData();
showParsedData();
newData = false;
}
}
//============
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
//============
void parseData() {
// split the data into its parts
char * strtokIndx; // this is used by strtok() as an index
// strtokIndx = strtok(tempChars,","); // get the first part - the string
// strcpy(messageFromPC, strtokIndx); // copy it to messageFromPC
strtokIndx = strtok(tempChars, ","); // this continues where the previous call left off
integerFromPC = atoi(strtokIndx); // convert this part to an integer
// strtokIndx = strtok(NULL, ",");
// floatFromPC = atof(strtokIndx); // convert this part to a float
}
//============
void showParsedData() {
Serial.print("Message ");
Serial.println(messageFromPC);
Serial.print("Integer ");
Serial.println(integerFromPC);
Serial.print("Float ");
Serial.println(floatFromPC);
}
我怀疑我的问题与变量的数据类型有关。
解决方案
推荐阅读
- javascript - 我可以在没有 Webpack 的情况下默认导入共享其父目录名称的文件吗?
- java - 使用 Axis 2 中的 WSDL2Java 时如何生成基础包下的所有类?
- java - Spring Boot @SpringBootTest 给出 NoClassDefFoundError: ...DataAccessException
- javascript - 带有 Firebase ML 套件的 Nativescript Js OCR
- android - 如何在 Angular 中使用 Nativescript 关闭模态窗口?
- keras - ImportError:无法导入名称'_obtain_input_shape'
- android - 颤动的蓝色示例无法连接到 gatt 服务器
- function - 如何调用元组列表的总和?
- apache - .htaccess 重写条件 %{REQUEST_FILENAME}
- c# - 在应用程序的任何位置访问对象 (C#/WPF)