using System; using System.IO.Ports; using System.Runtime.InteropServices; namespace ardujoy { class Program { [DllImport("user32.dll")] private static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo); const uint KEYEVENTF_KEYUP = 0x0002; public static void Main(string[] args) { string line; string JOY_BUTTON="1"; string JOY_UP="1"; string JOY_DOWN="1"; string JOY_LEFT="1"; string JOY_RIGHT="1"; string PREV_BUTTON="1"; string PREV_UP="1"; string PREV_DOWN="1"; string PREV_LEFT="1"; string PREV_RIGHT="1"; char KEY_BUTTON='Q'; char KEY_UP='W'; char KEY_DOWN='S'; char KEY_LEFT='A'; char KEY_RIGHT='D'; if (args.Length == 1) { var port = new SerialPort(args[0],115200, Parity.None, 8, StopBits.One); port.Open(); port.DiscardOutBuffer(); port.DiscardInBuffer(); while (port.IsOpen) { line = port.ReadLine(); // Console.WriteLine(line); if (line.Length == 5) { JOY_BUTTON=line.Substring(0,1); JOY_UP=line.Substring(4,1); JOY_DOWN=line.Substring(3,1); JOY_LEFT=line.Substring(2,1); JOY_RIGHT=line.Substring(1,1); if (JOY_BUTTON != PREV_BUTTON) { PREV_BUTTON=JOY_BUTTON; if (JOY_BUTTON == "0") { keybd_event(Convert.ToByte(KEY_BUTTON), 0, 0, 0); } else { keybd_event(Convert.ToByte(KEY_BUTTON), 0, KEYEVENTF_KEYUP, 0); } } if (JOY_UP != PREV_UP) { PREV_UP=JOY_UP; if (JOY_UP == "0") { keybd_event(Convert.ToByte(KEY_UP), 0, 0, 0); } else { keybd_event(Convert.ToByte(KEY_UP), 0, KEYEVENTF_KEYUP, 0); } } if (JOY_DOWN != PREV_DOWN) { PREV_DOWN=JOY_DOWN; if (JOY_DOWN == "0") { keybd_event(Convert.ToByte(KEY_DOWN), 0, 0, 0); } else { keybd_event(Convert.ToByte(KEY_DOWN), 0, KEYEVENTF_KEYUP, 0); } } if (JOY_LEFT != PREV_LEFT) { PREV_LEFT=JOY_LEFT; if (JOY_LEFT == "0") { keybd_event(Convert.ToByte(KEY_LEFT), 0, 0, 0); } else { keybd_event(Convert.ToByte(KEY_LEFT), 0, KEYEVENTF_KEYUP, 0); } } if (JOY_RIGHT != PREV_RIGHT) { PREV_RIGHT=JOY_RIGHT; if (JOY_RIGHT == "0") { keybd_event(Convert.ToByte(KEY_RIGHT), 0, 0, 0); } else { keybd_event(Convert.ToByte(KEY_RIGHT), 0, KEYEVENTF_KEYUP, 0); } } } } } else Console.WriteLine("You must provide the name of the serial port to use."); } } }