Hi all,
I'm trying to program an arduino to send a slimmed-down version of incoming data on the serial port, to a softwareSerial port.
This is what I'm trying to do:
I have a lot of mobiles each sending their position to my reception unit. The reception unit will output one phrase for each mobile that it receives. It does this on a serial port at a baudrate of 115200. The syntax looks slightly like NMEA, but for some reason they have decided not to fully follow the NMEA syntax. For example, there is no checksum.
An example:
$DSM00,063943,+48.06851,+007.28909,3885.0,63.0,208.1,0F (followed by a new line 0x0D 0x0A)
The 00 is the ID. Likewise there could be a 01, 02, AA, FF,..
There is a maximum of 28 ID's at the same time. In case they are all online, there will be a gap of approximately 35 milliseconds between eacht start of a new phrase.
Since I'm using for my project only the position of ID 00 and 01, I figured it would be a good idea to have an arduino setup as a filter, to ignore all the other ID's, and then pass this new stream to the next processor, so that that one has more processing time left to handle the actual data. I'm using an Atmega328 processor.
For the moment it seems like the $DSM00 data passes correctly, however the $DSM01 doesn't pass a soon as the stream gets busy.
Since I'm an amateur programmer, I think I might have written a very poorly programmed sketch for the job which isn't very efficient. My guess would be that because of the way it's written, it loses too much time doing something and missed the next phrase which might be the one I'm actually after.
It's just a guess though, I might be completely wrong.
Could someone offer some insight on the problem and advice a better way to do so? Or would the Atmega328 just not be up to the job to handle that amount of data?
thanks a lot!
Cheers,
Jens
int numberofbytes = 0;
int timeout = 250;
unsigned long marker;
int DSM00_received = 0;
int DSM01_received = 0;
char answer[100];
int number = 0;
char DSM00[7] = "$DSM00";
char DSM01[7] = "$DSM01";
char waste;
#include <SoftwareSerial.h>
SoftwareSerial out(9,10);
void setup() {
Serial.begin(115200); //incoming data
out.begin(115200); //stripped-down output
startsetup:
if(Serial.available()>0){
afterFirstBytes:
marker = millis();
afterFirstBytes2:
if(Serial.available()>0){
waste = Serial.read(); //discard any garbage data that might have been received already
goto afterFirstBytes;
} else {
if(millis()-marker > timeout){
reset(); } else { goto afterFirstBytes2;}
}
} else {
goto startsetup;
}
}
void loop() {
if(Serial.available() >0){
number = number + 1;
answer[number] = Serial.read();
}
else {
if(number>0){
if(answer[number]==10){
DSM00_received = 0;
DSM01_received = 0;
for (int i=1;i<7;i++){ // Verifies if the received phrase starts with "DSM00", one character at a time
if (answer[i]==DSM00[i-1]){
DSM00_received++;
}
}
if(DSM00_received==6){ //if the phrase starts with "DSM00", output that phrase
for(int i=1;i<number+1;i++){
out.write(answer[i]);
}
}
else {
for(int i=0;i<7;i++){//Verifies if the received phrase starts with "DSM01", one character at a time
if(answer[i]==DSM01[i-1]){
DSM01_received++;
}
}
if(DSM01_received==6){ //if the phrase starts with "DSM01", output that phrase
for(int i=1;i<number+1;i++){
out.write(answer[i]);
}}}
number = 0;
}
}
}
}
void reset(){
numberofbytes = 0;
marker = millis();
}