请问esp32在rs485模式下单字节收发怎么做?
Posted: Tue Jun 11, 2019 6:34 am
我的esp32一端通过wifi与电脑通信,另一端通过uart串口通信。当由电脑通过wifi发送数据到esp32,esp32再通过uart串口RS485模式将数据发送出去。
我的wifi端接收数据代码如下:
uart口发送数据代码如下:
若我要一字节一字节的接收与发送,达到形如下图的波形效果,该如何改进呢?
下图所发数据为00 55 00 55 。
感谢!!!
我的wifi端接收数据代码如下:
Code: Select all
void recv_data(void *pvParameters)
{
uint32_t recvLen = 0;
char *sendFromNetToUartBuff = (char *)malloc(NETRECEIVEBUFF);
while(1){
memset(sendFromNetToUartBuff, 0, NETRECEIVEBUFF);
if(currentParameters.sWiFiModule.sWiFiModSel.WiFiTransPcolFlag == TCPCLIENT || currentParameters.sWiFiModule.sWiFiModSel.WiFiTransPcolFlag == TCPSERVER){
recvLen = recv(connect_socket, sendFromNetToUartBuff, 1, 0);
if (recvLen > 0) {
} else {
show_socket_error_reason1("recv_data", connect_socket);
break;
}
}
else{
ESP_LOGE(TAG, "WiFiTransPcolFlag configuration is invalid......");
break;
}
uint8_t* newBuffer = (uint8_t*)malloc(recvLen+1);
memset(newBuffer, 0, (recvLen+1));
memcpy(newBuffer,sendFromNetToUartBuff,recvLen);
uartNetTransmitCapsule_t* capsule = malloc(sizeof(uartNetTransmitCapsule_t));
capsule->netRecvData = newBuffer;
capsule->bufferSize = recvLen;
ESP_LOGI(TAG, "Transmit.c: buffer is %s\n", newBuffer);
if(!xQueueSend(queueToUart, capsule, 100/portTICK_RATE_MS)){
xQueueReset(queueToUart);
ESP_LOGE(TAG, "Failed to send net recv datas to uart recv task......");
continue;
}
free(capsule);
}
ESP_LOGE(TAG, "Tcp recv data error......");
isNetRecvTaskErrorTrigger = true;
free(sendFromNetToUartBuff);
xEventGroupSetBits(rs485EventGroup, UDP_RS485_RS232_BIT);
vTaskDelete(NULL);
}
Code: Select all
static void uartSendData(void *pvParameters)
{
uint32_t recvLen, delayTickTimes;
int netPacketTimes = 0;
recvFromNetToUartBuff = (uartNetTransmitCapsule_t*)malloc(UART_Q_ITEM_SIZE);
while(1){
if(!xQueueReceive(queueToUart, recvFromNetToUartBuff, portMAX_DELAY)){
ESP_LOGE(TAG, "Failed to receive datas from net to uart......");
continue;
}
if(currentParameters.sPort[0].uiTransitMode == RS485_2_wires){
netPacketTimes++;
comportCurrentTransmitMode = RS485_TX_MODE;
uart_disable_intr_mask(EX_UART_NUM, UART_INTR_MASK);
recvLen = uart_write_bytes(EX_UART_NUM, (const char*) recvFromNetToUartBuff->netRecvData, 1);
if(recvLen > 0){
comportCurrentTransmitMode = RS485_RX_MODE;
uart_flush(EX_UART_NUM);
uart_enable_intr_mask(EX_UART_NUM, UART_INTR_MASK);
}else{
RS485TransmitToggle(RS485_RX_MODE);
comportCurrentTransmitMode = RS485_RX_MODE;
uart_flush(EX_UART_NUM);
uart_enable_intr_mask(EX_UART_NUM, UART_INTR_MASK);
free(recvFromNetToUartBuff->netRecvData);
continue;
}
}else{
recvLen = uart_write_bytes(EX_UART_NUM, (const char*) recvFromNetToUartBuff->netRecvData, recvFromNetToUartBuff->bufferSize);
if(recvLen > 0){
}else{
free(recvFromNetToUartBuff->netRecvData);
continue;
}
}
free(recvFromNetToUartBuff->netRecvData);
}
free(recvFromNetToUartBuff);
xSemaphoreGive(portManagerTaskSemaphore);
vTaskDelete(NULL);
}
下图所发数据为00 55 00 55 。
感谢!!!