Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 #include "mbed.h" 00002 Serial pc(SERIAL_TX, SERIAL_RX); 00003 Serial modbus(PC_10, PC_11); 00004 uint8_t get[13]; 00005 float tem[29]; 00006 00007 uint16_t CRC16(uint8_t *bpMsgStartAddress, uint8_t bDataLen) 00008 { 00009 uint16_t crc = 0xffff; 00010 uint8_t u8tmp; 00011 for (int i=0; i<bDataLen; i++) { 00012 crc ^= *bpMsgStartAddress++; 00013 for (int n=0; n<8; n++) { 00014 u8tmp = crc & 1; 00015 crc >>= 1; 00016 crc &= 0x7fff; 00017 if (u8tmp == 1) { 00018 crc ^= 0xa001; 00019 } 00020 } 00021 } 00022 return crc; 00023 } 00024 00025 void get_data(int len) 00026 { 00027 int i=0; 00028 while(i<len) { 00029 if(modbus.readable() ) { 00030 get[i]=modbus.getc(); 00031 i=i+1; 00032 } 00033 } 00034 } 00035 00036 void put_data(int address,int len) 00037 { 00038 uint8_t put[8]; // 读取4路温度 00039 put[0]=address; 00040 put[1]=0x03; 00041 put[2]=0x00; 00042 put[3]=0x28; 00043 put[4]=0x00; 00044 put[5]=0x04; 00045 uint16_t crc=CRC16(put,6); 00046 //pc.printf("crc:%x,%x,%x",crc,crc&0xFF,(crc&0xFF00)>>8); 00047 put[6]=crc&0xFF; 00048 put[7]=(crc&0xFF00)>>8; 00049 for(int i=0; i<len; i++) 00050 modbus.putc(put[i]); 00051 } 00052 00053 void read_tem() 00054 { 00055 for(int i=0; i<7; i++) { 00056 put_data(i+1,8); 00057 get_data(13); 00058 tem[1+4*i]=(get[3]*256.0+get[4])/10.0f;//一通道 00059 tem[2+4*i]=(get[5]*256.0+get[6])/10.0f;//二通道 00060 tem[3+4*i]=(get[7]*256.0+get[8])/10.0f;//三通道 00061 tem[4+4*i]=(get[9]*256.0+get[10])/10.0f;//四通道 00062 wait_ms(5); 00063 } 00064 } 00065 00066 int main() 00067 { 00068 pc.baud(115200); 00069 modbus.baud(9600); 00070 pc.printf("PC connected!\n"); 00071 while(1) { 00072 read_tem(); 00073 pc.printf("d0:"); 00074 for(int i=1; i<28; i++) { 00075 pc.printf("%.1f,",tem[i]); 00076 } 00077 pc.printf("%.1f",tem[28]); 00078 pc.printf("\n"); 00079 wait(0.1); 00080 } 00081 }
Generated on Sun Jul 31 2022 05:18:37 by
1.7.2