首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Azure Sphere单击Mikroe RS485 -使用RS485发送和接收字节数组

使用Azure Sphere单击Mikroe RS485 -使用RS485发送和接收字节数组
EN

Stack Overflow用户
提问于 2021-05-25 05:51:53
回答 1查看 53关注 0票数 0

我正在使用Avnet Azure球板与RS485点击板连接。在RS485点击中,GPIO设为高电平以发送来自点击板的RS485信号,而GPIO设为低电平以接收RS485信号。这里的问题是发送请求,它至少需要2秒,但在我们将GPIO PWM切换回低以进行接收之前,连接的传感器在毫秒内返回响应。因此,我的UART返回事件没有触发。任何帮助都将不胜感激。

我的发送UART消息函数

代码语言:javascript
复制
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返回事件

代码语言:javascript
复制
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);
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-25 06:06:32

RS485是半双工的,因此一次只能传输一台设备。因此,您必须在完成发送数据后立即关闭发射器。理想情况下,您应该在硬件中完成此操作。在你的情况下,它看起来就像你在关闭发射器之前有一个delay_milliseconds(2000);。这会导致冲突,并且传输的任何数据都将丢失。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67679350

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档