hi
im writing the wireless program that using ,
joy stick class that i downloaded from this site
, but there is delay when i send command , with joy stick,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.DirectX.DirectInput;
using System.Diagnostics;
namespace new_joystick
{
public class Joystick
{
private Device joystickDevice;
private JoystickState state;
private int axisCount;
public int AxisCount
{
get { return axisCount; }
}
private int axisA;
public int AxisA
{
get { return axisA; }
}
private int axisB;
public int AxisB
{
get { return axisB; }
}
private int axisC;
public int AxisC
{
get { return axisC; }
}
private int axisD;
public int AxisD
{
get { return axisD; }
}
private int axisE;
public int AxisE
{
get { return axisE; }
}
private int axisF;
public int AxisF
{
get { return axisF; }
}
private IntPtr hWnd;
private bool[] buttons;
public bool[] Buttons
{
get { return buttons; }
}
private string[] systemJoysticks;
public Joystick(IntPtr window_handle)
{
hWnd = window_handle;
axisA = -1;
axisB = -1;
axisC = -1;
axisD = -1;
axisE = -1;
axisF = -1;
axisCount = 0;
}
private void Poll()
{
try
{
joystickDevice.Poll();
state = joystickDevice.CurrentJoystickState;
}
catch (Exception err)
{
Debug.WriteLine("Poll()");
Debug.WriteLine(err.Message);
Debug.WriteLine(err.StackTrace);
}
}
public string[] FindJoysticks()
{
systemJoysticks = null;
try
{
DeviceList gameControllerList = Manager.GetDevices(DeviceClass.GameControl, EnumDevicesFlags.AttachedOnly);
if (gameControllerList.Count > 0)
{
systemJoysticks = new string[gameControllerList.Count];
int i = 0;
foreach (DeviceInstance deviceInstance in gameControllerList)
{
joystickDevice = new Device(deviceInstance.InstanceGuid);
joystickDevice.SetCooperativeLevel(hWnd,
CooperativeLevelFlags.Background |
CooperativeLevelFlags.NonExclusive);
systemJoysticks[i] = joystickDevice.DeviceInformation.InstanceName;
i++;
}
}
}
catch (Exception err)
{
Debug.WriteLine("FindJoysticks()");
Debug.WriteLine(err.Message);
Debug.WriteLine(err.StackTrace);
}
return systemJoysticks;
}
public bool AcquireJoystick(string name)
{
try
{
DeviceList gameControllerList = Manager.GetDevices(DeviceClass.GameControl, EnumDevicesFlags.AttachedOnly);
int i = 0;
bool found = false;
foreach (DeviceInstance deviceInstance in gameControllerList)
{
if (deviceInstance.InstanceName == name)
{
found = true;
joystickDevice = new Device(deviceInstance.InstanceGuid);
joystickDevice.SetCooperativeLevel(hWnd,
CooperativeLevelFlags.Background |
CooperativeLevelFlags.NonExclusive);
break;
}
i++;
}
if (!found)
return false;
joystickDevice.SetDataFormat(DeviceDataFormat.Joystick);
joystickDevice.Acquire();
DeviceCaps cps = joystickDevice.Caps;
Debug.WriteLine("Joystick Axis: " + cps.NumberAxes);
Debug.WriteLine("Joystick Buttons: " + cps.NumberButtons);
axisCount = cps.NumberAxes;
UpdateStatus();
}
catch (Exception err)
{
Debug.WriteLine("FindJoysticks()");
Debug.WriteLine(err.Message);
Debug.WriteLine(err.StackTrace);
return false;
}
return true;
}
public void ReleaseJoystick()
{
joystickDevice.Unacquire();
}
public void UpdateStatus()
{
Poll();
int[] extraAxis = state.GetSlider();
axisA = state.Rz;
axisB = state.Rx;
axisC = state.X;
axisD = state.Y;
axisE = extraAxis[0];
axisF = extraAxis[1];
}
i think there is section that make a delay
byte[] jsButtons = state.GetButtons();
buttons = new bool[jsButtons.Length];
int i = 0;
foreach (byte button in jsButtons)
{
buttons[i] = button >= 20;
i++;
}
}
}
i think that the delay of the button (button not axis) it`s depend on this last block.
and it ` s section that connect and send data
void senddata(string messagebody)
{
try
{
byte[] data = new byte[100];
data = Encoding.ASCII.GetBytes(messagebody);
client.Send(data, data.Length, SocketFlags.None);
}
catch(Exception F)
{
label2.Text = F.Message;
}
}
Socket client;
Socket newsock;
private void button3_Click(object sender, EventArgs e)
{
byte[] data = new byte[100];
IPAddress ip = IPAddress.Parse("192.168.0.100");
IPEndPoint ipep = new IPEndPoint(ip, 8001);
newsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
newsock.Bind(ipep);
newsock.Listen(1);
client = newsock.Accept();
IPEndPoint newclinet = (IPEndPoint)client.RemoteEndPoint;
label2.Text = "Connected with " + newclinet.Address + " at port " + newclinet.Port;
string welcome = "Welcome goh to U.S.R.F wireless program";
data = Encoding.ASCII.GetBytes(welcome);
client.Send(data, data.Length, SocketFlags.None);
timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
byte[] data = new byte[1024];
int recv;
data = new byte[1024];
recv = client.Receive(data);
if (recv != 0)
{
label3.Text = " ";
label3.Text += Encoding.ASCII.GetString(data, 0, recv);
}
}
private void button4_Click(object sender, EventArgs e)
{
senddata(textBox1.Text);
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
client.Shutdown(SocketShutdown.Both);
client.Close();
}
i use two timer for send and recieve data in server program and one thread and one timer for send and recieve in client ,,, can anyone tell me why my program has delay ???