
send RS485 for testing Delta Sevro
Fork of RS4851121R by
main.cpp@3:48b96c8c25fa, 2018-04-13 (annotated)
- Committer:
- WeberYang
- Date:
- Fri Apr 13 06:55:06 2018 +0000
- Branch:
- RS485_DeltaSevro
- Revision:
- 3:48b96c8c25fa
- Parent:
- 2:bbb217e1219e
- Child:
- 6:ec210c04bec9
send data to sevro and get.; Then print data to pc
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
YusukeWakuta | 0:c963f91bdd23 | 1 | //相互通信確認用 |
YusukeWakuta | 0:c963f91bdd23 | 2 | #include "mbed.h" |
YusukeWakuta | 0:c963f91bdd23 | 3 | #define BUFFER 30 |
YusukeWakuta | 0:c963f91bdd23 | 4 | |
WeberYang | 2:bbb217e1219e | 5 | Serial rs485(PA_9,PA_10);//,115200);//(p9,p10); |
YusukeWakuta | 0:c963f91bdd23 | 6 | Serial pc(USBTX,USBRX); |
WeberYang | 2:bbb217e1219e | 7 | DigitalOut Receiver(D7);//(p5); |
WeberYang | 3:48b96c8c25fa | 8 | |
WeberYang | 3:48b96c8c25fa | 9 | unsigned int CRC_Verify(unsigned char *cBuffer, unsigned int iBufLen); |
WeberYang | 3:48b96c8c25fa | 10 | void setAddress(char MotorAddress,int16_t DataAddress,int16_t Data); |
WeberYang | 3:48b96c8c25fa | 11 | |
WeberYang | 2:bbb217e1219e | 12 | #define LENG 31 //0x42 + 31 bytes equal to 32 bytes |
WeberYang | 2:bbb217e1219e | 13 | unsigned char buf[LENG]; |
WeberYang | 2:bbb217e1219e | 14 | //attributes - program variables |
WeberYang | 2:bbb217e1219e | 15 | char stringOverSerialBuffer[41]; // buffer to store received string over pc |
WeberYang | 2:bbb217e1219e | 16 | int newCommandFlag = 0; // flag for ISR |
YusukeWakuta | 0:c963f91bdd23 | 17 | |
WeberYang | 2:bbb217e1219e | 18 | char data[BUFFER]; |
YusukeWakuta | 0:c963f91bdd23 | 19 | int num1,num2; |
WeberYang | 2:bbb217e1219e | 20 | int idx; |
WeberYang | 2:bbb217e1219e | 21 | bool flagrec = false; |
WeberYang | 3:48b96c8c25fa | 22 | |
WeberYang | 3:48b96c8c25fa | 23 | unsigned int CRC_Verify(unsigned char *cBuffer, unsigned int iBufLen) |
WeberYang | 3:48b96c8c25fa | 24 | { |
WeberYang | 3:48b96c8c25fa | 25 | unsigned int i, j; |
WeberYang | 3:48b96c8c25fa | 26 | //#define wPolynom 0xA001 |
WeberYang | 3:48b96c8c25fa | 27 | unsigned int wCrc = 0xffff; |
WeberYang | 3:48b96c8c25fa | 28 | unsigned int wPolynom = 0xA001; |
WeberYang | 3:48b96c8c25fa | 29 | /*---------------------------------------------------------------------------------*/ |
WeberYang | 3:48b96c8c25fa | 30 | for (i = 0; i < iBufLen; i++) |
WeberYang | 3:48b96c8c25fa | 31 | { |
WeberYang | 3:48b96c8c25fa | 32 | wCrc ^= cBuffer[i]; |
WeberYang | 3:48b96c8c25fa | 33 | for (j = 0; j < 8; j++) |
WeberYang | 3:48b96c8c25fa | 34 | { |
WeberYang | 3:48b96c8c25fa | 35 | if (wCrc &0x0001) |
WeberYang | 3:48b96c8c25fa | 36 | { |
WeberYang | 3:48b96c8c25fa | 37 | wCrc = (wCrc >> 1) ^ wPolynom; |
WeberYang | 3:48b96c8c25fa | 38 | } |
WeberYang | 3:48b96c8c25fa | 39 | else |
WeberYang | 3:48b96c8c25fa | 40 | { |
WeberYang | 3:48b96c8c25fa | 41 | wCrc = wCrc >> 1; |
WeberYang | 3:48b96c8c25fa | 42 | } |
WeberYang | 3:48b96c8c25fa | 43 | } |
WeberYang | 3:48b96c8c25fa | 44 | } |
WeberYang | 3:48b96c8c25fa | 45 | return wCrc; |
WeberYang | 3:48b96c8c25fa | 46 | } |
WeberYang | 2:bbb217e1219e | 47 | void pc_rx(){ |
WeberYang | 2:bbb217e1219e | 48 | //pc.putc(rs485.getc()); |
WeberYang | 2:bbb217e1219e | 49 | int index = 0; |
WeberYang | 2:bbb217e1219e | 50 | |
WeberYang | 2:bbb217e1219e | 51 | Receiver = 0; |
WeberYang | 2:bbb217e1219e | 52 | // while(rs485.readable() || (rx != '\n')) { |
WeberYang | 2:bbb217e1219e | 53 | // while(flagrec) { |
WeberYang | 2:bbb217e1219e | 54 | num1 = rs485.readable(); |
WeberYang | 2:bbb217e1219e | 55 | for(idx= 0; rs485.readable(); idx++) |
WeberYang | 2:bbb217e1219e | 56 | { |
WeberYang | 2:bbb217e1219e | 57 | data[idx]= rs485.getc(); |
WeberYang | 2:bbb217e1219e | 58 | |
WeberYang | 2:bbb217e1219e | 59 | } |
WeberYang | 2:bbb217e1219e | 60 | |
WeberYang | 2:bbb217e1219e | 61 | data[++idx]= '\0'; |
WeberYang | 2:bbb217e1219e | 62 | for(idx= 0; data[idx] != '\0'; idx++) |
WeberYang | 2:bbb217e1219e | 63 | pc.putc(data[idx]); |
WeberYang | 2:bbb217e1219e | 64 | flagrec = true; |
WeberYang | 2:bbb217e1219e | 65 | // } |
WeberYang | 2:bbb217e1219e | 66 | // pc.printf("finish!"); |
YusukeWakuta | 0:c963f91bdd23 | 67 | |
taurin | 1:41cb97261876 | 68 | } |
WeberYang | 3:48b96c8c25fa | 69 | void setAddress(char MotorAddress,int16_t DataAddress,int16_t Data) |
WeberYang | 3:48b96c8c25fa | 70 | { |
WeberYang | 3:48b96c8c25fa | 71 | unsigned char sendData[8]; |
WeberYang | 3:48b96c8c25fa | 72 | int16_t tmpCRC; |
WeberYang | 3:48b96c8c25fa | 73 | //char MotorAddress; |
WeberYang | 3:48b96c8c25fa | 74 | char Function; |
WeberYang | 3:48b96c8c25fa | 75 | //int DataAddress,Data; |
WeberYang | 3:48b96c8c25fa | 76 | char DataAddressH,DataAddressL; |
WeberYang | 3:48b96c8c25fa | 77 | char dataH,dataL; |
WeberYang | 3:48b96c8c25fa | 78 | int i; |
WeberYang | 3:48b96c8c25fa | 79 | sendData[6] = 0xff; |
WeberYang | 3:48b96c8c25fa | 80 | sendData[7] = 0xff; |
WeberYang | 3:48b96c8c25fa | 81 | |
WeberYang | 3:48b96c8c25fa | 82 | //MotorAddress = address; |
WeberYang | 3:48b96c8c25fa | 83 | Function = 0x06; |
WeberYang | 3:48b96c8c25fa | 84 | //DataAddress = data; |
WeberYang | 3:48b96c8c25fa | 85 | //Data = 0x0001; |
WeberYang | 3:48b96c8c25fa | 86 | DataAddressH = ((DataAddress>>8)&0xFF); |
WeberYang | 3:48b96c8c25fa | 87 | DataAddressL = ((DataAddress>>0)&0xFF); |
WeberYang | 3:48b96c8c25fa | 88 | dataH = ((Data>>8)&0xFF); |
WeberYang | 3:48b96c8c25fa | 89 | dataL = ((Data>>0)&0xFF); |
WeberYang | 3:48b96c8c25fa | 90 | |
WeberYang | 3:48b96c8c25fa | 91 | sendData[0] = 0x01;//MotorAddress; |
WeberYang | 3:48b96c8c25fa | 92 | sendData[1] = 0x03;//Function; |
WeberYang | 3:48b96c8c25fa | 93 | sendData[2] = 0x02;//DataAddressH; |
WeberYang | 3:48b96c8c25fa | 94 | sendData[3] = 0;//DataAddressL; |
WeberYang | 3:48b96c8c25fa | 95 | sendData[4] = 0;//dataH; |
WeberYang | 3:48b96c8c25fa | 96 | sendData[5] = 2;//dataL; |
WeberYang | 3:48b96c8c25fa | 97 | tmpCRC = CRC_Verify(sendData, 6); |
WeberYang | 3:48b96c8c25fa | 98 | sendData[6] = (tmpCRC & 0xFF); |
WeberYang | 3:48b96c8c25fa | 99 | sendData[7] = (tmpCRC>>8); |
WeberYang | 3:48b96c8c25fa | 100 | Receiver = 1; |
WeberYang | 3:48b96c8c25fa | 101 | wait_ms(1); |
WeberYang | 3:48b96c8c25fa | 102 | for (i=0;i<8;i++) |
WeberYang | 3:48b96c8c25fa | 103 | { |
WeberYang | 3:48b96c8c25fa | 104 | rs485.printf("%c",sendData[i]); |
WeberYang | 3:48b96c8c25fa | 105 | } |
WeberYang | 3:48b96c8c25fa | 106 | wait_ms(1); |
WeberYang | 3:48b96c8c25fa | 107 | Receiver = 0; |
WeberYang | 3:48b96c8c25fa | 108 | //RS485.attach(&onInterrupt,Serial::RxIrq); |
WeberYang | 3:48b96c8c25fa | 109 | //=========================================== |
WeberYang | 3:48b96c8c25fa | 110 | } |
YusukeWakuta | 0:c963f91bdd23 | 111 | int main() |
YusukeWakuta | 0:c963f91bdd23 | 112 | { |
WeberYang | 2:bbb217e1219e | 113 | rs485.baud(115200); |
WeberYang | 2:bbb217e1219e | 114 | pc.baud(115200); |
WeberYang | 3:48b96c8c25fa | 115 | |
taurin | 1:41cb97261876 | 116 | Receiver=0; |
WeberYang | 2:bbb217e1219e | 117 | rs485.attach(pc_rx, Serial::RxIrq); |
WeberYang | 2:bbb217e1219e | 118 | while(1) |
WeberYang | 2:bbb217e1219e | 119 | { |
WeberYang | 2:bbb217e1219e | 120 | // pc.printf("test"); |
WeberYang | 3:48b96c8c25fa | 121 | |
WeberYang | 3:48b96c8c25fa | 122 | setAddress(1,0x0214,0x0001); |
WeberYang | 3:48b96c8c25fa | 123 | |
WeberYang | 3:48b96c8c25fa | 124 | |
WeberYang | 3:48b96c8c25fa | 125 | wait_ms(1); |
WeberYang | 3:48b96c8c25fa | 126 | |
WeberYang | 3:48b96c8c25fa | 127 | |
WeberYang | 3:48b96c8c25fa | 128 | |
WeberYang | 3:48b96c8c25fa | 129 | } |
YusukeWakuta | 0:c963f91bdd23 | 130 | } |