Exemplo n.º 1
0
        static void Main(string[] args)
        {
            string COMPortName;                  // Variable for Holding serial port number ,eg:- ttyUSB1

            Menu();                              // Used for displaying the banner

            Console.Write("\t  Enter Serial Port Number(eg :- ttyUSB1) ->");
            COMPortName = Console.ReadLine();      //Store COM number in COMPortName

            COMPortName = COMPortName.Trim();      // Remove any trailing whitespaces

            COMPortName = @"/dev/" + COMPortName;  //

            SerialPort COMPort = new SerialPort(); // Create a SerialPort Object called COMPort

            COMPort.PortName = COMPortName;        // Assign the COM port number
            COMPort.BaudRate = 9600;               // Set Baud rate = 9600
            COMPort.DataBits = 8;                  // Number of data bits = 8
            COMPort.Parity   = Parity.None;        // No parity
            COMPort.StopBits = StopBits.One;       // One stop bit

            Console.WriteLine();
            Console.WriteLine("\t  {0} Selected  \n", COMPortName);
            Console.WriteLine("\t  Baud rate = {0}", COMPort.BaudRate);
            Console.WriteLine("\t  Data Bits = {0}", COMPort.DataBits);
            Console.WriteLine("\t  Parity    = {0}", COMPort.Parity);
            Console.WriteLine("\t  Stop Bits = {0}", COMPort.StopBits);

            COMPort.Open();                       // Open the serial port
            Console.WriteLine("\n\t  {0} opened \n", COMPortName);

            COMPort.DtrEnable = false;            // Since DTR = 0.~DTR = 1 So  DE = 1 Transmit Mode enabled
            COMPort.RtsEnable = false;            // Since RTS = 0,~RTS = 1 So ~RE = 1

            Console.WriteLine("\t  DTR = 0 so ~DTR = 1,  DE = 1 Transmit Mode enabled");
            Console.WriteLine("\t  RTS = 0 so ~RTS = 1, ~RE = 1");

            COMPort.Write("A");                   // Write  "A" to opened serial port

            Console.WriteLine("\n\t  A written to {0} ", COMPortName);

            COMPort.Close();                      // Close the Serial port
            Console.WriteLine("\n\t  {0} Closed", COMPortName);

            Menu_End();                           // Used for displaying the banner
            Console.Read();                       // Press to Exit
        }//End of Main
        static void Main(string[] args)
        {
            string COMPortName;                  // Variable for Holding COM port number ,eg:- COM23
            string RxedData;                     // Variable for Holding received data

            Menu();                              // Used for displaying the banner

            Console.Write("\t  Enter COM Port Number(eg :- COM32) ->");
            COMPortName = Console.ReadLine();      //Store COM number in COMPortName

            COMPortName = COMPortName.Trim();      // Remove any trailing whitespaces
            COMPortName = COMPortName.ToUpper();   // Convert the string to upper case

            SerialPort COMPort = new SerialPort(); // Create a SerialPort Object called COMPort

            COMPort.PortName = COMPortName;        // Assign the COM port number
            COMPort.BaudRate = 9600;               // Set Baud rate = 9600
            COMPort.DataBits = 8;                  // Number of data bits = 8
            COMPort.Parity   = Parity.None;        // No parity
            COMPort.StopBits = StopBits.One;       // One stop bit

            Console.WriteLine();
            Console.WriteLine("\t  {0} Selected  \n", COMPortName);
            Console.WriteLine("\t  Baud rate = {0}", COMPort.BaudRate);
            Console.WriteLine("\t  Data Bits = {0}", COMPort.DataBits);
            Console.WriteLine("\t  Parity    = {0}", COMPort.Parity);
            Console.WriteLine("\t  Stop Bits = {0}", COMPort.StopBits);

            COMPort.Open();                       // Open the serial port
            Console.WriteLine("\n\t  {0} opened \n", COMPortName);

            COMPort.RtsEnable = true;            // Since RTS = 1, ~RTS = 0 So ~RE = 0 Receive  Mode enabled
            COMPort.DtrEnable = true;            // Since DTR = 1. ~DTR = 0 So  DE = 0
                                                 //~RE and DE LED's on USB2SERIAL board will be off

            Console.WriteLine("\t  RTS = 1 so ~RTS = 0, ~RE = 0 Receive  Mode enabled");
            Console.WriteLine("\t  DTR = 1 so ~DTR = 0,  DE = 0 ");

            RxedData = COMPort.ReadLine();       // Wait for data reception

            Console.WriteLine("\n\t  Data Received ");
            Console.WriteLine("\n\t \" {0} \" ", RxedData);

            Menu_End();
            Console.Read();                       // Press to Exit
        }//end of Main
Exemplo n.º 3
0
		public static void Main()
		{
			
			string COMPortName;  	 // To store COM port name 
			string Choice;      	 // For selecting the action
			bool Sentry = true;  	 // Used to exit from the loop
			
			SerialPort COMPort = new SerialPort();   // Create a new SerialPort Object 
			
			Choice_Menu(); // Draw a Banner on Top displaying Program name 
			
			Console.WriteLine("\n\t Enter the Serial Port Number eg ttyUSB0->"); // Enter the COM port to be used for communication,eg ttyUSB0
			COMPortName = Console.ReadLine();                                  // Read the COM port number                             
			COMPortName = COMPortName.Trim();
			
			ConfigureSerial(COMPort,COMPortName);  //Assign Baudrate,databit,start/stop bits to COMPort 
			
			// Loop for Controlling the Robot 
			while(Sentry)
			{
				Choice_Menu();                       // Display Program name Banner 
				SerialSettingsDisplay_Menu(COMPort); // Display Selected COMPort and Baudrate 
				Display_Commands();
				
				Choice = EnterChoice();              // Ask for user input 
				
				Console.WriteLine("\t Choice -->{0}",Choice); //Display selected option
				
				
				switch(Choice)
				{
					case "W":  RS485_Transmit(COMPort,"A");
					           Console.WriteLine("\n\t Robot-Forward,Both Motors ON");
					           Console.Write("\t Press Any Key to Continue....");
					           Console.Read();
						       break;
						       
					case "S":  RS485_Transmit(COMPort,"B");
							   Console.WriteLine("\n\t Robot-Reverse,Both Motors Reverse");
							   Console.Write("\t Press Any Key to Continue....");
					           Console.Read();
						       break;
						       
					case "D":  RS485_Transmit(COMPort,"C");
							   Console.WriteLine("\n\t Robot-Turn Right,Right Motor ON");
							   Console.Write("\t Press Any Key to Continue....");
					           Console.Read();
						       break;	
						       
					case "A":  RS485_Transmit(COMPort,"D");
				        	   Console.WriteLine("\n\t Robot-Turn Left,Left Motor ON");
					           Console.Write("\t Press Any Key to Continue....");
					           Console.Read();
						       break;
						       
				    case "Q":  RS485_Transmit(COMPort,"E");
				               Console.WriteLine("\n\t Robot-Stop,Both Motors OFF");
					           Console.Write("\t Press Any Key to Continue....");
						       break;	
						       
				    case "F":  RS485_Transmit(COMPort,"F");
				    		   Console.WriteLine("\n\t Checking Connectivity");
					           Console.Write("\t Press Any Key to Continue....");	
						       break;	
						       
					case "X":  Sentry = false;
							   RS485_Transmit(COMPort,"E");	
							   Console.WriteLine("\n\t Press Any Key to Quit");
						       break;	
						       
				    default :  Console.WriteLine("Invalid Input");
						       break;
				
				}//end of Switch()
				
			}//end of While() loop
			Console.Read();
			
		}
        static void Main()
        {
            string COMPortName;                                  // Name of the Serial port eg :- COM32
            bool   Sentry = true;
            string Choice;                                       // Used for Menu

            HeaderDialog();                                      // Displays the top banner
            InstructionsDialog();                                // Displays the instructions

            Console.Write("\n\t  Enter The COM port Number ->"); // Enter the COM port number
            COMPortName = Console.ReadLine();                    // Read the COM port number
            COMPortName = COMPortName.Trim();                    // Trim any unwanted SPACE's


            SerialPort COMPort = new SerialPort(); //Create a new SerialPort Object named COMPort

            COMPort.PortName = COMPortName;        // Assign the name of your COM port
            COMPort.BaudRate = 9600;               // Set the baudrate = 9600 ,(8N1 Format)
            COMPort.StopBits = StopBits.One;       // Number of Stop bits = 1,(8N1 Format)
            COMPort.Parity   = Parity.None;        // No Parity, (8N1 Format)
            COMPort.DataBits = 8;                  // Databits = 8,(8N1 Format)

            try
            {
                COMPort.Open(); // try to open the COM port based on the name you entered earlier
            }
            catch               // If opening fails ,print error message and exit program
            {
                Console.WriteLine("\n\t  Unable to Open Serial Port !");
                Console.WriteLine("\n\t  Press Any Key to Exit");
                Sentry = false; // Make Sentry False So that code will not enter the While () loop below
            }

            while (Sentry == true)
            {
                Console.Clear();                            // Clear the console Window
                HeaderDialog();                             // Show the dialogs again
                InstructionsDialog();
                Console.Write("\n\t  Enter your Choice->"); // Prompt for choice
                Choice = Console.ReadLine();                // Read choice
                Choice = Choice.ToUpper();                  // Convert to upper case
                Choice = Choice.Trim();                     // Remove SPACE's

                COMPort.DtrEnable = false;                  // ~DTR [FT232] ->  DE[MAX485] in USB2SERIAL,DTR =0 ,so ~DTR = 1,DE=1 so transmit mode
                COMPort.RtsEnable = false;                  //  ~RTS [FT232] -> ~RE[MAX485] in USB2SERIAL,RTS =0,so ~RTS =1,~RE =1

                switch (Choice)
                {
                case "A": COMPort.Write("A");     // Send 'A' to Start the Right motor
                    break;

                case "Q": COMPort.Write("Q");     // Send 'Q' to Stop  the Right Motor
                    break;

                case "D": COMPort.Write("D");     // Send 'D' to Start the Left Motor
                    break;

                case "E": COMPort.Write("E");     // Send 'E' to Stop  the Left Motor
                    break;

                case "X": Sentry = false;         // Exit from the loop
                    Console.WriteLine("\n\t  Press Any Key to Exit");
                    break;

                default: break;
                }
            }
            COMPort.Close(); //Close the ComPort
            Console.Read();
        }//End of Main()
Exemplo n.º 5
0
        static void Main(string[] args)
        {
            string COMPortName;                  // Variable for Holding COM port number ,eg:- COM23
            int    Baudrate;                     // Variable for holding the Baudrate

            Menu();                              // Used for displaying the banner

            Console.Write("\t  Enter COM Port Number(eg :- COM32) ->");
            COMPortName = Console.ReadLine();     //Store COM number in COMPortName

            COMPortName = COMPortName.Trim();     // Remove any trailing whitespaces
            COMPortName = COMPortName.ToUpper();  // Convert the string to upper case

            Console.Write("\t  Enter Baudrate (eg :- 9600) ->");
            Baudrate = Convert.ToInt32(Console.ReadLine()); //Convert character to int32 and store baudrate value from user

            SerialPort COMPort = new SerialPort();          // Create a SerialPort Object called COMPort

            COMPort.PortName = COMPortName;                 // Assign the COM port number
            COMPort.BaudRate = Baudrate;                    // Set Baud rate decided by user
            COMPort.DataBits = 8;                           // Number of data bits = 8
            COMPort.Parity   = Parity.None;                 // No parity
            COMPort.StopBits = StopBits.One;                // One stop bit

            Console.WriteLine();
            Console.WriteLine("\t  {0} Selected  \n", COMPortName);
            Console.WriteLine("\t  Baud rate = {0}", COMPort.BaudRate);
            Console.WriteLine("\t  Data Bits = {0}", COMPort.DataBits);
            Console.WriteLine("\t  Parity    = {0}", COMPort.Parity);
            Console.WriteLine("\t  Stop Bits = {0}", COMPort.StopBits);

            // Try Opening the Serialport to receive data from Microcontroller
            try
            {
                COMPort.Open();                       // Open the serial port
                Console.WriteLine("\n\t  {0} opened \n", COMPortName);
            }
            catch (Exception e)
            {
                Console.WriteLine("\n\t  {0} Cannot be opened \n", COMPortName);
                Console.WriteLine("\n\t  An Exception has Ocurred \n");
                //Console.WriteLine("\n\t  {0}", e.ToString()); //un comment this line to view the error message
                COMPort.Close();     //close the com port
                Menu_End();
                Environment.Exit(0); // exit the program
            }


            COMPort.DtrEnable = false;            // Since DTR = 0.~DTR = 1 So  DE = 1 Transmit Mode enabled
            COMPort.RtsEnable = false;            // Since RTS = 0,~RTS = 1 So ~RE = 1

            Console.WriteLine("\t  DTR = 0 so ~DTR = 1,  DE = 1 Transmit Mode enabled");
            Console.WriteLine("\t  RTS = 0 so ~RTS = 1, ~RE = 1");


            COMPort.Write("A");                   // Write  "A" to opened serial port
            Console.WriteLine("\n\t  A written to {0} ", COMPortName);
            Console.ReadLine();

            COMPort.Write("B");                   // Write  "B" to opened serial port
            Console.WriteLine("\n\t  B written to {0} ", COMPortName);
            Console.ReadLine();

            COMPort.Write("C");                   // Write  "C" to opened serial port
            Console.WriteLine("\n\t  C written to {0} ", COMPortName);
            Console.ReadLine();

            COMPort.Close();                      // Close the Serial port
            Console.WriteLine("\n\t  {0} Closed", COMPortName);

            Menu_End();                           // Used for displaying the banner
        }//End of Main
Exemplo n.º 6
0
        static void Main(string[] args)
        {
            string COMPortName;                  // Variable for Holding COM port number ,eg:- COM23
            string RxedData;                     // Variable for Holding received data
            int    Baudrate;                     // Variable for holding the Baudrate

            Menu();                              // Used for displaying the banner

            Console.Write("\t  Enter COM Port Number(eg :- COM32) ->");
            COMPortName = Console.ReadLine();     //Store COM number in COMPortName

            COMPortName = COMPortName.Trim();     // Remove any trailing whitespaces
            COMPortName = COMPortName.ToUpper();  // Convert the string to upper case

            Console.Write("\t  Enter Baudrate (eg :- 9600) ->");
            Baudrate = Convert.ToInt32(Console.ReadLine()); //Convert character to int32 and store baudrate value from user

            COMPortName = COMPortName.Trim();               // Remove any trailing whitespaces

            SerialPort COMPort = new SerialPort();          // Create a SerialPort Object called COMPort

            COMPort.PortName = COMPortName;                 // Assign the COM port number
            COMPort.BaudRate = Baudrate;                    // Set Baud rate entered by the user
            COMPort.DataBits = 8;                           // Number of data bits = 8
            COMPort.Parity   = Parity.None;                 // No parity
            COMPort.StopBits = StopBits.One;                // One stop bit

            Console.WriteLine();
            Console.WriteLine("\t  {0} Selected  \n", COMPortName);
            Console.WriteLine("\t  Baud rate = {0}", COMPort.BaudRate);
            Console.WriteLine("\t  Data Bits = {0}", COMPort.DataBits);
            Console.WriteLine("\t  Parity    = {0}", COMPort.Parity);
            Console.WriteLine("\t  Stop Bits = {0}", COMPort.StopBits);

            // Try Opening the Serialport to receive data from Microcontroller
            try
            {
                COMPort.Open();                       // Open the serial port
                Console.WriteLine("\n\t  {0} opened \n", COMPortName);
            }
            catch (Exception e)
            {
                Console.WriteLine("\n\t  {0} Cannot be opened \n", COMPortName);
                Console.WriteLine("\n\t  An Exception has Ocurred \n");
                //Console.WriteLine("\n\t  {0}", e.ToString()); //un comment this line to view the error message
                COMPort.Close();     //close the com port
                Menu_End();
                Environment.Exit(0); // exit the program
            }


            COMPort.RtsEnable = true;            // Since RTS = 1, ~RTS = 0 So ~RE = 0 Receive  Mode enabled
            COMPort.DtrEnable = true;            // Since DTR = 1. ~DTR = 0 So  DE = 0
                                                 //~RE and DE LED's on USB2SERIAL board will be off

            Console.WriteLine("\t  RTS = 1 so ~RTS = 0, ~RE = 0 Receive  Mode enabled");
            Console.WriteLine("\t  DTR = 1 so ~DTR = 0,  DE = 0 ");

            //Continously read data,you will have to close program window to exit.
            //removing the while(true) statement will make the program read data only once
            while (true)
            {
                RxedData = COMPort.ReadLine();       // Wait for data reception

                //Console.WriteLine("\n\t  Data Received ");
                Console.WriteLine("\n\t {0}", RxedData);
            }

            Menu_End();
        }//end of Main