Hello ,
I'm trying to collect the output of a sensor RS232 via a software serial on an Arduino 1. The output collected and the voltage on A0 will be send to a PC using standard serial.
Below my code, this is the best I can do searching on internet
#include "RTClib.h" //for timestamp
#include <SoftwareSerial.h>
SoftwareSerial portOne(10, 11); //portOne connected to external sensor
char inByte;
const byte numChars = 52; //length of string send by external sensor
char receivedChars[numChars]; // an array to store the received data
boolean newData = false;
RTC_DS1307 rtc;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
while (!Serial); // wait for serial port to connect. Needed for native USB
portOne.begin(9600);
if (! rtc.begin()) {
//Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); //got some problem with RTC I need to initialize it every time btw..
}
void loop() {
// put your main code here, to run repeatedly:
DateTime now = rtc.now();
int a=analogRead(A0); //voltage measure
recvWithEndMarker(); //routine for getting rs232 data from external sensor
Serial.print(now.timestamp(DateTime::TIMESTAMP_FULL));
Serial.print("\t");
Serial.print(a);
Serial.print("\t");
Serial.write(receivedChars);
Serial.println();
newData=false;
//delay(1000);
}
void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = '\n';
char rc;
if (portOne.available() > 0) {
while (portOne.available() > 0 && newData == false) {
rc = portOne.read();
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
}
}
}
}
Using serial monitor everything works almost fine (I've got some error reading the string from sensor).
I want to use an external software (R with Rstudio, using "serial" package) to read the output from Arduino.
I have some problem, first of all I can't collect all the data send from Arduino, viceversa I need a scanrate of 1 second. I cant' control this time with R so I thought to apply a delay on the Serial.print instruction using an if statement with currentmillis() (without using delay), see code:
void loop() {
// put your main code here, to run repeatedly:
recvWithEndMarker();
if(millis() - lastRefreshTime >= REFRESH_INTERVAL)
{
lastRefreshTime += REFRESH_INTERVAL;
getdata();
}
}
void getdata() {
DateTime now = rtc.now();
int a=analogRead(A0);
Serial.print(now.timestamp(DateTime::TIMESTAMP_FULL));
Serial.print("\t");
Serial.print(a);
Serial.print("\t");
Serial.write(receivedChars);
Serial.println();
newData=false;
}
but in this way the routine for reading on portOne doesn't work well.
Is there any way to "freeze" the output for 1 second without altering the reading on portOne?
By the way the external sensor send message each 0.2 second with a 52 chars string, the end of the string is marked with cr/lf.
In attachment an output from serial monitor for both sketches, see in the second one how the sensor data are shifted each loop.
Thank you for any idea