簡體   English   中英

Mikroe RS485 Click with Azure Sphere - 使用 RS485 發送和接收字節 arrays

[英]Mikroe RS485 Click with Azure Sphere - Issue sending and receiving byte arrays using RS485

我正在使用連接了 RS485 點擊板的 Avnet Azure 球板。 在 RS485 Click 中,GPIO PWM 拉高以從 Click 板發送 RS485 信號,GPIO PWM 拉低以接收 RS485 信號。 這里的問題是發送請求至少需要 2 秒,但連接的傳感器會在幾毫秒內返回響應,然后我們將 GPIO PWM 切換回低電平以進行接收。 因此我的 UART 返回事件沒有觸發。 任何幫助,將不勝感激。

我發送 UART 消息 function

static void SendUartMessage(int uartFd, const char* dataToSend, size_t totalBytesToSend, bool ignoreRx)
{
    size_t totalBytesSent = 0;
    //size_t totalBytesToSend = strlen(dataToSend);
    int sendIterations = 0;
    close(r1PinFd);
    r1PinFd = GPIO_OpenAsOutput(MIKROE_PWM, GPIO_OutputMode_PushPull, GPIO_Value_High);
    while (totalBytesSent < totalBytesToSend) {
        sendIterations++;

        // Send as much of the remaining data as possible
        size_t bytesLeftToSend = totalBytesToSend - totalBytesSent;
        const char* remainingMessageToSend = dataToSend + totalBytesSent;
        ssize_t bytesSent = write(uartFd, remainingMessageToSend, bytesLeftToSend);
        if (bytesSent == -1) {
            Log_Debug("ERROR: Could not write to UART: %s (%d).\n", strerror(errno), errno);
            exitCode = ExitCode_SendMessage_Write;
            return;
        }

        totalBytesSent += (size_t)bytesSent;
    }
    int c, d;

    delay_milliseconds(2000);
    close(r1PinFd);
    r1PinFd = GPIO_OpenAsOutput(MIKROE_PWM, GPIO_OutputMode_PushPull, GPIO_Value_Low);

    Log_Debug("Sent %zu bytes over UART in %d calls.\n", totalBytesSent, sendIterations);
}

我的 RS485 UART 返回事件

static void UartEventHandler(EventLoop* el, int fd, EventLoop_IoEvents events, void* context)
{
    const size_t receiveBufferSize = 256;
    uint8_t receiveBuffer[receiveBufferSize + 1]; // allow extra byte for string termination
    ssize_t bytesRead;

    // Read incoming UART data. It is expected behavior that messages may be received in multiple
    // partial chunks.
    bytesRead = read(uartFd, receiveBuffer, receiveBufferSize);
    if (bytesRead == -1) {
        Log_Debug("ERROR: Could not read UART: %s (%d).\n", strerror(errno), errno);
        exitCode = ExitCode_UartEvent_Read;
        return;
    }

    if (bytesRead > 0) {
        receiveBuffer[bytesRead] = 0;
        Log_Debug("UART received %d bytes: '%s'.\n", bytesRead);
        char  data_hex_str[sizeof(receiveBuffer) / sizeof(receiveBuffer[0])];
        get_hex(receiveBuffer, sizeof(receiveBuffer) / sizeof(receiveBuffer[0]), data_hex_str, sizeof(receiveBuffer) / sizeof(receiveBuffer[0]), 16);
        strcat(returnDataUart, data_hex_str);
        Log_Debug("\s", returnDataUart);
    }

    char* pjsonBuffer = (char*)malloc(JSON_BUFFER_SIZE);
    if (pjsonBuffer == NULL) {
        Log_Debug("ERROR: not enough memory to send telemetry");
    }

    snprintf(pjsonBuffer, JSON_BUFFER_SIZE,
        "{\"gX\":%.2lf, \"gY\":%.2lf, \"gZ\":%.2lf, \"aX\": %.2f, \"aY\": "
        "%.2f, \"aZ\": %.2f, \"pressure\": %.2f, \"light_intensity\": %.2f, "
        "\"altitude\": %.2f, \"temp\": %.2f,  \"rssi\": %d, \"RS485\": %s}",
        acceleration_g.x, acceleration_g.y, acceleration_g.z, angular_rate_dps.x,
        angular_rate_dps.y, angular_rate_dps.z, pressure_kPa, light_sensor, altitude,
        lsm6dso_temperature, network_data.rssi, returnDataUart);

    Log_Debug("\n[Info] Sending telemetry: %s\n", pjsonBuffer);
    SendTelemetry(pjsonBuffer, true);
    free(pjsonBuffer);
}

RS485 是半雙工的,因此一次只能傳輸一個設備。 因此,您必須在完成發送數據后立即關閉發射器。 理想情況下,您應該在硬件中執行此操作。 在你的情況下,看起來你有一個delay_milliseconds(2000); 在關閉發射器之前。 這會導致沖突,並且任何傳輸的數據都將丟失。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM