Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Serial port 使用arduino进行ASCII读取_Serial Port_Arduino_Ascii - Fatal编程技术网

Serial port 使用arduino进行ASCII读取

Serial port 使用arduino进行ASCII读取,serial-port,arduino,ascii,Serial Port,Arduino,Ascii,这是我的第一篇文章,我知道这个主题可能非常简单或明显,但我不知道如何解决它 我有一个jyetech wiuch的电容表,声称有一个8-N-1串行输出,下面是。我只想用我的Arduino Uno阅读输出,有人能帮我吗?这是我做的代码,我得到了一些真实的数据,但也有一些奇怪的字符 #include <stdio.h> void setup() { Serial.begin(38400); Serial.println("OK"); } char command[1024]; cha

这是我的第一篇文章,我知道这个主题可能非常简单或明显,但我不知道如何解决它

我有一个jyetech wiuch的电容表,声称有一个8-N-1串行输出,下面是。我只想用我的Arduino Uno阅读输出,有人能帮我吗?这是我做的代码,我得到了一些真实的数据,但也有一些奇怪的字符

#include <stdio.h>

void setup() {
Serial.begin(38400);

Serial.println("OK");
}

char command[1024];
char commandBuffer[128];
int commandBufferSize = 0;

void readCommandBuffer(int bytesToRead) {
int i = 0;
char c = 0;
while (i < 128 && (i < bytesToRead || bytesToRead <= 0)) {
    while (!Serial.available())
        ;
    c = Serial.read();
    if (c == '\r' || c == '\n') {
        break;
    }
    commandBuffer[i] = c;
    i++;
}
commandBufferSize = i;
}

void readCommand() {
command[0] = '\0';
readCommandBuffer(0);
if (strncmp(commandBuffer, "RCV", 3) == 0) {
    commandBuffer[commandBufferSize] = '\0';
    int expectedSize = atoi(commandBuffer + 4);
    if (expectedSize <= 0 || expectedSize > 1024) {
        return;
    }
    Serial.println("RDY");
    int bytesRead = 0;
    while (bytesRead < expectedSize) {
        readCommandBuffer(expectedSize - bytesRead);
        memcpy(command + bytesRead, commandBuffer, commandBufferSize);
        bytesRead += commandBufferSize;
        Serial.print("ACK ");
        Serial.println(commandBufferSize);
    }
    command[bytesRead] = '\0';
} else {
    memcpy(command, commandBuffer, commandBufferSize);
    command[commandBufferSize] = '\0';
}
}

void loop() {
if (Serial.available()) {
    readCommand();
    // "command" now contains the full command
    Serial.println(command);
}}
#包括
无效设置(){
Serial.begin(38400);
Serial.println(“OK”);
}
char命令[1024];
char命令缓冲区[128];
int commandBufferSize=0;
void readCommandBuffer(int bytesToRead){
int i=0;
字符c=0;

而(i<128&&(i 顺便说一句,刚才注意到一个非常类似的问题被问了一会儿:


为什么要写/检查“RCV”/“RDY”/“ACK”?你链接到的手册根本没有提到这些代码——只是一个序列、时间戳和读数。@GregHNZ这只是我找到的一个读取串行输入的代码。我尝试过其他简单的代码,但这一个给了我更清晰的输出。我得到了这样的东西。23513 10112.06r)23514 10112.47 0r)23515 10112.89 00I)23516 101L&ir)23517 1011&r)因此我需要使用软件串行读取传入数据,并使用普通串行发送数据?这将是最简单的解决方案,IMHO。