Hi, i'm working on a sketch to read serial data. It works however when i send duplicate characters I get funny data returning.
EX
If i send:
123456789012345678901234567890
I get:
123456789012345678901234567890
however if i send:
111111111222222222333333333333344444444444
I get:
11±1±1±1±22²2²2²2²233³3³3³3³344´4´4´4´4´
I have tried several things like adding time delays, removing libraries that may have conflicted different serial data sources. all with no improvement. Also I can only get 63 characters despite changing the buffer size with this command
#define _NewSS_MAX_RX_BUFF 128
any help would be appreciated
#include <NewSoftSerial.h>
NewSoftSerial mySerial(4, 5);
//#include <serialGLCD.h>
#define _NewSS_MAX_RX_BUFF 128 // RX buffer size
char string[128];
int count2 = 0;
int availableBytes;
void setup(){
// initialize the serial communications:
Serial.begin(9600);
mySerial.begin(9600);
pinMode(8,OUTPUT); //max 485 power
pinMode(13,OUTPUT); //system latch
pinMode(10,OUTPUT); // 485 write enable
delay(1000);
digitalWrite(8, 1);
digitalWrite(13, 1);
digitalWrite(10, 0);
delay(1000);
}
// serialGLCD lcd; // initialisation
void loop()
{
Serial.flush(); // clear buffer
while(mySerial.available() == 0) // wait for new data
{
}
delay(500); // wait for all data
availableBytes = mySerial.available();
for(int i=0; i<availableBytes; i++)
{
delay(2);
string[i] = mySerial.read(); //read into array for later processing
}
while(count2 < availableBytes)
{
delay(5);
Serial.print(string[count2]); // display for testing
count2 = count2+1;
}
count2 = 0;
}
Serial test code that doesn't seem to have issues with your repeating string (requires ending the string with a , to signal the end of the string).
//zoomkat 3-5-12 simple delimited ',' string parce
//from serial port input (via serial monitor)
//and print result out serial port
// CR/LF could also be a delimiter
String readString;
void setup() {
Serial.begin(9600);
Serial.println("serial delimit test 1.0"); // so I can keep track of what is loaded
}
void loop() {
//expect a string like wer,qwe rty,123 456,hyre kjhg,
//or like hello world,who are you?,bye!,
if (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
if (c == ',') {
//do stuff
Serial.println(readString); //prints string to serial port out
readString=""; //clears variable for new input
}
else {
readString += c; //makes the string readString
}
}
}
Thanks for the advice, I will try some of this other code tonight when im off work, I have tried without the delays and the flush. I added them later to try and solve the issue. The data im eventually going to try and read is in WITS format. meaning each peice of data is delimited with a carrage return and newline. Is there a way to chage the end line charicter to a carrage return or newline instead of a "," or "#"?
OK, so the problem was actually 3.7 volts driving my max 485 , it dragged down my whole buss. which is why testing with another device it also failed (same buss).
In regards to looking for a carage return or new line I used : if (c == 13)
/*
Example of processing incoming serial data without blocking.
Author: Nick Gammon
Date: 13 November 2011
Released for public use.
*/
void setup()
{
Serial.begin(9600);
}
const unsigned int MAX_INPUT = 20;
char input_line [MAX_INPUT];
unsigned int input_pos = 0;
void loop()
{
if (Serial.available () > 0)
{
char inByte = Serial.read ();
switch (inByte)
{
case '\n': // end of text
input_line [input_pos] = 0; // terminating null byte
// terminator reached! process input_line here ...
Serial.println (input_line);
// reset buffer for next time
input_pos = 0;
break;
case '\r': // discard carriage return
break;
default:
// keep adding if not full ... allow for terminating null byte
if (input_pos < (MAX_INPUT - 1))
input_line [input_pos++] = inByte;
break;
} // end of switch
} // end of incoming data
// do other stuff here like testing digital input (button presses) ...
} // end of loop