Click here to Skip to main content
15,900,461 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i need to send joystick axis data using serial communication, and i have some problems since this is first time im using C# in windows form. On the bottom of the code i have a function called: public void sending, but when i compile the program is not executing that function. so what i have to do to execute it.



C#
namespace JoystickSample
{

   
    public partial class frmMain : Form
    {
        
        private JoystickInterface.Joystick jst;
       
        public frmMain()
        {
            InitializeComponent();
        }

    
        private void frmMain_Load(object sender, EventArgs e)
        {
            // grab the joystick
            jst = new JoystickInterface.Joystick(this.Handle);
            string[] sticks = jst.FindJoysticks();
            jst.AcquireJoystick(sticks[0]);

            // add the axis controls to the axis container
            for (int i = 0; i < jst.AxisCount; i++)
            {
                Axis ax = new Axis();
                ax.AxisId = i + 1;
                flpAxes.Controls.Add(ax);
            }

            // add the button controls to the button container
            for (int i = 0; i < jst.Buttons.Length; i++)
            {
                JoystickSample.Button btn = new Button();
                btn.ButtonId = i + 1;
                btn.ButtonStatus = jst.Buttons[i];
                flpButtons.Controls.Add(btn);
            }

            // start updating positions
            tmrUpdateStick.Enabled = true;
        }

        private void tmrUpdateStick_Tick(object sender, EventArgs e)
        {
            // get status
            jst.UpdateStatus();

            // update the axes positions
            foreach (Control ax in flpAxes.Controls)
            {
                if (ax is Axis)
                {
                    switch (((Axis)ax).AxisId)
                    {
                        case 1:
                            ((Axis)ax).AxisPos = jst.AxisA;
                            
                            break;
                        case 2:
                            ((Axis)ax).AxisPos = jst.AxisB;

                            break;
                        case 3:
                            ((Axis)ax).AxisPos = jst.AxisC;
                            break;
                        case 4:
                            ((Axis)ax).AxisPos = jst.AxisD;
                            break;
                        case 5:
                            ((Axis)ax).AxisPos = jst.AxisE;
                            break;
                        case 6:
                            ((Axis)ax).AxisPos = jst.AxisF;
                            break;
                    }                
                 
                }
            }

            // update each button status
            foreach (Control btn in flpButtons.Controls)
            {
                if (btn is JoystickSample.Button)
                {
                    ((JoystickSample.Button)btn).ButtonStatus =
                        jst.Buttons[((JoystickSample.Button)btn).ButtonId - 1];
                }
            }
        }

        private void flpAxes_Paint(object sender, PaintEventArgs e)
        {

        }

        private void buttonStart_Click(object sender, EventArgs e)
        {
            SerialPort.PortName = "COM3";
            SerialPort.BaudRate = 9600;

            SerialPort.Open();
           if (SerialPort.IsOpen)
            {
                buttonStart.Enabled = false;
                buttonStop.Enabled = true;
               
            }
        }
        
        private void buttonStop_Click(object sender, EventArgs e)
        {
            if (SerialPort.IsOpen)
            {
                SerialPort.Close();
                buttonStart.Enabled = true;
                buttonStop.Enabled = false;

            }
        }

      
            public void sending(object sender, EventArgs e)
            {
                if (!SerialPort.IsOpen) return;
                //slanje AxisPos=jst.AxisA(ili možda axisA?) 

                string strAxisA = Convert.ToString(jst.AxisA);
                SerialPort.Write(strAxisA);

                string strAxisB = Convert.ToString(jst.AxisB);
                SerialPort.Write(strAxisB);

                string strAxisC = Convert.ToString(jst.AxisC);
                SerialPort.Write(strAxisC);

                string strAxisD = Convert.ToString(jst.AxisD);
                SerialPort.Write(strAxisD);
            }
       
       
    }
}
Posted
Comments
[no name] 30-Nov-15 7:10am    
What is the error ? please share it

Since you don't use any of the method's arguments sender and EventArgs, you can simply call
C#
sending(null, null);
from anywhere in your code.
On the other hand, since neither of the arguments is used, you could omit them altogether.

Windows Forms is an event-driven architecture. Deciding what event to call your method from is left to you.

Should there be no event but you want the data just to be coming on its own, you have to use a timer to generate such an event. There are three Timers available that I know of. For their differences, have a look at the linked MSDN pages.
 
Share this answer
 
Comments
Member 12150151 30-Nov-15 11:39am    
thank you, i used sending(null, null); and timer and it works.
I did not see any code where your reading and writing from Serial Port?
 
Share this answer
 
here is a function that i want to be send. so can you show me how that should be send?
C#
public void sending(object sender, EventArgs e)
           {
               if (!SerialPort.IsOpen) return;
               //slanje AxisPos=jst.AxisA(ili možda axisA?)

               string strAxisA = Convert.ToString(jst.AxisA);
               SerialPort.Write(strAxisA);

               string strAxisB = Convert.ToString(jst.AxisB);
               SerialPort.Write(strAxisB);

               string strAxisC = Convert.ToString(jst.AxisC);
               SerialPort.Write(strAxisC);

               string strAxisD = Convert.ToString(jst.AxisD);
               SerialPort.Write(strAxisD);
           }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900