【问题标题】:Can I temporarily disable Arduino Serial data receive?我可以暂时禁用 Arduino 串行数据接收吗?
【发布时间】:2018-08-31 12:21:11
【问题描述】:

我在做一个项目,遇到了一些问题。

我正在使用 DHT11 温度传感器Arduino UnoTFT LCD 显示屏 2.2 英寸型号 ITDB02-2.2。。 p>

我希望我的项目做的是为传感器使用 2 种功能模式,我可以在程序开始时从键盘上选择它们(一种是正常的,一种将在特殊场合使用)(所以我需要串行通信)。

我注意到,如果我以任何速率启动串行通信,屏幕将无法正常工作,因此我将Arduino Serial.begin(9600)Serial.end() 用于程序的模式选择部分。

问题:即使我结束了串行通信,我的 Arduino 仍在通过串行端口发送数据并且看起来像这样:

我发现Serial.end() 函数不会关闭串行通信,而只是关闭通信速率。我很好奇您是否有任何想法可以用来消除多余的数据,在计算机接收到它之前忽略它。

我卡住了。我认为中断是一种解决方案,但据我在互联网上研究的结果并不理想。

我的 ARDUINO 代码:

#include <SimpleDHT.h>
#include <UTFT.h>

UTFT myGLCD(ITDB22,A5,A4,A3,A2);
SimpleDHT11 dht11;

// Declare which fonts we will be using
extern uint8_t BigFont[];
//dht sensor data pin
int dataPinSensor1 = 12;
char mode;
int del;

void setup()
{
    Serial.begin(9600);
    Serial.print("Select functioning mode");
    mode=SensorModeSelect(mode);
    Serial.end();
    pinMode(12, INPUT);
}

void loop()
{
    if(mode=='1') {
        FirstFuncMode(dataPinSensor1);
    }
    if(mode=='2') {
        SecondFuncMode(dataPinSensor1,del);
    }
    delay(10);
}

char SensorModeSelect(char in)
{
    char mode='0';
    while(mode=='0') {
        if(Serial.available() > 0) {
            mode=Serial.read();
        }
    }
    if (mode == '1') {
        Serial.print("\nMOD1 SELECTED: press t key to aquire data \n");
    }
    if (mode == '2') {
        Serial.print("\nMOD2 SELECTED: press q if you want to quit auto mode \n");
        Serial.print("Select the data aquisition period(not smaller than 1 second) \n");
    }
    return mode;
}

int DataAqPeriod()
{
    int del=0;
    while(del==0) {
        while(Serial.available() > 0) {
            //Get char and convert to int
            char a = Serial.read();
            int c = a-48;
            del *= 10;
            del += c;
            delay(10);
        }
    }
    del*=1000;
    return del;
}

void FirstFuncMode(int dataPinSensor1)
{
    byte temperature = 0;
    byte humidity = 0;
    int err = SimpleDHTErrSuccess;
    bool DispCond=false;
    Serial.begin(9600);
    delay(1500);
    if (Serial.read() == 't' ) {
        DispCond=true;
        //read temperature and compare it with an error value
        if((err = dht11.read(dataPinSensor1, &temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
            Serial.print("unreliable measurement or unselected functioning mode");
        }
        byte f = temperature * 1.8 + 32;
        Serial.print((int)temperature);
        Serial.print(" *C, ");
        Serial.print((int)f);
        Serial.print(" *F, ");
        Serial.print((int)humidity);
        Serial.println(" H humidity");
        delay(1500);
    }

    Serial.end();
    if(DispCond==true) {
        //Setup the LCD
        myGLCD.InitLCD();
        myGLCD.setFont(BigFont);
        //print value on LCD
        displayNoInit((int)temperature,(int)humidity);
    }
}

void SecondFuncMode(int dataPinSensor1,int del)
{
    bool q=false;
    byte temperature = 0;
    byte humidity = 0;
    int err = SimpleDHTErrSuccess;
    Serial.begin(9600);
    del=DataAqPeriod();
    Serial.end();
    //Setup the LCD
    myGLCD.InitLCD();
    myGLCD.setFont(BigFont);

    while(q==false) {
        Serial.begin(9600);
        //read temperature and compare it with an error value
        if((err = dht11.read(dataPinSensor1, &temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
            Serial.print("unreliable measurement or unselected functioning mode \n");
        }
        float f = temperature * 1.8 + 32;
        Serial.print((int)temperature);
        Serial.print(" *C, ");
        Serial.print((int)f);
        Serial.print(" *F, ");
        Serial.print((int)humidity);
        Serial.println(" H humidity");
        delay(del);
        if(Serial.read() == 'q')
            q=true;
        Serial.end();
        displayNoInit((int)temperature,(int)humidity);
        delay(10);
    }
}

void displayNoInit(int temperature,int humidity)
{
    //effective data display
    myGLCD.clrScr();
    myGLCD.setColor(255, 255, 0);
    myGLCD.setBackColor(10,10,10);
    myGLCD.print(" Temperature ", CENTER, 10);
    myGLCD.setColor(254, 254, 254);
    myGLCD.printNumI(temperature, CENTER, 45);
    myGLCD.setColor(255, 255, 0);
    myGLCD.print("C ", RIGHT, 45);
    myGLCD.print("Relative Hum.", CENTER, 90);
    myGLCD.setColor(204, 245, 250);
    myGLCD.printNumI(humidity, CENTER, 120);
    myGLCD.print("%", RIGHT, 120);
}

【问题讨论】:

    标签: arduino serial-port


    【解决方案1】:

    Serial.end() 的定义是正确的,它不会禁用串行监视器,只会禁用中断。调用Serial.end() 后,您可以像这样禁用串行监视器。

    #include <avr/io.h>
    
    // Save status register, disable interrupts
    uint8_t oldSREG = SREG;
    cli();
    
    // Disable TX and RX    
    cbi(UCSRB, RXEN);
    cbi(UCSRB, TXEN);
    
    // Disable RX ISR
    cbi(UCSRB, RXCIE);
    
    // Flush the internal buffer
    Serial.flush();
    
    // Restore status register
    SREG = oldSREG; 
    

    【讨论】:

    • 你好斯特凡。你能告诉我UCSRB是在哪里定义的吗?当我尝试将上面的代码放入我的程序中时,我收到一条错误消息,指出 UCSRB 未在我的函数范围内声明。或者我应该如何定义它。
    • UCSRB 是一个寄存器,用于控制串行外设的各种功能。寄存器定义在avr/io.h 文件中定义。将#include &lt;avr/io.h&gt; 语句添加到您的源文件中。
    猜你喜欢
    • 1970-01-01
    • 2016-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-21
    • 1970-01-01
    • 2016-08-22
    • 1970-01-01
    相关资源
    最近更新 更多