controlling c# game using arduino buttons

i need help on interfacing my c# game with arduino, i've programmed my arduino to send a serial character to the serial port and i want my c# program to read this character and generate an event based on the character read. for example when a press a push button from my arduino the character 'E' is sent, then when this character is read from the c# program, the start button is clicked. here is my code for the arduino and c# program. i'm really confused about how serial communication works on c#, i hope somebody can help me on this.

//For Arduino
int buttonPinEnt = 2;   
int buttonState1= LOW;    

void setup() {

  Serial.begin(9600);   
  pinMode(buttonPinEnt, INPUT);     
}

void loop () {

  delay(30);
  buttonState1 = digitalRead(buttonPinEnt);
      if (buttonState1 == HIGH) {
        Serial.println('E');
      }
}

//For C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;



namespace picp
{
    
    public partial class Form1 : Form
    {
        Form2 secondForm;
        string RxString;
            
        public Form1()
        {
            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            serialPort1.PortName = "COM3";
            serialPort1.BaudRate = 9600;
            serialPort1.Open();
        }

        private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            RxString = serialPort1.ReadExisting();

        }

        private void Start_Click(object sender, EventArgs e)
        {
                secondForm = new Form2();
                secondForm.Show();
                this.Hide();
        }

        private void Exit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}

Everything seems fine, this is missing:

private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            switch (serialPort1.ReadChar())
            {
                case 'E':
                    // do something
                    break;

                default:
                    // other
                    break;
            }
        }

doesn't seem to work, i added the code:

private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
switch (serialPort1.ReadChar())
{
case 'e':
Start.PerformClick();
break;
}
}

doesn't seem to work

Useless response.

What did you read from the serial port? How do you KNOW it was an 'e'?

since, i've programmed my arduino to send the character 'e' to the serial port, i'm assuming that when c# reads from the serial port , it will be the same character. i'm making my arduino act as keyboard, emulating the arrow keys and the enter key.

chae_sb:
since, i've programmed my arduino to send the character 'e' to the serial port, i'm assuming that when c# reads from the serial port , it will be the same character. i'm making my arduino act as keyboard, emulating the arrow keys and the enter key.

The thing is you need to do something extra on your side, add a breakpoint in the receive event, or test something else. The .net debugger is very powerfull you can edit, evaluate and do almost anything while debugging to solve your issues.

hi, i managed to make the next form appear using a background worker, now my problem is that the next form freezes while loading. also the event from the previous form is still in effect in the succeeding forms, for example in the first form i set up 'd' to exit the program, in the next form i set it up to function as down key, but it still functions as exit. am i using the background worker incorrectly.

Form2 nform;
        SerialPort port;
        char input1;
        BackgroundWorker back;
            
        public Form1()
        {
            InitializeComponent();
            back = new BackgroundWorker();
            back.WorkerReportsProgress = true;
            back.DoWork += new DoWorkEventHandler(back_DoWork);
            back.ProgressChanged += new ProgressChangedEventHandler(back_ProgressChanged);
            back.RunWorkerCompleted += new   RunWorkerCompletedEventHandler(back_RunWorkerCompleted);
            input1 = '\0';
            port = new SerialPort("COM3", 9600, Parity.None, 8, StopBits.One);
            port.WriteTimeout = 500;
            try
            {
                if (port.IsOpen)            //if port is open
                {
                    port.Close();
                    port.Open();
                }
                else
                    port.Open();
                if (!this.back.IsBusy)
                {
                    this.back.RunWorkerAsync(360000);
                }
            }
            catch
            {
                Debug.WriteLine("The port cannot be opened!");
            }
        }

        void back_DoWork(object sender, DoWorkEventArgs e)
        {
            while (true)
            {
                try
                {
                    //read the input
                    input1 = (char)port.ReadChar();
                    if (input1 == 'e')
                    {
                        nform = new Form2();
                        nform.Show();
                        this.Hide();
                    }
                    else if (input1 == 'x')
                    {
                        Application.exit();
                    }
                }
                catch
                {
                }
            }
        }
        void back_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {}
        void back_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {}

Just don't use a background worker for the first version. You need to make it simpler. DoWork runs in a thread different than the one handling UI stuff, you can't make threads talk safely without something else (InvokeRequired and callbacks) and the worker will eat the exceptions making exe complex to debug.

If you have visual studio 2010, use IntelliTrace (set it to record all calls) to find the problems.