Pages: [1]   Go Down
Author Topic: controlling c# game using arduino buttons  (Read 1062 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Code:
//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();
        }
    }
}
Logged

Chile
Offline Offline
Edison Member
*
Karma: 35
Posts: 1251
Arduino rocks?
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Everything seems fine, this is missing:

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

                default:
                    // other
                    break;
            }
        }
Logged

My website: http://ried.cl

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

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

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

Quote
doesn't seem to work
Useless response.

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

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

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

Chile
Offline Offline
Edison Member
*
Karma: 35
Posts: 1251
Arduino rocks?
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

My website: http://ried.cl

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

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.
Code:
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)
        {}
Logged

Chile
Offline Offline
Edison Member
*
Karma: 35
Posts: 1251
Arduino rocks?
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

My website: http://ried.cl

Pages: [1]   Go Up
Jump to: