通过串口控制LED [英] Controlling LED using serial port

查看:172
本文介绍了通过串口控制LED的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为一个简单的自动化项目的一部分,我试图控制通过串口成LED。我不能让下面的code工作

  INT引脚= 0;
INT状态= 0;无效设置(){
    Serial.begin(9600); //打开串口,设置数据传输速率为9600 bps
}空隙环(){
    //发送数据,只有当您收到数据:
    如果(Serial.available()大于0){
        //读取传入字节:
        如果(Serial.read()=='S'和放大器;&安培; Serial.read()=='S'){
            //命令来设置引脚
            引脚= Serial.read() - 65;
            状态= Serial.read() - '0';
            Serial.print(配置状态命令收到);
            //设置引脚
            pinMode(针,OUTPUT);
            digitalWrite(引脚,状态== 0 LOW:HIGH?);
        }
    }
}

我从我的Python程序发送SSN1到Arduino串行端口进行测试,并没有任何反应(我已连接上13脚的LED)

  SS  - 配置状态命令
N - (无引脚)+'A' - 针数13
1 - 状态(0 =低,1 =高)


解决方案

我提高了我的code,因为它看起来像这样

  INT引脚= 0;
INT状态= 0;无效设置(){
        Serial.begin(9600); //打开串口,设置数据传输速率为9600 bps
}空隙环(){        //发送数据,只有当您收到数据:
        如果(Serial.available()→3){//我使用的4个字符块/字节为命令
                //读取传入字节:
                如果(Serial.read()=='S'和放大器;&安培; Serial.read()=='S'){
                  //命令来设置引脚
                  引脚= Serial.read() - 65;
                  状态= Serial.read() - '0';
                  Serial.print(配置状态命令收到);
                  //设置引脚
                  pinMode(针,OUTPUT);
                  digitalWrite(引脚,状态== 0 LOW:HIGH?);
                }
                延迟(50);
        }
}

它可以完美的我。由于循环的高频率,阿尔杜伊诺未能拾取连续读取的字节。所以,我们都在等待4个字节到缓冲区中积累阅读这些(Arduino的串行缓冲区为64字节)。

As part of a simple Automation project, I was trying to control some LEDs through serial port. I cannot make the following code working

int pin =0;
int state = 0;

void setup() {
    Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
}

void loop() {
    // send data only when you receive data:
    if (Serial.available() > 0) {
        // read the incoming byte:
        if(Serial.read() == 'S' && Serial.read() == 'S') {
            // Command to set the pin
            pin  = Serial.read() - 65;
            state = Serial.read() - '0';
            Serial.print("Set State Command received");
            // Set the Pin
            pinMode(pin, OUTPUT);          
            digitalWrite(pin, state == 0? LOW:HIGH);
        }
    }
}

I am sending "SSN1" from my python program to the Arduino serial port for testing, and nothing happens (I have an LED connected on pin 13)

SS -  Set State Command
N  - (pin no) + 'A'  - Pin number 13
1  - State ( 0 = LOW, 1= HIGH)

解决方案

I improved my code as it look like this

int pin =0;
int state = 0;

void setup() {
        Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
}

void loop() {

        // send data only when you receive data:
        if (Serial.available() > 3) { // I am using chunk of 4 characters/bytes as a command 
                // read the incoming byte:
                if(Serial.read() == 'S' && Serial.read() == 'S') {
                  // Command to set the pin
                  pin  = Serial.read() - 65;
                  state = Serial.read() - '0';
                  Serial.print("Set State Command received");
                  // Set the Pin
                  pinMode(pin, OUTPUT);          
                  digitalWrite(pin, state == 0? LOW:HIGH);
                }
                delay(50); 
        }
}

It works perfectly for me. Due to the high frequency of looping, Arduino was not able pickup the bytes for consecutive read. So we are waiting for 4 bytes to accumulate in the buffer to read those (Arduino's serial buffer is 64 bytes).

这篇关于通过串口控制LED的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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