#include "uart_dev.h"
#include <stdarg.h>
#include <string.h>
#include <stdio.h>
#include "pDebug.h"

#include "FreeRTOS.h"
#include "task.h"

device_handle g_J1_uart6_handle;
device_handle g_J2_uart7_handle;
device_handle g_J3_usart2_handle;
device_handle g_J4_uart8_handle;
device_handle g_J5_0_usart3_handle;
device_handle g_Upward_uart5_handle;

/* 接收缓冲区数组 */
uint8_t J1_Rbuffer[1] = {0x00};
uint8_t J2_Rbuffer[1] = {0x00};
uint8_t J3_Rbuffer[1] = {0x00};
uint8_t J4_Rbuffer[1] = {0x00};
uint8_t J5_0_Rbuffer[1] = {0x00};
uint8_t Upward_Rbuffer[1] = {0x00};

/* 环形buff */
#define  J485BuffLen 256
uint8_t J1_inBuff[J485BuffLen];
uint8_t J2_inBuff[J485BuffLen];
uint8_t J3_inBuff[J485BuffLen];
uint8_t J4_inBuff[J485BuffLen];
uint8_t J5_0_inBuff[J485BuffLen];
#define  UpwardBuffLen 1024
uint8_t Upward_inBuff[UpwardBuffLen];

/* 用于当做发送时的标志位 8个位，分别对应各个串口 */
#define J1_485_Send 0x01
#define J2_485_Send 0x02
#define J3_485_Send 0x04
#define J4_485_Send 0x08
#define J5_0_485_Send 0x20
#define Upward_uart_Send 0x40
static uint8_t uartInterruptSendFlag = 0;

// // static uint8_t getJ1_485_SendState(void);
// static void setJ1_485_SendState(uint8_t state);
// // static uint8_t getJ2_485_SendState(void);
// static void setJ2_485_SendState(uint8_t state);
// // static uint8_t getJ3_485_SendState(void);
// static void setJ3_485_SendState(uint8_t state);
// // static uint8_t getJ4_485_SendState(void);
// static void setJ4_485_SendState(uint8_t state);
// // static uint8_t getJ5_0_485_SendState(void);
// static void setJ5_0_485_SendState(uint8_t state);
// // static uint8_t getUpward_uart_SendState(void);
// static void setUpward_uart_SendState(uint8_t state);


#ifdef RS485BUSIDLE1
/* 用于总线空闲标志位 */
//该位为0时表示总线空闲
#define BUS_IDLE 0x01
// //该位为0时表示进入了空闲中断
// #define softwareTimeFlag 0x02
static uint8_t J1_485_IDLE_Flag = 0;
static uint8_t J2_485_IDLE_Flag = 0;
static uint8_t J3_485_IDLE_Flag = 0;
static uint8_t J4_485_IDLE_Flag = 0;
static uint8_t J5_0_485_IDLE_Flag = 0;
#endif

/* J5_0_485当前连接到的端口 */
static uint8_t connectPort = connectJ0;

/**
  * @brief  串口信息初始化，串口号及波特率.
  * @param  uart_index    对应的硬件串口号
  * @param  uart_baudrate 波特率
  */
uart_device_info uart_devices[]={
  [0] = {
      .init          = 0,
      .uart_index    = J1_485_INDEX,
      .uart_baudrate = 9600,
  },
  [1] = {
      .init          = 0,
      .uart_index    = J2_485_INDEX,
      .uart_baudrate = 9600,
  },
  [2] = {
      .init          = 0,
      .uart_index    = J3_485_INDEX,
      .uart_baudrate = 9600,
  },
  [3] = {
      .init          = 0,
      .uart_index    = J4_485_INDEX,
      .uart_baudrate = 9600,
  },
  [4] = {
      .init          = 0,
      .uart_index    = J5_0_485_INDEX,
      .uart_baudrate = 9600,
  },
  [5] = {
      .init          = 0,
      .uart_index    = Upward_UART_INDEX,
      .uart_baudrate = 115200,
  },
};

static device_handle uart_dev_init(uartIndex_e uart_index, uint8_t *buff, int buff_size);
static void uart_init(uartIndex_e uart_index, uint32_t baud);


/**
  * @brief  初始化串口设备.
  * @param  uart_index 初始化串口号
  * @param  buff       串口循环buff地址
  * @param  buff_size  串口循环buff对应大小
  * @retval 串口句柄
  */
device_handle uart_dev_init(uartIndex_e uart_index, uint8_t *buff, int buff_size)
{
    int i = 0;
    for (; i < ELEMENT_OF(uart_devices); i++) {
        if (uart_devices[i].uart_index == uart_index) {
            if (!uart_devices[i].init) {
                InitRingQueue(&uart_devices[i].uart_ring_queue, buff, buff_size); 
                uart_init(uart_index, uart_devices[i].uart_baudrate);  
                
                uart_devices[i].init = 1;
            }
            return (device_handle)(&uart_devices[i]);
        }
    }
    return 0;
}

/**
  * @brief  串口硬件初始化.
  * @param  uart_index 串口号
  * @param  baud 波特率
  * @retval None
  */
void uart_init(uartIndex_e uart_index, uint32_t baud)
{
    if (uart_index == J1_485_INDEX) {
        J1_485_Init(baud);
    } else if (uart_index == J2_485_INDEX) {
        J2_485_Init(baud);
    } else if (uart_index == J3_485_INDEX) {
        J3_485_Init(baud);
    } else if (uart_index == J4_485_INDEX) {
        J4_485_Init(baud);
    } else if (uart_index == J5_0_485_INDEX) {
        J5_0_485_Init(baud);
    } else if (uart_index == Upward_UART_INDEX) {
        Upward_UART5_Init(baud);
    }
}

/**
  * @brief  从串口设备循环buff读取一个数据.
  * @param  device 串口句柄
  * @retval 读取到的字符
  */
uint8_t uart_dev_in_char(device_handle device)
{
    uart_device_info *device_info = (uart_device_info *)device;
    char c = 0;

    if (uart_dev_char_present(device))
        OutRingQueue(&device_info->uart_ring_queue, (u_int8_t*)&c);
    return c;
}

/**
  * @brief  判断串口设备循环buff是否有数据.
  * @param  device 串口句柄
  * @retval 0 空   1有数据
  */
int uart_dev_char_present(device_handle device)
{
    uart_device_info *device_info = (uart_device_info *)device;

    if((!device) || (!device_info->init))
        return 0;

    return !RingQueueEmpty(&device_info->uart_ring_queue);
}

/* 初始化 */
void Init_J1_485(uint32_t baud)
{
    uart_devices[0].uart_baudrate = baud;
    g_J1_uart6_handle = uart_dev_init(J1_485_INDEX, J1_inBuff, J485BuffLen);
}

void Init_J2_485(uint32_t baud)
{
    uart_devices[1].uart_baudrate = baud;
    g_J2_uart7_handle = uart_dev_init(J2_485_INDEX, J2_inBuff, J485BuffLen);
}

void Init_J3_485(uint32_t baud)
{
    uart_devices[2].uart_baudrate = baud;
    g_J3_usart2_handle = uart_dev_init(J3_485_INDEX, J3_inBuff, J485BuffLen);
}

void Init_J4_485(uint32_t baud)
{
    uart_devices[3].uart_baudrate = baud;
    g_J4_uart8_handle = uart_dev_init(J4_485_INDEX, J4_inBuff, J485BuffLen);
}

void Init_J5_0_485(uint32_t baud)
{
    uart_devices[4].uart_baudrate = baud;
    g_J5_0_usart3_handle = uart_dev_init(J5_0_485_INDEX, J5_0_inBuff, J485BuffLen);
}

void Init_Upward_uart(uint32_t baud)
{
    uart_devices[5].uart_baudrate = baud;
    g_Upward_uart5_handle = uart_dev_init(Upward_UART_INDEX, Upward_inBuff, UpwardBuffLen);
}

/**
  * @brief  得到串口的发送状态
  * @param  device 设备
  * @retval 0：发送完成   1：发送中 0xFF：错误
  */
uint8_t getUartSendState(device_handle device)
{
    if (device == g_J1_uart6_handle) {
        return uartInterruptSendFlag & J1_485_Send;
    }
    else if (device == g_J2_uart7_handle) {
        return uartInterruptSendFlag & J2_485_Send;
    }
    else if (device == g_J3_usart2_handle) {
        return uartInterruptSendFlag & J3_485_Send;
    }
    else if (device == g_J4_uart8_handle) {
        return uartInterruptSendFlag & J4_485_Send;
    }
    else if (device == g_J5_0_usart3_handle) {
        return uartInterruptSendFlag & J5_0_485_Send;
    }
    else if (device == g_Upward_uart5_handle) {
        return uartInterruptSendFlag & Upward_uart_Send;
    }
    else {
        return 0xFF;
    }
}

// /**
//   * @brief  设置设备的发送状态
//   * @param  
//             state    0：发生完成   1：发送中
//   * @retval 
//   */
// void setUartSendState(device_handle device, uint8_t state)
// {
//     if (device == g_J1_uart6_handle) {
//         if (state == 1) {
//             uartInterruptSendFlag |= J1_485_Send;
//         }
//         else if (state == 0) {
//             uartInterruptSendFlag &= (~J1_485_Send);
//         }
//     }
//     else if (device == g_J2_uart7_handle) {
//         if (state == 1) {
//             uartInterruptSendFlag |= J2_485_Send;
//         }
//         else if (state == 0) {
//             uartInterruptSendFlag &= (~J2_485_Send);
//         }
//     }
//     else if (device == g_J3_usart2_handle) {
//         if (state == 1) {
//             uartInterruptSendFlag |= J3_485_Send;
//         }
//         else if (state == 0) {
//             uartInterruptSendFlag &= (~J3_485_Send);
//         }
//     }
//     else if (device == g_J4_uart8_handle) {
//         if (state == 1) {
//             uartInterruptSendFlag |= J4_485_Send;
//         }
//         else if (state == 0) {
//             uartInterruptSendFlag &= (~J4_485_Send);
//         }
//     }
//     else if (device == g_J5_0_usart3_handle) {
//         if (state == 1) {
//             uartInterruptSendFlag |= J5_0_485_Send;
//         }
//         else if (state == 0) {
//             uartInterruptSendFlag &= (~J5_0_485_Send);
//         }
//     }
//     else if (device == g_Upward_uart5_handle) {
//         if (state == 1) {
//             uartInterruptSendFlag |= Upward_uart_Send;
//         }
//         else if (state == 0) {
//             uartInterruptSendFlag &= (~Upward_uart_Send);
//         }
//     }
// }

/**
  * @brief  得到J2_485的发送状态
  * @param  
  * @retval 0：发生完成   1：发送中
  */
uint8_t getJ1_485_SendState(void)
{
    return uartInterruptSendFlag & J1_485_Send;
}

/**
  * @brief  设置J2_485的发送状态
  * @param  0：发生完成   1：发送中
  * @retval 
  */
void setJ1_485_SendState(uint8_t state)
{
    if (state == 1) {
        uartInterruptSendFlag |= J1_485_Send;
    }
    else if (state == 0) {
        uartInterruptSendFlag &= (~J1_485_Send);
    }
}

/**
  * @brief  得到J2_485的发送状态
  * @param  
  * @retval 0：发生完成   1：发送中
  */
uint8_t getJ2_485_SendState(void)
{
    return uartInterruptSendFlag & J2_485_Send;
}

/**
  * @brief  设置J2_485的发送状态
  * @param  0：发生完成   1：发送中
  * @retval 
  */
void setJ2_485_SendState(uint8_t state)
{
    if (state == 1) {
        uartInterruptSendFlag |= J2_485_Send;
    }
    else if (state == 0) {
        uartInterruptSendFlag &= (~J2_485_Send);
    }
}

/**
  * @brief  得到J3_485的发送状态
  * @param  
  * @retval 0：发生完成   1：发送中
  */
uint8_t getJ3_485_SendState(void)
{
    return uartInterruptSendFlag & J3_485_Send;
}

/**
  * @brief  设置J3_485的发送状态
  * @param  0：发生完成   1：发送中
  * @retval 
  */
void setJ3_485_SendState(uint8_t state)
{
    if (state == 1) {
        uartInterruptSendFlag |= J3_485_Send;
    }
    else if (state == 0) {
        uartInterruptSendFlag &= (~J3_485_Send);
    }
}

/**
  * @brief  得到J4_485的发送状态
  * @param  
  * @retval 0：发生完成   1：发送中
  */
uint8_t getJ4_485_SendState(void)
{
    return uartInterruptSendFlag & J4_485_Send;
}

/**
  * @brief  设置J4_485的发送状态
  * @param  0：发生完成   1：发送中
  * @retval 
  */
void setJ4_485_SendState(uint8_t state)
{
    if (state == 1) {
        uartInterruptSendFlag |= J4_485_Send;
    }
    else if (state == 0) {
        uartInterruptSendFlag &= (~J4_485_Send);
    }
}

/**
  * @brief  得到J5_0_485的发送状态
  * @param  
  * @retval 0：发生完成   1：发送中
  */
uint8_t getJ5_0_485_SendState(void)
{
    return uartInterruptSendFlag & J5_0_485_Send;
}

/**
  * @brief  设置J1_485的发送状态
  * @param  0：发生完成   1：发送中
  * @retval 
  */
void setJ5_0_485_SendState(uint8_t state)
{
    if (state == 1) {
        uartInterruptSendFlag |= J5_0_485_Send;
    }
    else if (state == 0) {
        uartInterruptSendFlag &= (~J5_0_485_Send);
    }
}

/**
  * @brief  得到Upward_uart的发送状态
  * @param  
  * @retval 0：发生完成   1：发送中
  */
uint8_t getUpward_uart_SendState(void)
{
    return uartInterruptSendFlag & Upward_uart_Send;
}

/**
  * @brief  设置Upward_uart的发送状态
  * @param  0：发生完成   1：发送中
  * @retval 
  */
void setUpward_uart_SendState(uint8_t state)
{
    if (state == 1) {
        uartInterruptSendFlag |= Upward_uart_Send;
    }
    else if (state == 0) {
        uartInterruptSendFlag &= (~Upward_uart_Send);
    }
}

/* 串口中断处理函数 */
void J3_Interrupt()
{
    /* 数据接收中断 */
    if (USART_GetITStatus(J3_USART, USART_IT_RXNE) != RESET) {   
    #ifdef RS485BUSIDLE1
        if (J3_485_IDLE_Flag == 0) {
            J3_485_IDLE_Flag = 1;
            J3_485_IN_RXNE();
        }
    #endif
        J3_Rbuffer[0] = USART_ReceiveData(J3_USART); //接收数据
        uart_device_info *dev = (uart_device_info *)g_J3_usart2_handle;
        if(!RingQueueFull(&dev->uart_ring_queue))
            InRingQueue(&dev->uart_ring_queue, J3_Rbuffer[0]);
    }

#ifdef UARTINTERRUPTSEND
    /* 数据发送中断 */
    if (USART_GetITStatus(J3_USART, USART_IT_TXE) != RESET) {
        uart_device_info *dev = (uart_device_info *)g_J3_usart2_handle;
        if (dev->uart_send_data.count >= dev->uart_send_data.len) {
            USART_ITConfig(J3_USART, USART_IT_TXE, DISABLE);
            // setJ3_485_SendState(0);
            J3_485_IN_TXE();
            readJ3_485;
            return;
        }        
        USART_SendData(J3_USART, dev->uart_send_data.data[dev->uart_send_data.count++]);
    }
#endif

#ifdef RS485BUSIDLE1
    /* 空闲中断 */
    if (USART_GetITStatus(J3_USART, USART_IT_IDLE) != RESET) {
        J3_485_IN_IDLE();
    }
#endif
}

void J5_0_Interrupt()
{
    /* 数据接收中断 */
    if (USART_GetITStatus(J5_0_USART, USART_IT_RXNE) != RESET) { //中断产生
    #ifdef RS485BUSIDLE1
        if (J5_0_485_IDLE_Flag == 0) {
            J5_0_485_IDLE_Flag = 1;
            J5_0_485_IN_RXNE();
        }
    #endif  
        J5_0_Rbuffer[0] = USART_ReceiveData(J5_0_USART); //接收数据
        uart_device_info *dev = (uart_device_info *)g_J5_0_usart3_handle;
        if(!RingQueueFull(&dev->uart_ring_queue))
           InRingQueue(&dev->uart_ring_queue, J5_0_Rbuffer[0]);
    }

#ifdef UARTINTERRUPTSEND
    /* 数据发送中断 */
    if (USART_GetITStatus(J5_0_USART, USART_IT_TXE) != RESET) {
        uart_device_info *dev = (uart_device_info *)g_J5_0_usart3_handle;
        if (dev->uart_send_data.count >= dev->uart_send_data.len) {
            USART_ITConfig(J5_0_USART, USART_IT_TXE, DISABLE);
            // setJ5_0_485_SendState(0);
            J5_0_485_IN_TXE();
            readJ5_0_485;
            return;
        }
        USART_SendData(J5_0_USART, dev->uart_send_data.data[dev->uart_send_data.count++]);
    }
#endif

#ifdef RS485BUSIDLE1
    /* 空闲中断 */
    if (USART_GetITStatus(J5_0_USART, USART_IT_IDLE) != RESET) {
        J5_0_485_IN_IDLE();
    }
#endif
}

void Upward_Interrupt()
{
    /* 数据接收中断 */
    if (USART_GetITStatus(Upward_USART, USART_IT_RXNE) != RESET) { //中断产生    
        Upward_Rbuffer[0] = USART_ReceiveData(Upward_USART); //接收数据
        uart_device_info *dev = (uart_device_info *)g_Upward_uart5_handle;
        if(!RingQueueFull(&dev->uart_ring_queue))
           InRingQueue(&dev->uart_ring_queue, Upward_Rbuffer[0]);
    }

#ifdef UARTINTERRUPTSEND
    /* 数据发送中断 */
    if (USART_GetITStatus(Upward_USART, USART_IT_TXE) != RESET) {
        uart_device_info *dev = (uart_device_info *)g_Upward_uart5_handle;
        // log_info("%d \n", dev->uart_send_data.count);
        if (dev->uart_send_data.count >= dev->uart_send_data.len) {
            USART_ITConfig(Upward_USART, USART_IT_TXE, DISABLE);
            // setUpward_uart_SendState(0);
            Upward_USART_IN_TXE();
            return;
        }
        USART_SendData(Upward_USART, dev->uart_send_data.data[dev->uart_send_data.count++]);
    }
#endif
}

void J1_Interrupt()
{
    /* 数据接收中断 */
    if (USART_GetITStatus(J1_USART, USART_IT_RXNE) != RESET) { //中断产生
    #ifdef RS485BUSIDLE1
        if (J1_485_IDLE_Flag == 0) {
            J1_485_IDLE_Flag = 1;
            J1_485_IN_RXNE();
        }
    #endif   
        J1_Rbuffer[0] = USART_ReceiveData(J1_USART); //接收数据
        uart_device_info *dev = (uart_device_info *)g_J1_uart6_handle;
        if(!RingQueueFull(&dev->uart_ring_queue))
           InRingQueue(&dev->uart_ring_queue, J1_Rbuffer[0]);
    }

#ifdef UARTINTERRUPTSEND
    /* 数据发送中断 */
    if (USART_GetITStatus(J1_USART, USART_IT_TXE) != RESET) {
        uart_device_info *dev = (uart_device_info *)g_J1_uart6_handle;
        if (dev->uart_send_data.count >= dev->uart_send_data.len) {
            USART_ITConfig(J1_USART, USART_IT_TXE, DISABLE);
            // setJ1_485_SendState(0);
            J1_485_IN_TXE();
            readJ1_485;
            return;
        }
        USART_SendData(J1_USART, dev->uart_send_data.data[dev->uart_send_data.count++]);
    }
#endif

#ifdef RS485BUSIDLE1
    /* 空闲中断 */
    if (USART_GetITStatus(J1_USART, USART_IT_IDLE) != RESET) {
        J1_485_IN_IDLE();
    }
#endif
}

void J2_Interrupt()
{
    /* 数据接收中断 */
    if (USART_GetITStatus(J2_USART, USART_IT_RXNE) != RESET) { //中断产生   
    #ifdef RS485BUSIDLE1
        if (J2_485_IDLE_Flag == 0) {
            J2_485_IDLE_Flag = 1;
            J2_485_IN_RXNE();
        }
    #endif    
        J2_Rbuffer[0] = USART_ReceiveData(J2_USART); //接收数据
        uart_device_info *dev = (uart_device_info *)g_J2_uart7_handle;
        if(!RingQueueFull(&dev->uart_ring_queue))
           InRingQueue(&dev->uart_ring_queue, J2_Rbuffer[0]);
    }

#ifdef UARTINTERRUPTSEND
    /* 数据发送中断 */
    if (USART_GetITStatus(J2_USART, USART_IT_TXE) != RESET) {
        uart_device_info *dev = (uart_device_info *)g_J2_uart7_handle;
        if (dev->uart_send_data.count >= dev->uart_send_data.len) {
            USART_ITConfig(J2_USART, USART_IT_TXE, DISABLE);
            // setJ2_485_SendState(0);
            J2_485_IN_TXE();
            readJ2_485;
            return;
        }
        USART_SendData(J2_USART, dev->uart_send_data.data[dev->uart_send_data.count++]);
    }
#endif

#ifdef RS485BUSIDLE1
    /* 空闲中断 */
    if (USART_GetITStatus(J2_USART, USART_IT_IDLE) != RESET) {
        J2_485_IN_IDLE();
    }
#endif
}

void J4_Interrupt()
{
    /* 数据接收中断 */
    if (USART_GetITStatus(J4_USART, USART_IT_RXNE) != RESET) { //中断产生    
    #ifdef RS485BUSIDLE1
        if (J4_485_IDLE_Flag == 0) {
            J4_485_IDLE_Flag = 1;
            J4_485_IN_RXNE();
        }
    #endif   
        J4_Rbuffer[0] = USART_ReceiveData(J4_USART); //接收数据
        uart_device_info *dev = (uart_device_info *)g_J4_uart8_handle;
        if(!RingQueueFull(&dev->uart_ring_queue))
           InRingQueue(&dev->uart_ring_queue, J4_Rbuffer[0]);
    }

#ifdef UARTINTERRUPTSEND
    /* 数据发送中断 */
    if (USART_GetITStatus(J4_USART, USART_IT_TXE) != RESET) {
        uart_device_info *dev = (uart_device_info *)g_J4_uart8_handle;
        if (dev->uart_send_data.count >= dev->uart_send_data.len) {
            USART_ITConfig(J4_USART, USART_IT_TXE, DISABLE);
            // setJ4_485_SendState(0);
            J4_485_IN_TXE();
            readJ4_485;
            return;
        }
        USART_SendData(J4_USART, dev->uart_send_data.data[dev->uart_send_data.count++]);
    }
#endif

#ifdef RS485BUSIDLE1
    /* 空闲中断 */
    if (USART_GetITStatus(J4_USART, USART_IT_IDLE) != RESET) {
        J4_485_IN_IDLE();
    }
#endif
}

#ifdef UARTINTERRUPTSEND
/**
  * @brief  通过中断发送
  * @param  device 设备
            data 指向数据的指针
            len 数据长度
  * @retval 0：成功 
            1：上次数据还未发送完成
            0xFF：错误
  */
uint8_t uartInterruptSend(device_handle device,uint8_t *data, uint16_t len)
{
    /* 上次未发送完，或设备不存在 */
    if (getUartSendState(device)) {
        return getUartSendState(device);
    }

    /* 指向数据 */
    uart_device_info *dev = (uart_device_info *)device;
    dev->uart_send_data.data = data;
    dev->uart_send_data.count = 0;
    dev->uart_send_data.len = len;

    /* 开始发送 */
    if (device == g_J1_uart6_handle) {
        setJ1_485_SendState(1);
        writeJ1_485;
        USART_ITConfig(J1_USART, USART_IT_TXE, ENABLE);
        USART_SendData(J1_USART, dev->uart_send_data.data[dev->uart_send_data.count++]);
    }
    else if (device == g_J2_uart7_handle) {
        setJ2_485_SendState(1);
        writeJ2_485;
        USART_ITConfig(J2_USART, USART_IT_TXE, ENABLE);
        USART_SendData(J2_USART, dev->uart_send_data.data[dev->uart_send_data.count++]);    
    }
    else if (device == g_J3_usart2_handle) {
        setJ3_485_SendState(1);
        writeJ3_485;
        USART_ITConfig(J3_USART, USART_IT_TXE, ENABLE);
        USART_SendData(J3_USART, dev->uart_send_data.data[dev->uart_send_data.count++]);    
    }
    else if (device == g_J4_uart8_handle) {
        setJ4_485_SendState(1);
        writeJ4_485;
        USART_ITConfig(J4_USART, USART_IT_TXE, ENABLE);
        USART_SendData(J4_USART, dev->uart_send_data.data[dev->uart_send_data.count++]);    
    }
    else if (device == g_J5_0_usart3_handle) {
        setJ5_0_485_SendState(1);
        writeJ5_0_485;
        USART_ITConfig(J5_0_USART, USART_IT_TXE, ENABLE);
        USART_SendData(J5_0_USART, dev->uart_send_data.data[dev->uart_send_data.count++]);    
    }
    else if (device == g_Upward_uart5_handle) {
        setUpward_uart_SendState(1);
        USART_ITConfig(Upward_USART, USART_IT_TXE, ENABLE);
        USART_SendData(Upward_USART, dev->uart_send_data.data[dev->uart_send_data.count++]);    
    }

    return 0;
}
#endif

#ifdef RS485BUSIDLE1
/**
  * @brief  得到总线的空闲状态
  * @param  device 设备
  * @retval 0 空闲
            1 不空闲
            0xFF 异常
  */
uint8_t getBUSIDLEFlag(device_handle device)
{
    if (device == g_J1_uart6_handle) {
        return (J1_485_IDLE_Flag & BUS_IDLE);
    } else if (device == g_J2_uart7_handle) {
        return (J2_485_IDLE_Flag & BUS_IDLE);
    } else if (device == g_J3_usart2_handle) {
        return (J3_485_IDLE_Flag & BUS_IDLE);
    } else if (device == g_J4_uart8_handle) {
        return (J4_485_IDLE_Flag & BUS_IDLE);
    } else if (device == g_J5_0_usart3_handle) {
        return (J5_0_485_IDLE_Flag & BUS_IDLE);
    }
    return 0xFF;
}

// /**
//   * @brief  得到总线的空闲状态
//   * @param  device 设备
//   * @retval 0 空闲
//             1 不空闲
//             0xFF 异常
//   */
// uint8_t getSoftwareIDLEFlag(device_handle device)
// {
//     if (device == g_J1_uart6_handle) {
//         return (J1_485_IDLE_Flag & softwareTimeFlag);
//     } else if (device == g_J2_uart7_handle) {
//         return (J2_485_IDLE_Flag & softwareTimeFlag);
//     } else if (device == g_J3_usart2_handle) {
//         return (J3_485_IDLE_Flag & softwareTimeFlag);
//     } else if (device == g_J4_uart8_handle) {
//         return (J4_485_IDLE_Flag & softwareTimeFlag);
//     } else if (device == g_J5_0_usart3_handle) {
//         return (J5_0_485_IDLE_Flag & softwareTimeFlag);
//     }
//     return 0xFF;
// }

// /**
//   * @brief  设置总线对应的串口进入空闲状态（空闲中断获得）
//   * @param  device 设备
//             state 状态  0 空闲
//                         1 不空闲
//   * @retval 
//   */
// void setSoftwareIDLEFlag(device_handle device, uint8_t state)
// {
//     if (state == 0) {
//         if (device == g_J1_uart6_handle) {
//             J1_485_IDLE_Flag &= (~softwareTimeFlag);
//         } else if (device == g_J2_uart7_handle) {
//             J2_485_IDLE_Flag &= (~softwareTimeFlag);
//         } else if (device == g_J3_usart2_handle) {
//             J3_485_IDLE_Flag &= (~softwareTimeFlag);
//         } else if (device == g_J4_uart8_handle) {
//             J4_485_IDLE_Flag &= (~softwareTimeFlag);
//         } else if (device == g_J5_0_usart3_handle) {
//             J5_0_485_IDLE_Flag &= (~softwareTimeFlag);
//         }
//     }
//     else if (state == 1) {
//         if (device == g_J1_uart6_handle) {
//             J1_485_IDLE_Flag &= (softwareTimeFlag);
//         } else if (device == g_J2_uart7_handle) {
//             J2_485_IDLE_Flag &= (softwareTimeFlag);
//         } else if (device == g_J3_usart2_handle) {
//             J3_485_IDLE_Flag &= (softwareTimeFlag);
//         } else if (device == g_J4_uart8_handle) {
//             J4_485_IDLE_Flag &= (softwareTimeFlag);
//         } else if (device == g_J5_0_usart3_handle) {
//             J5_0_485_IDLE_Flag &= (softwareTimeFlag);
//         }    
//     }
// }

/**
  * @brief  设置总线空闲状态
  * @param  device 设备
            state 状态  0 空闲
                        1 不空闲
  * @retval 
  */
void setBUSIDLEFlag(device_handle device, uint8_t state)
{
    // if (state == 0) {
    //     if (device == g_J1_uart6_handle) {
    //         J1_485_IDLE_Flag &= (~BUS_IDLE);
    //     } else if (device == g_J2_uart7_handle) {
    //         J2_485_IDLE_Flag &= (~BUS_IDLE);
    //     } else if (device == g_J3_usart2_handle) {
    //         J3_485_IDLE_Flag &= (~BUS_IDLE);
    //     } else if (device == g_J4_uart8_handle) {
    //         J4_485_IDLE_Flag &= (~BUS_IDLE);
    //     } else if (device == g_J5_0_usart3_handle) {
    //         J5_0_485_IDLE_Flag &= (~BUS_IDLE);
    //     }
    // }
    // else if (state == 1) {
    //     if (device == g_J1_uart6_handle) {
    //         J1_485_IDLE_Flag &= (BUS_IDLE);
    //     } else if (device == g_J2_uart7_handle) {
    //         J2_485_IDLE_Flag &= (BUS_IDLE);
    //     } else if (device == g_J3_usart2_handle) {
    //         J3_485_IDLE_Flag &= (BUS_IDLE);
    //     } else if (device == g_J4_uart8_handle) {
    //         J4_485_IDLE_Flag &= (BUS_IDLE);
    //     } else if (device == g_J5_0_usart3_handle) {
    //         J5_0_485_IDLE_Flag &= (BUS_IDLE);
    //     }    
    // }

    if (state != 0 || state != 1) {
        return;
    }

    if (device == g_J1_uart6_handle) {
        J1_485_IDLE_Flag = state;
    } else if (device == g_J2_uart7_handle) {
        J2_485_IDLE_Flag = state;
    } else if (device == g_J3_usart2_handle) {
        J3_485_IDLE_Flag = state;
    } else if (device == g_J4_uart8_handle) {
        J4_485_IDLE_Flag = state;
    } else if (device == g_J5_0_usart3_handle) {
        J5_0_485_IDLE_Flag = state;
    }
}
#endif

/**
  * @brief  设置总线空闲状态
  * @param  port 要连接的端口
  * @retval 
  */
void setConnectPort(uint8_t port)
{
    if (port == connectJ0) {
        connectPort = connectJ0;
        USART_CONNET_J0();
    } else if (port == connectJ5) {
        connectPort = connectJ5;
        USART_CONNET_J5();
    } else if (port == connectJ6) {
        connectPort = connectJ6;
        USART_CONNET_J6();
    } else if (port == connectJ7) {
        connectPort = connectJ7;
        USART_CONNET_J7();
    } else if (port == connectJ8) {
        connectPort = connectJ8;
        USART_CONNET_J8();
    } else if (port == connectJ9) {
        connectPort = connectJ9;
        USART_CONNET_J9();
    } 
}

/**
  * @brief  得到连接的端口
  * @param  
  * @retval 连接的端口
  */
uint8_t getConnectPort(void)
{
    return connectPort;
}
