static void Main(string[] args) { string COMPortName; // Variable for Holding COM port number ,eg:- COM23 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.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
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
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