【问题标题】:PC to Arduino RS-485 connection via converters通过转换器连接 PC 到 Arduino RS-485
【发布时间】:2016-01-27 16:31:47
【问题描述】:

我正在尝试在 PC (Windows) 和 Arduino 之间建立 modbus RS-485 连接。

在 PC 端,我使用 USB 转 RS485 转换器,例如 http://ru.aliexpress.com/item/USB-to-485-RS485-converter-Adapter-Support-Windows-7-8-for-Arduino/1577970568.html

在 Arduino 方面,我使用 TTL-to-RS-485 像这样http://ru.aliexpress.com/item/5pcs-lot-MAX485-Module-RS-485-TTL-to-RS485-Module-for-Arduino/32262338107.html

问题1: 当我将字节从 PC 发送到 Arduino 时。没发生什么事。 Arduino 没有收到任何东西。在这种情况下,我将此代码上传到 Arduino:

#include <SoftwareSerial.h>
#include "RS485_protocol.h"

SoftwareSerial rs485 (10, 11);  // receive pin, transmit pin
const byte ENABLE_PIN = 3;

void setup()
{
  Serial.begin(9600);
  rs485.begin (28800);
  pinMode (ENABLE_PIN, OUTPUT);  // driver output enable
}

void loop()
{ 
  byte buf [1];
  byte received = recvMsg (fAvailable, fRead, buf, sizeof (buf));

  if (received)
    {
      Serial.println(buf[0]);
    } else {
      Serial.println("--");
    }      
}  // end of loop

int fAvailable ()
  {
  return rs485.available ();  
  }

int fRead ()
  {
  return rs485.read ();  
  }

并在 Arduino IDE 中打开串行监视器以显示接收到的数据。 然后我打开 Arduino IDE 的新实例,选择正确的 USB-to-RS485 COM 端口并打开它的串行监视器以发送一些数据。

所以在 Arduino 端串行监视器中,我只看到“--”行。即使我试图在 PC 端的串行监视器中发送一些东西。

另一个问题是:

反之亦然。当我从 Arduino 向 PC 发送一些字节时,我收到了一些奇怪的符号而不是发送的字节。

本例中的Arduino代码为:

#include <SoftwareSerial.h>
#include "RS485_protocol.h"

SoftwareSerial rs485 (10, 11);  // receive pin, transmit pin
const byte ENABLE_PIN = 3;

void setup()
{
  rs485.begin (28800);
  pinMode (ENABLE_PIN, OUTPUT);  // driver output enable

}

void loop()
{ 
  byte msg[1] = {3};
  sendMsg(msg);
}  // end of loop

void sendMsg(byte msg[])
{
  delay (1);  // give the master a moment to prepare to receive
  digitalWrite (ENABLE_PIN, HIGH);  // enable sending
  sendMsg (fWrite, msg, 1);
  digitalWrite (ENABLE_PIN, LOW);  // disable sending
}

void fWrite (const byte what)
  {
  rs485.write (what);  
  }

int fAvailable ()
  {
  return rs485.available ();  
  }

在 PC 端(在 Arduino IDE 串行监视器中)我收到奇数符号而不是“3”符号。

=======

RS485 转换器使用双绞线 A 到 A 和 B 到 B。 RS485 到 TTL 转换器正确连接到 Arduino。 (两个 arduino 之间的通信工作正常)。

请帮忙。

【问题讨论】:

    标签: arduino serial-port rs485


    【解决方案1】:

    RS485 示例:

    使用 SN75176 IC。

    RE (pin 2) connect to ground(用于一直阅读)

    仅限Arduino控制DE PIN for writing data

    但是PC边的问题:

    1. 在哪里桥接DE 引脚? (CTS,DTR,RTD 如果支持)
    2. 您的流量控制是什么? (相关#1)
    3. 你有没有在AB pin 上连接任何电阻?&lt;+5V&gt;----[560R]-----(A)----[120R]-----(B)------[560R]------&lt;-5V&gt; 所以意思是line end
    4. 您是否过滤了DE Pin 2 信号(用于噪声)

    一个技巧:在 arduino 端使用SN75176,因为this IC a RS232(UART) to (RS485) 转换器。

    编辑:Modbus 在串行上有 2 种类型(RTUASCII)。不要在意 RS232/485 的差异,因为different signal, same protocol

    示例数据包类型:

    ASCII : :010300200004+CRC+FE(crc =数据包校验码, fe=结束分隔符) RTU:\x01\x03\x00\x20\x00\x04\x +CRC 在 rtu 上:每个字节需要 1.5 个字符空间,并且没有 start and end 分隔符。

    Modbus node type 的意思是masterslave

    希望对你有帮助。

    【讨论】:

    • 谢谢。对理解 RS485 很有帮助。我的问题出在 RS485_protocol.h 库中。当我连接两台 arduino 或两台 PC 时,它工作正常。但不是当我连接 Arduino 和 PC 时。
    猜你喜欢
    • 2021-09-13
    • 2017-06-21
    • 2020-08-05
    • 1970-01-01
    • 1970-01-01
    • 2011-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多