- //unsigned char是无符号字节型,char类型变量的大小通常为1个字节(1字节=8个位),且属于整型
- // 声明一个无符号的char类型数组,它拥有38个元素
- unsigned char USART_RX_BUF[37] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
- //读数据寄存器(功能码 03H):读 48H 开始的 8 个寄存器值
- //查询消息帧包括设备地址、功能代码、数据信息码、校验码
- unsigned char USART_TX_BUF[8] = {0x01, 0x03, 0x00, 0x48, 0x00, 0x08, 0xC4, 0x1A};
- // 0x01设备地址
- // 0x03读取命令,功能码
- // 0x00, 0x48读取的开始地址
- // 0x08寄存器数
- // 0xC4, 0x1A校验码
- float Dian_Ya, Dian_Liu, Gong_Lv, Dian_Neng, Pf_data, Er_Yang_Hua_Tan, Wen_Du, Pin_Lv;
- void setup() {
- Serial3.begin(9600);
- Serial.begin(9600);
- }
- void loop() {
- Shua_Xin_IM1281B_Shu_Ju(1000);//刷新IM1281B数据
- Huo_Qu_IM1281B_Shu_Ju(1000);//获取IM1281B数据
- }
- /* 刷新IM1281B_数据 */
- unsigned long Shang_Ci_Shua_Xin_IM1281B_Shu_Ju_Shi_Jian ;
- void Shua_Xin_IM1281B_Shu_Ju( int Jian_Ge_Shi_Jian ) {
- if ( millis() - Shang_Ci_Shua_Xin_IM1281B_Shu_Ju_Shi_Jian >= Jian_Ge_Shi_Jian) {
- Shang_Ci_Shua_Xin_IM1281B_Shu_Ju_Shi_Jian = millis();
- /*
- Serial.write()
- 写二进制数据到串口,数据是一个字节一个字节地发送的,若以字符形式发送数字请使用print()代替。
- 写二进制数据到串口,数据是一个字节一个字节地发送的
- 语法:
- Serial.write(val) Serial.write(str) Serial.write(buf, len)
- 参数:
- val: 作为单个字节发送的数据
- str: 由一系列字节组成的字符串
- buf: 同一系列字节组成的数组
- len: 要发送的数组的长度
- 返回:
- byte
- write()会返回发送的字节数,所以读取该返回值是可选的。
- */
- // 1、刷新数据
- Serial3.write(USART_TX_BUF, 8);//写二进制数据到串口(数组,数组的长度)
- }
- }
- /* 获取IM1281B_数据 */
- unsigned long Shang_Ci_Huo_Qu_IM1281B_Shu_Ju_Shi_Jian ;
- void Huo_Qu_IM1281B_Shu_Ju( int Jian_Ge_Shi_Jian ) {
- if ( millis() - Shang_Ci_Huo_Qu_IM1281B_Shu_Ju_Shi_Jian >= Jian_Ge_Shi_Jian) {
- Shang_Ci_Huo_Qu_IM1281B_Shu_Ju_Shi_Jian = millis();
- // 2、获取数据
- int num = 0;
- for (int i = 0; i <= 36; i++) {
- USART_RX_BUF[i] = Serial3.read();
- // Serial.print(USART_RX_BUF[i], HEX);
- // Serial.print(" ");
- }
- Serial.println();
- if (USART_RX_BUF[0] == 1) { //由于开始几次会得到乱码,首字符为1的时候才进行数据处理
- // 3、解析数据 <<位左移 |位或
- Dian_Ya = (((unsigned long)(USART_RX_BUF[3])) << 24) | (((unsigned long)(USART_RX_BUF[4])) << 16) | (((unsigned long)(USART_RX_BUF[5])) << 8) | USART_RX_BUF[6];
- Dian_Liu = (((unsigned long)(USART_RX_BUF[7])) << 24) | (((unsigned long)(USART_RX_BUF[8])) << 16) | (((unsigned long)(USART_RX_BUF[9])) << 8) | USART_RX_BUF[10];
- Gong_Lv = (((unsigned long)(USART_RX_BUF[11])) << 24) | (((unsigned long)(USART_RX_BUF[12])) << 16) | (((unsigned long)(USART_RX_BUF[13])) << 8) | USART_RX_BUF[14];
- Dian_Neng = (((unsigned long)(USART_RX_BUF[15])) << 24) | (((unsigned long)(USART_RX_BUF[16])) << 16) | (((unsigned long)(USART_RX_BUF[17])) << 8) | USART_RX_BUF[18];
- Pf_data = (((unsigned long)(USART_RX_BUF[19])) << 24) | (((unsigned long)(USART_RX_BUF[20])) << 16) | (((unsigned long)(USART_RX_BUF[21])) << 8) | USART_RX_BUF[22];
- Er_Yang_Hua_Tan = (((unsigned long)(USART_RX_BUF[23])) << 24) | (((unsigned long)(USART_RX_BUF[24])) << 16) | (((unsigned long)(USART_RX_BUF[25])) << 8) | USART_RX_BUF[26];
- Wen_Du = (((unsigned long)(USART_RX_BUF[27])) << 24) | (((unsigned long)(USART_RX_BUF[28])) << 16) | (((unsigned long)(USART_RX_BUF[29])) << 8) | USART_RX_BUF[30];
- Pin_Lv = (((unsigned long)(USART_RX_BUF[31])) << 24) | (((unsigned long)(USART_RX_BUF[32])) << 16) | (((unsigned long)(USART_RX_BUF[33])) << 8) | USART_RX_BUF[34];
- // 4、打印数据
- // Serial.println("电压:" + String(Dian_Ya ) + "V");
- // Serial.println("电流:" + String(Dian_Liu ) + "A");
- // Serial.println("有功功率:" + String(Gong_Lv ) + "W");
- // Serial.println("有功电能:" + String(Dian_Neng ) + "W");
- // Serial.println("功率因数:" + String(Pf_data )); //功率因数是指交流电路有功功率对视在功率的比值。用户电器设备在一定电压和功率下,该值越高效益越好,发电设备越能充分利用。
- // Serial.println("二氧化碳排量:" + String(Er_Yang_Hua_Tan ) + "KG");
- //
- // Serial.println();
- // Serial.println();
- // Serial.println("电压:" + String(Dian_Ya * 0.0001) + "V");
- // Serial.println("电流:" + String(Dian_Liu * 0.0001) + "A");
- // Serial.println("有功功率:" + String(Gong_Lv * 0.0001) + "W");
- // Serial.println("有功电能:" + String(Dian_Neng * 0.0001) + "W");
- // Serial.println("功率因数:" + String(Pf_data * 0.001)); //功率因数是指交流电路有功功率对视在功率的比值。用户电器设备在一定电压和功率下,该值越高效益越好,发电设备越能充分利用。
- // Serial.println("二氧化碳排量:" + String(Er_Yang_Hua_Tan * 0.0001) + "KG");
- // Serial.println();
- // Serial.println();
- // Serial.print(Dian_Ya * 0.0001); Serial.print(","); // 第一个值
- // Serial.print(Dian_Liu * 0.0001); Serial.print(","); // 第二个值
- Serial.print(Gong_Lv * 0.0001); Serial.print(",");
- Serial.println(); // 所有数据发送完后要加这一句
- Dian_Ya = 0;
- Dian_Liu = 0;
- Gong_Lv = 0;
- Dian_Neng = 0;
- Pf_data = 0;
- Er_Yang_Hua_Tan = 0;
- }
- }
- }
复制代码
|