zoukankan      html  css  js  c++  java
  • PMS5003ST+Arduino Nano 串口读取数据

    先上代码:

    库文件是在guihub上的大神写的https://github.com/jbanaszczyk,我拿来小改下用以支持5003ST

    #include <Arduino.h>
    #include <pms.h>
    
    ////////////////////////////////////////
    
    #if defined PMS_DYNAMIC
    Pms5003 *_pms;
    #define pms (*_pms)
    #else
    Pms5003 pms;
    #endif
    
    ////////////////////////////////////////
    
    auto lastRead = millis();
    
    void setup( void ) {
        Serial.begin( 115200 );
        while ( !Serial ) { };
        Serial.println( "PMS5003ST" );
    
    #if defined PMS_DYNAMIC
        _pms = new Pms5003();
    #else
        pms.begin();
    #endif
    
        //    pms.write( Pms5003::cmdWakeup );
        //    pms.write( Pms5003::cmdModePassive );
    
        //    Serial.println( pms.getDataSize() );
        //    pms.write( Pms5003::cmdModeActive );
    }
    
    ////////////////////////////////////////
    
    void loop( void ) {
    
    
        const int n = Pms5003::Reserved;
        Pms5003::pmsData data[ n ];
    
        Pms5003::PmsStatus status = pms.read( data, n );
    
        switch ( status ) {
            case Pms5003::OK:
            {
    
                Serial.println( "_________________" );
                auto newRead = millis();
                Serial.print( "Wait time " );
                Serial.println( newRead - lastRead );
                lastRead = newRead;
    
    
                for ( size_t i = Pms5003::PM1dot0; i < n; ++i ) {
                    if(i == n-3)
                       Serial.print( float(data[ i ]/1000) );
                    if(i == n-2 || i == n-1)
                        Serial.print( float(data[ i ]/10)); 
                    else
                      Serial.print( data[ i ] );
                    Serial.print( "	" );
                    Serial.print( Pms5003::dataNames[ i ] );
                    Serial.print( " [" );
                    Serial.print( Pms5003::metrics[ i ] );
                    Serial.print( "]" );
                    Serial.println();
                }
                break;
            }
            case Pms5003::noData:
                break;
            default:
                Serial.println( "_________________" );
                Serial.println( Pms5003::errorMsg[ status ] );
        };
    }

    接线图:

    PIN1-----> 5V

    PIN2----->GND

    PIN5----->D8(要接电平转换板,PIN5要3.3V,而D8输出是5V;或者直接用  USB转TTL 连接电脑也可以)

    在来张实拍图:

  • 相关阅读:
    js获取屏幕大小
    获取系统开机的时间(Windows、Linux)
    C++的STL中vector内存分配方法的简单探索
    服务器端如何判断客户端是不是手机
    测试简单for循环的效率
    多少钱都买不到这张表!百万都买不到这张表
    Unable to compile class for JSP
    windows上java中文乱码-指定字符集 -Dfile.encoding=UTF-8
    google翻译插件安装
    工作任务分配时的五个问题
  • 原文地址:https://www.cnblogs.com/nightnine/p/6572891.html
Copyright © 2011-2022 走看看