[quote]Hi,I’m beginner in C# and wrote this code below in Microsoft Visual C# 2008 Express in Console application to control DC motors. Actually I want to control DC motors from the keyboard, but the problem is I have to press enter after press key.How can I solve this problem? and How can i change Com port?Thank you.using RoboticsConnection.Serializer;using RoboticsConnection.Serializer.Ids;using RoboticsConnection.Serializer.Sensors;using RoboticsConnection.Serializer.Components;using RoboticsConnection.Serializer.Controllers;using System;using System.Threading; namespace test{ class Program { static Serializer serializer; static PwmDCMotorController LeftMotor; static PwmDCMotorController RightMotor; static void Main(string[] args) { serializer = new Serializer(); LeftMotor = new PwmDCMotorController(serializer); RightMotor = new PwmDCMotorController(serializer); LeftMotor.DCMotorId = DCMotorId.DCMotor2; RightMotor.DCMotorId = DCMotorId.DCMotor1; serializer.StartCommunication(); while (true) { serializer.PumpEvents(); Console.Write("Enter Key:"); string s = Console.ReadLine(); if (s == "w") { LeftMotor.Speed = 100; RightMotor.Speed = 100; } if (s == "a") { LeftMotor.Speed = -100; RightMotor.Speed = 100; } if (s == "d") { LeftMotor.Speed = 100; RightMotor.Speed = -100; } if (s == "x") { LeftMotor.Speed = -100; RightMotor.Speed = -100; } if (s == "s") { LeftMotor.Speed = 0; RightMotor.Speed = 0; } } } }}
Something like this. The true in the readkey function blocks the key pressed from being displayed on the screen.
ConsoleKeyInfo cki; while (true) { cki = Console.ReadKey(true); switch (cki.Key) { case ConsoleKey.DownArrow: //Do DownArrow key processing here break; case ConsoleKey.UpArrow: //Do UpArrow key processing here break; case ConsoleKey.LeftArrow: //Do LeftArrow key processing here break; case ConsoleKey.RightArrow: //Do RightArrow key processing here break; case ConsoleKey.S: //Do s key processing here break; default: //Do any other key processing here break; }
Hope this helps.
Bruno