Send byte to arduino through c#

I have an application in c#, with 2 buttons. I want to turn on a led when I click on the "On" button, and turn off the led, when I click on the "Off button". My problem is that I can't turn on the led. Here is the code that I'm using:

C#

public tempreaderform()
        {
            InitializeComponent();
            if (comport.connect(9600, "I'M ARDUINO", 4, 8, 16))
            {
                labelconnection.Text = "Connection Successful - Connected to  " + comport.port;
                portConnection = true;                
            }
            else
            {
                labelconnection.Text = "Not connected . . . ";
                portConnection = false;

            }
        }

        SerialPort port;
        communicator comport = new communicator();
        Boolean portConnection = false;
        private void button1_Click(object sender, EventArgs e)
        { 
            
        }

        private void tempreader_Tick(object sender, EventArgs e)
        {
            labeltemp.Text =  comport.message(4, 8, 32)+ "C";
        }
        
        private void off_Click(object sender, EventArgs e)
        {            
                SerialPort port = new SerialPort(comport.port);
                port.Open();
                port.Write(new byte[] {48},0,1);
                port.Close();
                //port = new SerialPort(comport.port, 9600);
                //port.Write(new byte[] { 48 }, 0, 1);
                
            
        }

        private void on_Click(object sender, EventArgs e)
        {            
                SerialPort port = new SerialPort(comport.port);
                port.Open();
                port.Write(new byte[] { 49 }, 0, 1);
                port.Close();
           
        }
        

       
        
    }

and the arduino code

//Setup message bytes
byte inputByte_0;
byte inputByte_1;
byte inputByte_2;
int reading;
int pin = 31;

void setup() {
  Serial.begin(9600);
  pinMode(pin,OUTPUT);
}

void loop() {
  inputByte_0 = Serial.read();
    delay(10);    
    inputByte_1 = Serial.read();
    delay(10);      
    inputByte_2 = Serial.read();
    delay(10);
   if(inputByte_0 == 4)
  {       
       //Detect Command type
       switch (inputByte_1) 
       {
          case 8:
             //Set PIN and value
             switch (inputByte_2)
            {
              case 16:
                Serial.print("I'M ARDUINO");
                break;
              case 32:               
                  if(Serial.read()==(byte)49)
                  digitalWrite(pin,HIGH);
                  if(Serial.read()==(byte)48)
                  digitalWrite(pin,LOW);
                  break;
            }
       }
  }
        //Clear Message bytes
        inputByte_0 = 0;
        inputByte_1 = 0;
        inputByte_2 = 0;
  }

Please help me fix my problem. Thanks.

First thing I would do is use something on the windows side like Putty (terminal.) When you can use that to turn the LED on, then work on the C# program. Try to get one program working at a time.

I haven't done anything with Serial input yet but some of the sketches I've seen check for characters first. Tale a look at the SerialEvent example:

/*
  SerialEvent occurs whenever a new data comes in the
 hardware serial RX.  This routine is run between each
 time loop() runs, so using delay inside loop can delay
 response.  Multiple bytes of data may be available.
 */
void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read();
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == '\n') {
      stringComplete = true;
    }
  }
}

It is important that your PC program only opens the serial port once and keeps it open until you are completely finished with the Arduino. Everytime the port is opened the Arduino will reset. The PC program must also allow time for the reset process before it starts sending data to the PC.

See how this is managed in this Python demo. The principles apply in any language. It might be a good learning idea to write a C# program that takes the place of my Python code while using my Arduino code unchanged. That way you know you know the problems are only in your PC code.

The examples in serial input basics may also be of interest.

...R

I was wondering if its possible to use:

comport.connect(9600,"",49,0,0);
//and
comport.connect(9600,"",48,0,0);

to send the bytes to the arduino, from c#? I need to know how to write data to the arduino, since I can't find a write to port in the methods that are available after I use connect. Can someone please help me? Thanks.

Take a look at the SerialPort class: serialport

            if (comport.connect(9600, "I'M ARDUINO", 4, 8, 16))

Does your PC really have a port named I'M ARDUINO?

nando88:
I was wondering if its possible to use:

comport.connect(9600,"",49,0,0);

//and
comport.connect(9600,"",48,0,0);

NO.
It looks like you did not read the first part of Reply #2

Did you read the rest of it?

…R