Click here to Skip to main content
15,912,977 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
this is my code:
C#
namespace JoystickSample
{

   
    public partial class frmMain : Form
    {
        
        private JoystickInterface.Joystick jst;
        string RxString;
        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 = "COM4";
            SerialPort.BaudRate = 9600;

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

            }
        }

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

                string strAxisA = Convert.ToString(jst.AxisA);
                SerialPort.Write( "AxisA=" + strAxisA);
          
                string strAxisB = Convert.ToString(jst.AxisB);
                SerialPort.Write("AxisB=" + strAxisB);
        
                string strAxisC = Convert.ToString(jst.AxisC);
                SerialPort.Write("AxisC=" + strAxisC);
        
                string strAxisD = Convert.ToString(jst.AxisD);
                SerialPort.Write("AxisD=" + strAxisD);
        
            }

            private void timerSending_Tick(object sender, EventArgs e)
            {
                slanje(null, null);
                                         
            }

            private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
            {
                
                    RxString = SerialPort.ReadExisting();
                    this.Invoke(new EventHandler(DisplayText));
              
            }
            private void DisplayText(object sender, EventArgs e)
            {
                textBox1.AppendText(RxString);
            }
           
    }
}



so im sending the joystick axis position true serial port to my textbox, and problem occures when i close the port by clicking the stop button, and the messege is not send all the way. it should be written like this AxisA:32454, and if i hit stop and it ends writting on AxisA:3, without 2454, form freeze. how to avoid that. sry for bad eng.
Posted
Updated 30-Nov-15 5:56am
v2
Comments
CHill60 30-Nov-15 12:01pm    
Have you tried debugging your code to see what is happening?
Member 12150151 30-Nov-15 12:08pm    
yes i have, and i still cant find a solution
Philippe Mori 30-Nov-15 12:30pm    
You have to wait that data is completly send if it does matters...
Member 12150151 30-Nov-15 12:37pm    
i get that, but i have to know if somehow i can hit stop butten and stop sending after it send all data

1 solution

C#
 public static bool canStop;
 public bool CanStop {get { return canStop; } }

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


Edit to add button from comments:

C#
private void buttonStop_Click(object sender, EventArgs e)
{
   if(CanStop == true)
   {
      if (SerialPort.IsOpen)
      {
      freeze();    // Wtf is this ??
      SerialPort.Close();
      buttonStart.Enabled = true;
      buttonStop.Enabled = false;
      timerSending.Stop();
      }
   }
   else
     {
       MessageBox.Show("Not finished");
     }
}








This is pretty rough, But you want to do something like this and then check if "CanStop" is true or false when you click Stop.

Basically, You're stopping before the Foreach loop finishes.

Also, Your application is probably hanging because you aren't invoking when you update your UI.
 
Share this answer
 
v12
Comments
Member 12150151 1-Dec-15 4:44am    
when i try to apply this your code, i just get a bunch of errors:

Error 24 'JoystickSample.frmMain.btn' is a 'field' but is used like a 'type' C:\Users\suni\Desktop\jd2-ovaj radi\JoystickSample\frmMain.cs 131 17 JoystickSample
Error 25 'JoystickSample.frmMain.btn' is a 'field' but is used like a 'type' C:\Users\suni\Desktop\jd2-ovaj radi\JoystickSample\frmMain.cs 133 41 JoystickSample
Error 21 'JoystickSample.frmMain.SS' is a 'field' but is used like a 'type' C:\Users\suni\Desktop\jd2-ovaj radi\JoystickSample\frmMain.cs 126 9 JoystickSample
Error 16 Invalid token '-' in class, struct, or interface member declaration C:\Users\suni\Desktop\jd2-ovaj radi\JoystickSample\frmMain.cs 133 111 JoystickSample
Error 7 Invalid token 'is' in class, struct, or interface member declaration C:\Users\suni\Desktop\jd2-ovaj radi\JoystickSample\frmMain.cs 131 21 JoystickSample
Error 20 The type or namespace name 'var' could not be found (are you missing a using directive or an assembly reference?) C:\Users\suni\Desktop\jd2-ovaj radi\JoystickSample\frmMain.cs 125 5 JoystickSample
and so on
Member 12150151 1-Dec-15 7:13am    
okey i resolved the problem, and the last one i cant resolve is this warning:
Error 1 'System.Windows.Forms.Control' does not contain a definition for 'Count'
[no name] 1-Dec-15 8:41am    
You may need to do flpButtons.Controls.Count();
[no name] 1-Dec-15 8:46am    
Updated my solution with another answer that may be easier to follow/change.
Member 12150151 1-Dec-15 8:47am    
still getting this warning:
Error 1 Non-invocable member 'System.Windows.Forms.Layout.ArrangedElementCollection.Count' cannot be used like a method.

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