Pages: [1]   Go Down
Author Topic: Serial issues.  (Read 118 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 32
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have sent and parsed a string over serial and then putting this string into the DMX master library, however i keep getting some random things happening, it would appear that when a value is sent between 10-100 it is moving over to another part of the dmx master library and giving some very bizarre results. I'm hoping someone can look over my code below and see if anything stands out as to why i may be getting these un expected results.

Code:
#include <DmxMaster.h>


char buffer[256];

String st, channel, msgvalue;

void setup()
{
      pinMode(13, OUTPUT);
      Serial.begin(115200);
      while(Serial.available())
      Serial.read();
     
}

void loop() {
        if (Serial.available() > 0) {
                int index=0;
                delay(300); // let the buffer fill up
                int numChar = Serial.available();
                if (numChar>180) {
                        numChar=180;
                }
                while (numChar--) {
                buffer[index++] = Serial.read();
                }
                splitString(buffer);
                         
        }
}

void splitString(char* data) {

Serial.print("Data entered: ");
Serial.println(data);
char* parameter;
parameter = strtok (data, ":"); // Note that this is a space before the comma in " , "
while (parameter != NULL) {
 setLED(parameter);
 parameter = strtok (NULL, ":"); // space before the comma in " , "
}


// Clear the text and serial buffers
for (int x=0; x<180; x++) {
        buffer[x]='\0';
}
while(Serial.available())
        Serial.read();}
void setLED(char* data) {
        if ((data[0] == 'a') || (data[0] == 'A')) {
                int Ans = strtol(data+1, NULL, 10);
                Ans = constrain(Ans,0,255);
                int c1val=Ans; 
                Serial.print("C1 = ");
                Serial.println(c1val);
                DmxMaster.write(1,c1val);

        }
        if ((data[0] == 'b') || (data[0] == 'B')) {
                int Ans = strtol(data+1, NULL, 10);
                Ans = constrain(Ans,0,255);
                int c2val=Ans;
                Serial.print("C2 = ");
                Serial.println(Ans);
                DmxMaster.write(2,c2val);
        }
        if ((data[0] == 'c') || (data[0] == 'C')) {
                int Ans = strtol(data+1, NULL, 10);
                Ans = constrain(Ans,0,255);
                int c3val=Ans;
                Serial.print("C3 = ");
                Serial.println(Ans);
                DmxMaster.write(3,c3val);
}
        if ((data[0] == 'd') || (data[0] == 'D')) {
                int Ans = strtol(data+1, NULL, 10);
                Ans = constrain(Ans,0,255);
                int c4val=Ans;
                Serial.print("C4 = ");
                Serial.println(Ans);
                DmxMaster.write(4,c4val);
        }
        if ((data[0] == 'e') || (data[0] == 'E')) {
                int Ans = strtol(data+1, NULL, 10);
                Ans = constrain(Ans,0,255);
                int c5val=Ans;
                Serial.print("C5 = ");
                Serial.println(Ans);
                DmxMaster.write(5,c5val);
        }
        if ((data[0] == 'f') || (data[0] == 'F')) {
                int Ans = strtol(data+1, NULL, 10);
                Ans = constrain(Ans,0,255);
                int c6val=Ans;
                Serial.print("C6 = ");
                Serial.println(Ans);
                DmxMaster.write(6,c6val);
        }       
         if ((data[0] == 'g') || (data[0] == 'G')) {
                int Ans = strtol(data+1, NULL, 10);
                Ans = constrain(Ans,0,255);
                int c7val=Ans;
                Serial.print("C7 = ");
                Serial.println(Ans);
                DmxMaster.write(7,c7val);
        }
        if ((data[0] == 'h') || (data[0] == 'H')) {
                int Ans = strtol(data+1, NULL, 10);
                Ans = constrain(Ans,0,255);
                int c8val=Ans;
                Serial.print("C8 = ");
                Serial.println(Ans);
                DmxMaster.write(8,c8val);
        }
        if ((data[0] == 'i') || (data[0] == 'I')) {
                int Ans = strtol(data+1, NULL, 10);
                Ans = constrain(Ans,0,255);
                int c9val=Ans;
                Serial.print("C9 = ");
                Serial.println(Ans);
                DmxMaster.write(9,c9val);
        }         
        if ((data[0] == 'j') || (data[0] == 'J')) {
                int Ans = strtol(data+1, NULL, 10);
                Ans = constrain(Ans,0,255);
                int c10val=Ans;
                Serial.print("C10 = ");
                Serial.println(Ans);
                DmxMaster.write(10,c10val);
        }
        if ((data[0] == 'k') || (data[0] == 'K')) {
                int Ans = strtol(data+1, NULL, 10);
                Ans = constrain(Ans,0,255);
                int c11val=Ans;
                Serial.print("C11 = ");
                Serial.println(Ans);
                DmxMaster.write(11,c11val);
        }
        if ((data[0] == 'l') || (data[0] == 'L')) {
                int Ans = strtol(data+1, NULL, 10);
                Ans = constrain(Ans,0,255);
                int c12val=Ans;
                Serial.print("C12 = ");
                Serial.println(Ans);
                DmxMaster.write(12,c12val);
        }       
         if ((data[0] == 'm') || (data[0] == 'M')) {
                int Ans = strtol(data+1, NULL, 10);
                Ans = constrain(Ans,0,255);
                int c13val=Ans;
                Serial.print("C13 = ");
                Serial.println(Ans);
                DmxMaster.write(13,c13val);
        }
        if ((data[0] == 'n') || (data[0] == 'N')) {
                int Ans = strtol(data+1, NULL, 10);
                Ans = constrain(Ans,0,255);
                int c14val=Ans;
                Serial.print("C14 = ");
                Serial.println(Ans);
                DmxMaster.write(14,c14val);
        }
        if ((data[0] == 'o') || (data[0] == 'O')) {
                int Ans = strtol(data+1, NULL, 10);
                Ans = constrain(Ans,0,255);
                int c15val=Ans;
                Serial.print("C15 = ");
                Serial.println(Ans);
                DmxMaster.write(15,c15val);
        }       
         if ((data[0] == 'p') || (data[0] == 'P')) {
                int Ans = strtol(data+1, NULL, 10);
                Ans = constrain(Ans,0,255);
                int c16val=Ans;
                Serial.print("C16 = ");
                Serial.println(Ans);
                DmxMaster.write(16,c16val);
        }
        if ((data[0] == 'q') || (data[0] == 'Q')) {
                int Ans = strtol(data+1, NULL, 10);
                Ans = constrain(Ans,0,255);
                int c17val=Ans;
                Serial.print("C17 = ");
                Serial.println(Ans);
                DmxMaster.write(17,c17val);
        }
        if ((data[0] == 'r') || (data[0] == 'R')) {
                int Ans = strtol(data+1, NULL, 10);
                Ans = constrain(Ans,0,255);
                int c18val=Ans;
                Serial.print("C18 = ");
                Serial.println(Ans);
                DmxMaster.write(18,c18val);
        }       
         if ((data[0] == 's') || (data[0] == 'S')) {
                int Ans = strtol(data+1, NULL, 10);
                Ans = constrain(Ans,0,255);
                int c19val=Ans;
                Serial.print("C19 = ");
                Serial.println(Ans);
                DmxMaster.write(19,c19val);
        }
        if ((data[0] == 't') || (data[0] == 'T')) {
                int Ans = strtol(data+1, NULL, 10);
                Ans = constrain(Ans,0,255);
                int c20val=Ans;
                Serial.print("C20 = ");
                Serial.println(Ans);
                DmxMaster.write(20,c20val);
        }
        if ((data[0] == 'u') || (data[0] == 'U')) {
                int Ans = strtol(data+1, NULL, 10);
                Ans = constrain(Ans,0,255);
                int c21val=Ans;
                Serial.print("C21 = ");
                Serial.println(Ans);
                DmxMaster.write(21,c21val);
        }       
         if ((data[0] == 'v') || (data[0] == 'V')) {
                int Ans = strtol(data+1, NULL, 10);
                Ans = constrain(Ans,0,255);
                int c22val=Ans;
                Serial.print("C22 = ");
                Serial.println(Ans);
                DmxMaster.write(22,c22val);
        }
        if ((data[0] == 'w') || (data[0] == 'W')) {
                int Ans = strtol(data+1, NULL, 10);
                Ans = constrain(Ans,0,255);
                int c23val=Ans;
                Serial.print("C23 = ");
                Serial.println(Ans);
                DmxMaster.write(23,c23val);
        }
        if ((data[0] == 'x') || (data[0] == 'X')) {
                int Ans = strtol(data+1, NULL, 10);
                Ans = constrain(Ans,0,255);
                int c24val=Ans;
                Serial.print("C24 = ");
                Serial.println(Ans);
                DmxMaster.write(24,c24val);
}
}

* DMX512_pi_arduino_fix.ino (8.41 KB - downloaded 2 times.)
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46107
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
char buffer[256];
Are you really sending this many characters?

Code:
        if (Serial.available() > 0) {
                int index=0;
                delay(300); // let the buffer fill up
                int numChar = Serial.available();
                if (numChar>180) {
                        numChar=180;
                }
This is crap. The buffer only holds 64 characters. Delaying, in the hopes that the whole packet will arrive is a fool's dream.

The packets need to be followed by an end-of-packet marker (of some sort; carriage return is not uncommon). You read and store data as it arrives (no delay()) until the end of packet marker arrives (of course, this means that index needs to be global or static). Only when the end of packet marker arrives do you use what you have stored, and reset index.

Code:
                while (numChar--) {
                buffer[index++] = Serial.read();
                }
More crap. Your char array is not NULL terminated.

Code:
String st, channel, msgvalue;
More crap. You have a char array. You do not need to wrap that char array in a String.

If it makes you feel better, substitute "less than perfect code" for crap.
Logged

0
Offline Offline
Tesla Member
***
Karma: 116
Posts: 8936
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You need to delimit the code sent somewhat like below so you don't need to use a delay.

Code:
//zoomkat 3-5-12 simple delimited ',' string 
//from serial port input (via serial monitor)
//and print result out serial port

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
    }
  }
}
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Offline Offline
Newbie
*
Karma: 0
Posts: 32
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry forgot to mention, i have resized the serial buffer to 256, i am sending around 120 characters.
You say send an end of marker character, how would i do this in  python, at the moment the string is being sent using

while True:
           ser.write(":a"+str(c1.get()))
           ser.write(":b"+str(c1.get()))

etc but how would an end of marker help or rather could you explain how it works?

I know my codes crap, but its been working fine until now, by know means am i making out to be a good programmer more of someone who has tried to teach himself, if you could give me an idea of how you would write my code that would help get me a long way, half the problem i've had is i don't know what I'm looking for, i know what i want to do and try and use what seems to work for me.
Logged

0
Offline Offline
Tesla Member
***
Karma: 116
Posts: 8936
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
how would i do this in  python

A Python forum might have answers.

Quote
how would an end of marker

It would allow the code to run faster and possibly prevent the serial in buffer from being over filled.

Quote
I know my codes crap, but its been working fine until now

Then what is your problem?

Quote
the problem i've had is i don't know what I'm looking for, i know what i want to do

That makes two of us.
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Offline Offline
Newbie
*
Karma: 0
Posts: 32
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok so i now want to read the string until the arduino detects "/n" which i can send however i want it to then repeat this process over and over so that the values are still updating, can you tell me how to do this please?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24370
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
so i now want to read the string until the arduino detects "/n"
Do you?
Or do you want it to read until the Arduino detects '\n' ?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 32
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

which ever really i thought it was just what you tell it to look for? i chose the /n because my wonderful mini pi keyboard seems to be hiding the \ somewhere else so i just went for the easiest option. Any ideas on how i can do this?
Thanks in advance
Logged

Pages: [1]   Go Up
Jump to: