Arduino uno串口通讯 [英] Arduino uno serial communication

查看:167
本文介绍了Arduino uno串口通讯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi
ia使用arduino uno将数据串行发送到visual basic。

问题是它不断发送数据。

i希望arduino发送数据只有一次。

请帮助



我尝试过:



#include< softwareserial.h>

/ * -----(声明常量和引脚号)----- * /

#define SSerialRX 10 //串行接收引脚

#define SSerialTX 11 //串行发送引脚



#define SSerialTxControl 3 / / RS485方向控制

#define RS485Transmit HIGH

#define RS485Receive LOW

int ledpin = 13;

/ * -----(声明对象)----- * /

SoftwareSerial RS485Serial(SSerialRX,SSerialTX); // RX,TX



/ * -----(声明变量)----- * /

int byteReceived;

int byteSend;



void setup()/ ****** SETUP:RUNS ONCE ****** /

{

//启动内置串口,可能是串口监视器

Serial.begin(9600);

Serial.println(SerialRemote); //可以忽略

pinMode(SSerialTxControl,OUTPUT);

digitalWrite(SSerialTxControl,RS485Receive); //初始收发器



//启动软件串口到另一台设备

RS485Serial.begin(9600); //设置数据速率

pinMode(ledpin,OUTPUT);

digitalWrite(ledpin,LOW);

} // - (结束设置)---





void loop()/ ******循环:运行不变***** * /

{

//将输入数据复制到输出

if(RS485Serial.available())

{

digitalWrite(ledpin,HIGH);

byteReceived = RS485Serial.read(); //读取字节

Serial.write(byteReceived); //在串口监视器上显示





}

}





} // - (结束主循环)---



/ * ----- (声明用户编写的函数)----- * /

//无人



// ******* **(结束)***********

解决方案

如果您只想发送一次数据,那么您应该将代码放入void setup(){}循环,因为它只会被执行一次。


它因为这行而连续发送数据

 Serial.write(byteReceived); <来自循环方法的/ pre>。循环方法一次又一次地执行...就是那里的东西。

如果你想要只执行一次,把那个代码行放在setup中,或者如果不可能的话出于任何原因,只需创建一个控制其执行的标志,因此它只在循环中执行一次。


嗯,在setup()中放置代码不是一件好事。将if()改为其他RS485.available()这就是为什么这是为什么因为你的设备不断发送数据发送

数据你可以向设备发送命令以保持安静。如果您的程序已收到数据,请使用Bool进行检查和设置。

Quote:

  bool  bSet = ; 
-----
void loop {

if (bSet == false )&& (RS485Serial.available == true
{
bSet = true ; // 也阻止重新触发
Serial.write(byteReceived);
}
}





或用户定义函数循环后的内容如下:



Quote:

  bool  bSet =  false ; 
-----
void loop {

if (bSet == false )&& (RS485Serial.available == true
{
bSet = true ; // 也阻止重新触发
ReadMe();
}
}

}
}


} // - (结束主循环)---

/ * -----(声明用户编写的函数)----- * /
void ReadMe(){

Serial.write(byteReceived); // 在串行监视器上显示

}


hi i a using arduino uno to send data serially to visual basic.
the problem is it keeps sending the data continuously.
i want the arduino to send data only once.
please help

What I have tried:

#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;
/*-----( 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


}
}


}//--(end main loop )---

/*-----( Declare User-written Functions )-----*/
//NONE

//*********( THE END )***********

解决方案

If you want to send the data just once then you should place your code in void setup(){} loop because it will be executed only once.


It send data continuously because of this line

Serial.write(byteReceived);

from your "loop" method. The loop method executes again and again...is just what is there for.
If you want this to be executed just once, put that code line in "setup" or, if that is not possible for any reason, then just make a flag that control its execution, so it only executes once in the "loop".


Ummm, not a good thing to place code in setup(). change the if() to something other that the RS485.available() this is why because your device is sending data constantly sending
data you could send a command to the device to be quiet. If use a Bool to check and set once your program has recieved the data.

Quote:

bool bSet = false;
-----
void loop{

if (bSet == false) && (RS485Serial.available == true)
{
   bSet = true; // too prevent retrigger
   Serial.write(byteReceived); 
}
}



or after the loop in user defined functions something like:

Quote:

bool bSet = false;
-----
void loop{

if (bSet == false) && (RS485Serial.available == true)
{
   bSet = true; // too prevent retrigger
   ReadMe(); 
}
}

}
}


}//--(end main loop )---

/*-----( Declare User-written Functions )-----*/
void ReadMe(){

Serial.write(byteReceived); // Show on Serial Monitor

}


这篇关于Arduino uno串口通讯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆