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.
Fork of RS4851121R by
Revision 7:853fc58d8624, committed 2018-04-17
- Comitter:
- WeberYang
- Date:
- Tue Apr 17 06:15:36 2018 +0000
- Branch:
- RS485_DeltaSevro
- Parent:
- 6:ec210c04bec9
- Commit message:
- change to get 0x0066(encoder rev)
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Tue Apr 17 03:35:44 2018 +0000 +++ b/main.cpp Tue Apr 17 06:15:36 2018 +0000 @@ -8,7 +8,6 @@ Serial rs485(PA_9,PA_10);//,115200);//(p9,p10); Serial pc(USBTX,USBRX); DigitalOut Receiver(D7);//(p5); - float cnv; @@ -16,15 +15,16 @@ void setAddress(char MotorAddress,int16_t DataAddress,int16_t Data); #define LENG 31 //0x42 + 31 bytes equal to 32 bytes +#define APR 0x0066 //0066H means encoder abs rev unsigned char buffer[LENG]; //attributes - program variables char stringOverSerialBuffer[41]; // buffer to store received string over pc int newCommandFlag = 0; // flag for ISR int len; - +int a,b; char data[BUFFER]; char num[16]; - +int dataH,datanum; char recChar=0; char recArr[20]; int index=0; @@ -54,6 +54,16 @@ } return wCrc; } +int str2int(const char* str, int star, int end) +{ + int i; + int ret = 0; + for (i = star; i < end+1; i++) + { + ret = ret *10 + (str[i] - '0'); + } + return ret; +} // functions void flushSerialBuffer() { while (rs485.readable()) { @@ -62,16 +72,41 @@ } void readData() { - recChar = rs485.getc(); - recArr[index] = recChar; - index++; - if ((recChar == '\r') || (index>=19)) { - - recArr[index] = 0; - index = 0; - pc.printf("%s\r\n", recArr); - flushSerialBuffer(); - recFlag = true; + while (rs485.readable()) + { + recChar = rs485.getc(); + recArr[index] = recChar; + index++; + if ((recChar == '\r') || (index>18)) + { + datanum = str2int(recArr,1,2); + if (datanum == 1) + { + dataH = str2int(recArr,7,10); + pc.printf("M1 = %d\r\n",dataH); + } + else + { + pc.printf("M1 = no data\r\n"); + } + + if (datanum == 2) + { + dataH = str2int(recArr,7,10); + pc.printf("M2 = %d\r\n",dataH); + } + else + { + pc.printf("M2 = no data\r\n"); + } + + recArr[index] = 0; + index = 0; + //pc.printf("%s\r\n", recArr); + + flushSerialBuffer(); + recFlag = true; + } } } @@ -107,10 +142,11 @@ } wait_ms(1); Receiver = 0; - //RS485.attach(&onInterrupt,Serial::RxIrq); - //=========================================== + wait_ms(10); //give 10ms let receive complete. } int i; +bool timeout=false; +int WDindex = 0; int main() { rs485.baud(38400); @@ -121,10 +157,22 @@ while(1) { recFlag = false; - setAddress(1,0x0214,0x0001); - while (recFlag == false) - { - wait_ms(1); - } +// setAddress(1,0x0214,0x0001); +// setAddress(2,0x0214,0x0001); + setAddress(1,APR,0x0001); + setAddress(2,APR,0x0001); +// while (recFlag == false || timeout == false) +// { +// if(WDindex<100) +// { +// WDindex++; +// wait_ms(1); +// } +// else +// { +// WDindex = 0; +// timeout = true; +// } +// } } } \ No newline at end of file