使用rs485进行主从通信 [英] Master and slave communication using rs485
本文介绍了使用rs485进行主从通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
hi
i我的应用程序我有两个arduino用作主人和奴隶。
当我的奴隶arduino开启时它必须向主人arduino发送消息。
i能够实现这个但问题是我希望消息只发送一次,就像一旦led发送消息然后停止。
rs485用于消息传输。
我尝试过:
主码
hi
i my application i have two arduino used as master and slave.
when my led on slave arduino turns on it has to send message to master arduino.
i am able to achieve this but the problem is i want the message to be sent only once,like once led is on send message then stop.
rs485 is used for message transmission.
What I have tried:
master code
#include <softwareserial.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define SSerialRX 10 //Serial Receive pin
#define SSerialTX 11 //Serial Transmit pin
#define SSerialTxControl 3 //RS485 Direction control
#define RS485Transmit HIGH
#define RS485Receive LOW
int ledpin = 13;
int data;
int data1;
int ack = 0;
/*-----( Declare objects )-----*/
SoftwareSerial RS485Serial(SSerialRX, SSerialTX); // RX, TX
/*-----( Declare Variables )-----*/
int byteReceived;
int byteSend;
void setup() /****** SETUP: RUNS ONCE ******/
{
// Start the built-in serial port, probably to Serial Monitor
Serial.begin(9600);
Serial.println("SerialRemote"); // Can be ignored
pinMode(SSerialTxControl, OUTPUT);
digitalWrite(SSerialTxControl, RS485Receive); // Init Transceiver
// Start the software serial port, to another device
RS485Serial.begin(9600); // set the data rate
pinMode(ledpin,OUTPUT);
digitalWrite(ledpin,LOW);
}//--(end setup )---
void loop() /****** LOOP: RUNS CONSTANTLY ******/
{
//Copy input data to output
if (RS485Serial.available())
{
digitalWrite(ledpin,HIGH);
byteReceived = RS485Serial.read(); // Read the byte
Serial.write(byteReceived); // Show on Serial Monitor
delay(15);
digitalWrite(SSerialTxControl, RS485Transmit);
}
else{}
digitalWrite(SSerialTxControl, RS485Receive);
//delay(100);
//digitalWrite(SSerialTxControl, RS485Transmit);
}
/*if(byteReceived == 2){
digitalWrite(SSerialTxControl, RS485Receive);
data1 = RS485Serial.read();
Serial.write(data1);
digitalWrite(SSerialTxControl, RS485Transmit);
}
else {};
delay(100);
// End If RS485SerialAvailable
digitalWrite(SSerialTxControl, RS485Receive);
}//--(end main loop )---
/*-----( Declare User-written Functions )-----*/
//NONE
//*********( THE END )***********
奴隶代码
slave code
#include <softwareserial.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define SSerialRX 10 //Serial Receive pin
#define SSerialTX 11 //Serial Transmit pin
#define SSerialTxControl 4 //RS485 Direction control
#define RS485Transmit HIGH
#define RS485Receive LOW
SoftwareSerial RS485Serial(SSerialRX, SSerialTX); // RX, TX
int byteReceived;
int byteSend;
volatile unsigned long LastPulseTimeA;
volatile unsigned long LastPulseTimeB;
int durationA;
int durationB;
int distanceA;
int distanceB;
//unsigned long startTime;
#define trigPinA 7
#define echoPinA 2
#define trigPinB 8
#define echoPinB 3
#define ledpinA 13
#define ledpinB 12
int val = 1;
int val1 = 1;
int temp = 0;
int temp1 = 0;
int slave1=1;
int slave2 = 2;
void setup() {
Serial.begin (9600);
pinMode(trigPinA, OUTPUT);
pinMode(echoPinA, INPUT);
pinMode(trigPinB, OUTPUT);
pinMode(echoPinB, INPUT);
pinMode(ledpinA,OUTPUT);
pinMode(ledpinB,OUTPUT);
attachInterrupt(0, EchoPinA_ISR, CHANGE); // Pin 2 interrupt on any change
attachInterrupt(1, EchoPinB_ISR, CHANGE); // Pin3 interrupt on any change
digitalWrite(ledpinA,LOW);
digitalWrite(ledpinB,LOW);
Serial.begin (9600);
pinMode(SSerialTxControl, OUTPUT);
digitalWrite(SSerialTxControl, RS485Transmit); // Enable RS485 Transmit
RS485Serial.begin(9600);
}
void loop(){
digitalWrite(trigPinA, LOW);
digitalWrite(trigPinB, LOW);
delayMicroseconds(2);
digitalWrite(trigPinA, HIGH);
digitalWrite(trigPinB, HIGH);
delayMicroseconds(10);
digitalWrite(trigPinA, LOW);
digitalWrite(trigPinB, LOW);
if (distanceA<10)
{
Serial.print("Sensor A ");
Serial.print(distanceA);
Serial.print('\t');
digitalWrite(ledpinA,HIGH);
}
else{
digitalWrite(ledpinA,LOW);
}
if (distanceB<10)
{
Serial.print("Sensor B ");
Serial.print(distanceB);
Serial.print('\t');
digitalWrite(ledpinB,HIGH);
}
else{
digitalWrite(ledpinB,LOW);
}
temp = digitalRead(ledpinA);
//temp1 = digitalRead(ledpinB);
if(temp==val){
digitalWrite(SSerialTxControl, RS485Transmit);
RS485Serial.write("car A present ");
digitalWrite(SSerialTxControl, RS485Receive);
}
else{}
digitalWrite(SSerialTxControl, RS485Transmit);
// if(temp1==val1){
//digitalWrite(SSerialTxControl, RS485Transmit);
//RS485Serial.write("car B present ");
// digitalWrite(SSerialTxControl, RS485Receive);
// }
// else{}
// digitalWrite(SSerialTxControl, RS485Transmit);
}//loop end
void EchoPinA_ISR() {
static unsigned long startTimeA;
if (digitalRead(2)) // Gone HIGH
startTimeA = micros();
else // Gone LOW
LastPulseTimeA = micros() - startTimeA;
distanceA=(LastPulseTimeA/2) / 29.1,1;
}
void EchoPinB_ISR() {
static unsigned long startTimeB;
if (digitalRead(3)) // Gone HIGH
startTimeB = micros();
else // Gone LOW
LastPulseTimeB = micros() - startTimeB;
distanceB=(LastPulseTimeB/2) / 29.1,1;
}
推荐答案
在此代码中,val设置为1,这样每次ledPinA变为HIGH时,您将重复发送汽车礼物的消息,除非我在这里遗漏了什么。当你第一次发送消息并在ledPinA变为高电平时设置标志并在低电平时重置然后检查代码中的标志时,设置一个标志。
In this code val is set to 1 so that every time the ledPinA goes HIGH you will repeatedly send the "car A present" message, unless I'm missing something here. To over come this set a flag when you 1st send the message and set the flag when the ledPinA goes HIGH and reset when it goes LOW then check the flag in your code.
temp = digitalRead(ledpinA);
//temp1 = digitalRead(ledpinB);
if(temp==val && !flag){
...
flag = true;
} else {
flag = false;
}
更改别针
尝试带中断的Pin3
#定义SSerialTxControl 3
请检查中断,微,毫,延迟()不起作用!
Change pin
Try Pin3 with interrupt
#define SSerialTxControl 3
Please check with interrupt, micro, millis, delay() does nor work!
这篇关于使用rs485进行主从通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文