我试图在PC (Windows)和Arduino之间建立modbus RS-485连接.
在PC端,我使用USB到PC 485转换器,就像这个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一样。
Problem1:当我从PC发送字节到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 485串口,并打开串口监视器发送一些数据。
因此,在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 side串行监视器中),我接收奇怪的符号而不是"3“符号。
=======
RS485转换器连接有双绞线A到A和B到B。RS485到TTL转换器连接到Arduino。(两个arduinos之间的交流很好)。
请帮帮忙。
发布于 2016-01-30 11:48:47
RS485的例子:
使用SN75176集成电路。
RE (pin 2) connect to ground
(经常阅读)
只有DE PIN for writing data
的Arduino控制
但是PC
方面的问题:
DE
引脚在哪里?(CTS,DTR,RTD
(如果支持)A
,B
引脚上了吗?<+5V>----[560R]-----(A)----[120R]-----(B)------[560R]------<-5V>
这么刻薄的line end
DE Pin 2
信号(为了噪声)吗?技巧:在arduino端使用SN75176
,因为this IC a RS232(UART) to (RS485)
转换器。
编辑: Modbus在串行上有2种类型(RTU
,ASCII
)。不要在意RS232 232/485的差异,因为different signal, same protocol
。
包类型示例:
:010300200004+CRC+FE
(crc =数据包检查代码,fe=end标记) RTU : rtu上的\x01\x03\x00\x20\x00\x04\x +CRC
:每个字节需要1.5个字符空间,没有start and end
标记。
Modbus node type
的意思是master
和slave
。
我希望能帮上忙。
https://stackoverflow.com/questions/35042887
复制相似问题