Show Posts
Pages: [1]
1  Using Arduino / Interfacing w/ Software on the Computer / Re: Serial Communication using Arduino and C# on: November 11, 2012, 02:08:12 pm
Hi Paul!

Quote
It is NOT C code. It is C++ code, and String is a class that DOES have those members.

Can you show me an example using an string in arduino code with string classes? Can you replace my arduino code for other else using those string methods that you said before like .toInt(), for example? or show me some link that show some example of arduino code using classes. Like i said, i'm new in arduino's world, i'll apreciate that because i didn't find yet any arduino code using classes.


Thanks again for your reply.
 
2  Using Arduino / Interfacing w/ Software on the Computer / Re: Serial Communication using Arduino and C# on: November 11, 2012, 06:58:27 am
The problem is solved. Thanks anyway.  The problem is that in C# is necessary to concatenate the Null Char ('\0') to your string otherwise arduino could not recognize where the string ends.
Can anyone tell me how can i see what im sending to arduino once i cant use the arduino's serial monitor because it's already being used by visual studio.
3  Using Arduino / Interfacing w/ Software on the Computer / Re: Serial Communication using Arduino and C# on: November 11, 2012, 06:00:40 am
Hi PaulS,
First of all, thanks for your reply.

Quote
Why isn't the range of the Fadebar simply 0 to 255? Then, there would be no reason to multiply by 10.
My scroll bar have a maximum of 10. So, it goes to maximum of 100. Sure it is better made all to 255, but i believe it is not a big problem.

Quote
The String class, which has documented issues, has a toInt() method. Why aren't you using it?
The String class has a toCharArray() method. Why aren't you using it?
Like i said before this is arduino code (C code). So i include stdlib.h which i believe it dont have such functions.
Quote
How is the Arduino supposed to know when the integer value, converted to a string, has been fully received?
In C code, '\0' is the final mark that a string have by default. Falling your example the string received by arduino is  {'140\0'}. You can see how i stop fill my number in arduino code (C code) that i posted.

Quote
Yes, but it is far better to not write buggy code in the first place.
My problem is how can i see what im sending to arduino. How can i print on the serial monitor of arduino software?
4  Using Arduino / Interfacing w/ Software on the Computer / Serial Communication using Arduino and C# on: November 10, 2012, 08:01:15 pm
Hi everyone,

I want to make an simple program which will help me understand how to communicate with arduino using C# code. The program consists in fade in/out  a LED by changing  an brightness value using a Scroll bar. See in this image.



In C# code first i defined my serial port.
Code:
private void Portname_SelectedIndexChanged(object sender, EventArgs e)
        {
            serialPort1.BaudRate = 9600;
            serialPort1.PortName = Portname.Text;
            try
            {
                serialPort1.Open();
           
            }
            catch (Exception ex)
            {   MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Error);
           
            }
then i designed my fade barscroll to balance my fade value and comunicate this value to arduino.

Code:
private void Fadebar_Scroll(object sender, EventArgs e)
        {
            int value = Fadebar.Value * 10;
   
            serialPort1.Write(Convert.ToString(value));
        }


Finally i use this arduino code:

Code:
#include<stdlib.h>


int ledPin = 9;    // LED connected to digital pin 9
String incomingstring = "";
int fadeValue = 0;

int stringToNumber(String thisString) {
  int i, value, length;
  length = thisString.length();
  char new[(length)];
  for(i=0; i<length; i++) {
    new[i] = thisString.charAt(i);
  }

  value = atoi(blah);
  return value;
}
void setup()  {
  Serial.begin(9600);
  pinMode(ledPin,OUTPUT);
}

void loop()  {
 if (Serial.available()>0){
 char incomingbyte =(char)Serial.read();
 incomingstring += incomingbyte;
 // Checks for null termination of the string.
    if (incomingbyte == '\0') {
      int fadeValue = stringToNumber(incomingstring);
      analogWrite(ledPin,fadeValue);
        // Serial.print(fadeValue);
       
      incomingstring = "";
    }
         
 }
}

Why doesnt work?
1. How can i send an int value through an serialport?
2. How can i print my fadeValue (Serial.print(fadeValue)) ? It appears "Serial Port "COM6" is already in use...".
3. Can i debug arduino code?
Pages: [1]