大季 矢花
/
MB2019_main_11_15_ok
aa
Communication/RS485/RS485.cpp@41:99a1158f3eca, 2019-11-08 (annotated)
- Committer:
- kishibekairohan
- Date:
- Fri Nov 08 13:57:55 2019 +0000
- Revision:
- 41:99a1158f3eca
- Parent:
- 31:17be2176063f
- Child:
- 42:980af34f6fe3
uwa
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
t_yamamoto | 0:669ef71cba68 | 1 | #include "RS485.h" |
t_yamamoto | 0:669ef71cba68 | 2 | #include "mbed.h" |
t_yamamoto | 0:669ef71cba68 | 3 | #include "ActuatorHub/ActuatorHub.h" |
M_souta | 21:e3b58d675c1c | 4 | #include "LineHub/LineHub.h" |
t_yamamoto | 0:669ef71cba68 | 5 | #include "../../LED/LED.h" |
t_yamamoto | 0:669ef71cba68 | 6 | #include "../../System/Using.h" |
t_yamamoto | 0:669ef71cba68 | 7 | |
t_yamamoto | 0:669ef71cba68 | 8 | namespace RS485 { |
M_souta | 21:e3b58d675c1c | 9 | |
M_souta | 20:eae8c84f318c | 10 | DigitalOut selectBitT(SELECTBIT_T_PIN); |
M_souta | 21:e3b58d675c1c | 11 | // DigitalOut selectBitR(SELECTBIT_R_PIN); |
M_souta | 31:17be2176063f | 12 | Serial RS485Uart(RS485UART_TX, RS485UART_RX); |
M_souta | 31:17be2176063f | 13 | // Serial RS485Uart(USBTX, USBRX); |
M_souta | 20:eae8c84f318c | 14 | Serial RS485Line(RS485LINE_TX, RS485LINE_RX); |
M_souta | 21:e3b58d675c1c | 15 | |
M_souta | 21:e3b58d675c1c | 16 | bool readFase = 0; |
M_souta | 21:e3b58d675c1c | 17 | char buffer[RS485_BUFFER_LINE] = {0}; |
t_yamamoto | 0:669ef71cba68 | 18 | |
t_yamamoto | 0:669ef71cba68 | 19 | void Transmit(); |
M_souta | 20:eae8c84f318c | 20 | void Recieve(); |
t_yamamoto | 0:669ef71cba68 | 21 | |
t_yamamoto | 0:669ef71cba68 | 22 | void RS485::Initialize() { |
M_souta | 20:eae8c84f318c | 23 | selectBitT = 1; //送信固定 |
M_souta | 21:e3b58d675c1c | 24 | // selectBitR = 0; //受信固定 |
M_souta | 31:17be2176063f | 25 | RS485Uart.baud(38400); // 38400 |
M_souta | 20:eae8c84f318c | 26 | RS485Uart.attach(Transmit, Serial::TxIrq); //送信割り込み |
kishibekairohan | 41:99a1158f3eca | 27 | //RS485Line.baud(9600); |
M_souta | 30:8f092276b2ba | 28 | RS485Line.baud(9600); |
M_souta | 20:eae8c84f318c | 29 | RS485Line.attach(Recieve, Serial::RxIrq); //受信割り込み |
t_yamamoto | 0:669ef71cba68 | 30 | } |
t_yamamoto | 0:669ef71cba68 | 31 | |
t_yamamoto | 0:669ef71cba68 | 32 | void Transmit() { |
M_souta | 21:e3b58d675c1c | 33 | // __disable_irq(); |
t_yamamoto | 0:669ef71cba68 | 34 | static uint8_t count = 0; |
t_yamamoto | 0:669ef71cba68 | 35 | RS485Uart.putc(RS485SendBuffer.GetData()); |
t_yamamoto | 0:669ef71cba68 | 36 | if(count >= 200) { |
t_yamamoto | 0:669ef71cba68 | 37 | #ifdef USE_MOTOR |
t_yamamoto | 0:669ef71cba68 | 38 | LED_DEBUG2 = !LED_DEBUG2; |
t_yamamoto | 0:669ef71cba68 | 39 | #endif |
t_yamamoto | 0:669ef71cba68 | 40 | |
t_yamamoto | 0:669ef71cba68 | 41 | count = 0; |
t_yamamoto | 0:669ef71cba68 | 42 | } else count++; |
t_yamamoto | 0:669ef71cba68 | 43 | __enable_irq(); |
t_yamamoto | 0:669ef71cba68 | 44 | } |
M_souta | 20:eae8c84f318c | 45 | |
M_souta | 20:eae8c84f318c | 46 | void Recieve() { |
M_souta | 21:e3b58d675c1c | 47 | __disable_irq(); |
M_souta | 21:e3b58d675c1c | 48 | static uint8_t time = 0; |
M_souta | 20:eae8c84f318c | 49 | static uint8_t count = 0; |
M_souta | 20:eae8c84f318c | 50 | char data = RS485Line.getc(); |
M_souta | 21:e3b58d675c1c | 51 | if (data == 'S') { |
M_souta | 21:e3b58d675c1c | 52 | readFase = true; |
M_souta | 21:e3b58d675c1c | 53 | time = 0; |
M_souta | 21:e3b58d675c1c | 54 | } else if(data == 'F') { |
M_souta | 21:e3b58d675c1c | 55 | readFase = false; |
M_souta | 21:e3b58d675c1c | 56 | for(int i = 0; i < 8; i++) { |
M_souta | 21:e3b58d675c1c | 57 | lineData[i] = buffer[i]; |
M_souta | 21:e3b58d675c1c | 58 | } |
M_souta | 21:e3b58d675c1c | 59 | time = 0; |
M_souta | 21:e3b58d675c1c | 60 | } else if(readFase) { |
M_souta | 21:e3b58d675c1c | 61 | buffer[time] = data; |
M_souta | 21:e3b58d675c1c | 62 | time++; |
M_souta | 21:e3b58d675c1c | 63 | } else { |
M_souta | 21:e3b58d675c1c | 64 | readFase = false; |
M_souta | 21:e3b58d675c1c | 65 | time = 0; |
M_souta | 21:e3b58d675c1c | 66 | } |
M_souta | 20:eae8c84f318c | 67 | if(count >= 200) { |
M_souta | 20:eae8c84f318c | 68 | LED_DEBUG1 = !LED_DEBUG1; |
M_souta | 20:eae8c84f318c | 69 | count = 0; |
M_souta | 20:eae8c84f318c | 70 | } else count++; |
M_souta | 20:eae8c84f318c | 71 | __enable_irq(); |
M_souta | 20:eae8c84f318c | 72 | } |
M_souta | 21:e3b58d675c1c | 73 | } |