我正在尝试在 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之间的通信工作正常)。
请帮忙。