0

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

在 PC 端,我使用像这样的 USB-to-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。(两个arduinos之间的通信工作正常)。

请帮忙。

4

1 回答 1

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引脚?<+5V>----[560R]-----(A)----[120R]-----(B)------[560R]------<-5V>好意思line end
  4. 您是否过滤了DE Pin 2信号(用于噪声)

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

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

示例数据包类型:

ASCII : :010300200004+CRC+FE(crc =packet check code, fe=end delimiter) RTU : \x01\x03\x00\x20\x00\x04\x +CRC On rtu : 每个字节需要 1.5 个字符空间并且没有start and end分隔符。

Modbus node type意思masterslave

我希望有帮助。

于 2016-01-30T11:48:47.673 回答