Go Down

Topic: Multiple Serial Commands c# (Read 923 times) previous topic - next topic

rizzotwinboys

I'm having trouble reading serial commands that I send with my computer using serial.

this is the arduino code.
Code: [Select]
if (Serial.available() > 6)
  {
      Serial.readBytesUntil('/', buffer, 120);
      int v = Serial.read(); //green
      int w = Serial.read();//red
      int x = Serial.read(); //green
      int y = Serial.read();//red
      int z = Serial.read();//blue
      int q = Serial.read();//blue
      analogWrite(6, x);
      analogWrite(5, y);
      analogWrite(3, z);
      analogWrite(11, q);
      analogWrite(10, v);
      analogWrite(9, w);
   
  }



here is the c# code that sends the data

Code: [Select]
public void serialwrite()
        {
            slider1 = BitConverter.GetBytes(trackBar4.Value);
            slider2 = BitConverter.GetBytes(trackBar5.Value);
            slider3 = BitConverter.GetBytes(trackBar6.Value);
            slider4 = BitConverter.GetBytes(trackBar7.Value);
            slider5 = BitConverter.GetBytes(trackBar8.Value);
            slider6 = BitConverter.GetBytes(trackBar9.Value);
            serialPort1.Write("/");
            serialPort1.Write(slider1, 0, 1);
            serialPort1.Write(slider2, 0, 1);
            serialPort1.Write(slider3, 0, 1);
            serialPort1.Write(slider4, 0, 1);
            serialPort1.Write(slider5, 0, 1);
            serialPort1.Write(slider6, 0, 1);
        }



Any help that you could provide would be greatly appreciated.

I'm looking to get the arduino to recieve a value from the trackbar between 0-255 and then set those value to the leds.  The problem i have is that an led will randomly turn on and then off quickly.

PaulS

You check to see if there is at least one byte to read. Then, you try to read at least 7 bytes. Clearly, that won't work. You should use start and end markers for your packets, then read and store all the data until the end of packet marker arrives. Only then should you try to parse the data.

Code: [Select]

                                                                                                                                [code]
#define SOP '<'
#define EOP '>'

bool started = false;
bool ended = false;

char inData[80];
byte index;

void setup()
{
   Serial.begin(57600);
   // Other stuff...
}

void loop()
{
  // Read all serial data available, as fast as possible
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
      if(index < 79)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet

    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
}

This code will read and store all the data, if you use < and > as the start and end markers.

Where it says "Process the packet" is where you would try to make use of the data received.[/code]

zoomkat

#2
Jan 18, 2013, 07:18 pm Last Edit: Jan 18, 2013, 07:24 pm by zoomkat Reason: 1
make sure your pc code does not open/close the comport each time it sends a command. doing so will cause the arduino to reset each time. simple serial monitor test code.

Code: [Select]

// zoomkat 7-30-11 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later

String readString;

void setup() {
  Serial.begin(9600);
  Serial.println("serial test 0021"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    delay(2);  //delay to allow byte to arrive in input buffer
    char c = Serial.read();
    readString += c;
  }

  if (readString.length() >0) {
    Serial.println(readString);

    readString="";
  }
}


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

rizzotwinboys

Thank you PaulS. Your code helped me get my program to work. :)

Go Up