大季 矢花
/
MB2019_main_11_15_ok
aa
Communication/RS485/RS485.cpp@30:8f092276b2ba, 2019-10-22 (annotated)
- Committer:
- M_souta
- Date:
- Tue Oct 22 02:03:26 2019 +0000
- Revision:
- 30:8f092276b2ba
- Parent:
- 22:7d93f79a3686
- Child:
- 31:17be2176063f
iii
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 | 30:8f092276b2ba | 12 | // Serial RS485Uart(RS485UART_TX, RS485UART_RX); |
M_souta | 30:8f092276b2ba | 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 | 30:8f092276b2ba | 25 | RS485Uart.baud(9600); // 38400 |
M_souta | 20:eae8c84f318c | 26 | RS485Uart.attach(Transmit, Serial::TxIrq); //送信割り込み |
M_souta | 30:8f092276b2ba | 27 | RS485Line.baud(9600); |
M_souta | 20:eae8c84f318c | 28 | RS485Line.attach(Recieve, Serial::RxIrq); //受信割り込み |
t_yamamoto | 0:669ef71cba68 | 29 | } |
t_yamamoto | 0:669ef71cba68 | 30 | |
t_yamamoto | 0:669ef71cba68 | 31 | void Transmit() { |
M_souta | 21:e3b58d675c1c | 32 | // __disable_irq(); |
t_yamamoto | 0:669ef71cba68 | 33 | static uint8_t count = 0; |
t_yamamoto | 0:669ef71cba68 | 34 | RS485Uart.putc(RS485SendBuffer.GetData()); |
t_yamamoto | 0:669ef71cba68 | 35 | if(count >= 200) { |
t_yamamoto | 0:669ef71cba68 | 36 | #ifdef USE_MOTOR |
t_yamamoto | 0:669ef71cba68 | 37 | LED_DEBUG2 = !LED_DEBUG2; |
t_yamamoto | 0:669ef71cba68 | 38 | #endif |
t_yamamoto | 0:669ef71cba68 | 39 | |
t_yamamoto | 0:669ef71cba68 | 40 | count = 0; |
t_yamamoto | 0:669ef71cba68 | 41 | } else count++; |
t_yamamoto | 0:669ef71cba68 | 42 | __enable_irq(); |
t_yamamoto | 0:669ef71cba68 | 43 | } |
M_souta | 20:eae8c84f318c | 44 | |
M_souta | 20:eae8c84f318c | 45 | void Recieve() { |
M_souta | 21:e3b58d675c1c | 46 | __disable_irq(); |
M_souta | 21:e3b58d675c1c | 47 | static uint8_t time = 0; |
M_souta | 20:eae8c84f318c | 48 | static uint8_t count = 0; |
M_souta | 20:eae8c84f318c | 49 | char data = RS485Line.getc(); |
M_souta | 21:e3b58d675c1c | 50 | if (data == 'S') { |
M_souta | 21:e3b58d675c1c | 51 | readFase = true; |
M_souta | 21:e3b58d675c1c | 52 | time = 0; |
M_souta | 21:e3b58d675c1c | 53 | } else if(data == 'F') { |
M_souta | 21:e3b58d675c1c | 54 | readFase = false; |
M_souta | 21:e3b58d675c1c | 55 | for(int i = 0; i < 8; i++) { |
M_souta | 21:e3b58d675c1c | 56 | lineData[i] = buffer[i]; |
M_souta | 21:e3b58d675c1c | 57 | } |
M_souta | 21:e3b58d675c1c | 58 | time = 0; |
M_souta | 21:e3b58d675c1c | 59 | } else if(readFase) { |
M_souta | 21:e3b58d675c1c | 60 | buffer[time] = data; |
M_souta | 21:e3b58d675c1c | 61 | time++; |
M_souta | 21:e3b58d675c1c | 62 | } else { |
M_souta | 21:e3b58d675c1c | 63 | readFase = false; |
M_souta | 21:e3b58d675c1c | 64 | time = 0; |
M_souta | 21:e3b58d675c1c | 65 | } |
M_souta | 20:eae8c84f318c | 66 | if(count >= 200) { |
M_souta | 20:eae8c84f318c | 67 | LED_DEBUG1 = !LED_DEBUG1; |
M_souta | 20:eae8c84f318c | 68 | count = 0; |
M_souta | 20:eae8c84f318c | 69 | } else count++; |
M_souta | 20:eae8c84f318c | 70 | __enable_irq(); |
M_souta | 20:eae8c84f318c | 71 | } |
M_souta | 21:e3b58d675c1c | 72 | } |