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" シンボルの代わりに奇数のシンボルが表示されます。
=======
ツイストペア A から A および B から B で配線された RS485 コンバーター。 Arduino に適切に接続された RS485 から TTL コンバーター。(2 つの arduino 間の通信は正常に動作します)。
助けてください。