Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 )***********
Posted
Updated 7-Feb-18 4:28am

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.
 
Share this answer
 
Comments
[no name] 16-Oct-16 7:15am    
You mean do it just like solution 1 said?
It send data continuously because of this line
C++
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".
 
Share this answer
 
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:
C++
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:
C++
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

}
 
Share this answer
 
v4
Comments
Dave Kreskowiak 7-Feb-18 12:42pm    
Two year old question?
glennPattonWork3 7-Feb-18 12:59pm    
Darn! clicked Q&A by accident saw a question that looked interesting, saw I could answer it did & then looked at the date!
Dave Kreskowiak 7-Feb-18 13:48pm    
I figured as much. I've done it too.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900