Arduino根据从串口接收到的数据包做出决策 [英] Arduino making decision according to a packet received from serial port

查看:24
本文介绍了Arduino根据从串口接收到的数据包做出决策的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

程序以以下形式监听来自串行端口的消息,其中第一个字符(A 或 D)表示模拟或数字,第二个字符 - 引脚,第三个字符 - 1/0 或 0 到 255.标记 <和 > 显示数据包的开始和结束.

The program listen to messages from serial port in the form or where first character (A or D) means analog or digital, the 2nd character - pin, the 3rd character - 1/0 or from 0 to 255. The markers < and > show the beginning and the end of packet.

例如,如果收到数据包,则通过 digitalWrite(13,1) 打开灯但什么也没有发生.例如,当我通过串行监视器发送时:灯应该闪烁但它没有.模拟输出也一样.

For example, if packet is received, the light is turned on by digitalWrite(13,1) But nothing happens. When I send via serial monitor, for instance: light is supposed to blink but it does not. The same with analogue outputs.

bool started = false;
bool ended = false;
char inData[5];
byte index;


void setup() 
{
  Serial.begin(9600);
}

void loop() 
{
  while (Serial.available() > 0)
  {
    char inChar = Serial.read();


    if (inChar == '<')
    {
      index = 0;
      started = true;
      ended = false; 
    }

    else if (inChar == '>')
    {
      ended = true;
      break;
    }

    else
    {
      if (index <= 4)
      {
        inData[index] = inChar;
        index++;
      }
    }



     if (started && ended)
    {
      if (inData[0] == 'A')
      {
        pinMode(inData[2],OUTPUT);
        analogWrite(inData[2],inData[4]);
      }


      else if (inData[0] == 'D')
      {
        if (inData[4] == 1)
        {
          pinMode(inData[2],OUTPUT);
          digitalWrite(inData[2],HIGH);
        }

        else if (inData[4] == 0)
        {
           pinMode(inData[2],OUTPUT);
           digitalWrite(inData[2],LOW);
        }
      }
      started = false;
      ended = false;
      index = 0; 
    }

  }


Serial.println("Sending");  



}

推荐答案

以下代码将允许您使用示例串行字符串执行方法:

The following code will allow you to execute a method with an example serial string:

<power,led>

一旦它处理了这个字符串,它就会执行以下方法:

Once it processes this string, it'll execute the following method:

sendCommand(cmd, val);

有关如何打开 PIN 11 上的 LED 的示例,请参见下文.

See below for an example of how to turn on an LED on PIN 11.

#include <avr/pgmspace.h>

int IRledPin = 11;

#define SOP '<'
#define EOP '>'

bool started = false;
bool ended = false;

char inData[80];
byte index;

void setup() {
  pinMode(IRledPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  // Read all serial data available, as fast as possible
  while (Serial.available() > 0) {
    char inChar = Serial.read();

    if (inChar == SOP) {
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    } else if (inChar == EOP) {
       ended = true;
       break;
    } else {
      if (index < 79) {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if (started && ended) {
    // The end of packet marker arrived. Process the packet
    char *cmd = strtok(inData, ",");

    if (cmd) {
       char *val = strtok(NULL, ",");
       if (val) {
          sendCommand(cmd, val);
       }
    }

    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
}

void sendCommand(char *command, char *value) {
  if (strcmp(command,"power") == 0) {
    power(value);
  }
}

void power(char* value) {
  if (strcmp(value, "led") == 0) {
    digitalWrite(IRledPin, HIGH);
  }
}

这篇关于Arduino根据从串口接收到的数据包做出决策的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆