static void Main(string[] args) { String RxedData; // String to store received data String COM_PortName; // String to store the name of the serial port SerialPort MyCOMPort = new SerialPort(); // Create a new SerialPort Object Console.WriteLine("\t+------------------------------------------+"); Console.WriteLine("\t| Reading from Serial Port using C# |"); Console.WriteLine("\t+------------------------------------------+"); Console.WriteLine("\t| (c) www.xanthium.in |"); Console.WriteLine("\t+------------------------------------------+"); Console.Write("\n\t Enter the COM port [eg COM32] -> "); COM_PortName = Console.ReadLine(); COM_PortName = COM_PortName.Trim(); // Remove any extra spaces COM_PortName = COM_PortName.ToUpper(); // convert the string to upper case //------------ COM port settings to 8N1 mode ------------------// MyCOMPort.PortName = COM_PortName; // Name of the COM port MyCOMPort.BaudRate = 9600; // Baudrate = 9600bps MyCOMPort.Parity = Parity.None; // Parity bits = none MyCOMPort.DataBits = 8; // No of Data bits = 8 MyCOMPort.StopBits = StopBits.One; // No of Stop bits = 1 MyCOMPort.Open(); // Open the port Console.WriteLine("\n\t Waiting for Data...."); RxedData = MyCOMPort.ReadLine(); // Wait for data reception Console.WriteLine("\n\t \" {0} \"", RxedData); // Write the data to console Console.WriteLine("\n\t Press any Key to Exit"); Console.Read(); // Press any key to exit MyCOMPort.Close(); // Close port } //end of main
static void Main(string[] args) { SerialPort MyCOMPort = new SerialPort(); // Create a new SerialPort Object String COM_PortName; String TX_data = "A"; Console.WriteLine("\t+------------------------------------------+"); Console.WriteLine("\t| Writing to Serial Port using C# |"); Console.WriteLine("\t+------------------------------------------+"); Console.WriteLine("\t| (c) www.xanthium.in |"); Console.WriteLine("\t+------------------------------------------+"); Console.Write("\n\t Enter the COM port [eg COM32] -> "); COM_PortName = Console.ReadLine(); COM_PortName = COM_PortName.Trim(); // Remove any extra spaces COM_PortName = COM_PortName.ToUpper(); // convert the string to upper case //COM port settings to 8N1 mode MyCOMPort.PortName = COM_PortName; // Name of your COM port MyCOMPort.BaudRate = 9600; // Baudrate = 9600bps MyCOMPort.Parity = Parity.None; // Parity bits = none MyCOMPort.DataBits = 8; // No of Data bits = 8 MyCOMPort.StopBits = StopBits.One; // No of Stop bits = 1 MyCOMPort.Open(); // Open the port MyCOMPort.Write(TX_data); // Write an ascii "A" Console.WriteLine("\n\t {0} written to {1}", TX_data, COM_PortName); MyCOMPort.Close(); // Close port Console.WriteLine("\t+------------------------------------------+"); Console.ReadLine(); } //end of Main
static void Main(string[] args) { SerialPort MyCOMPort = new SerialPort(); // Create a new SerialPort Object String COM_PortName; Console.WriteLine("\t+--------------------------------------------------------+"); Console.WriteLine("\t|Controlling the RTS and DTR lines Serial Port using C# |"); Console.WriteLine("\t+--------------------------------------------------------+"); Console.WriteLine("\t| (c) www.xanthium.in |"); Console.WriteLine("\t+--------------------------------------------------------+"); Console.Write("\t Enter the COM Port Number [eg COM32] ->"); COM_PortName = Console.ReadLine(); COM_PortName = COM_PortName.Trim(); // remove the trailing and leading spaces COM_PortName = COM_PortName.ToUpper(); // convert to upper case MyCOMPort.PortName = COM_PortName; // Assign the name of the serial port to be opened MyCOMPort.Open(); // Open the port //make both RTS and DTR LED's on USB2SERIAL off MyCOMPort.RtsEnable = true; // RTS = 1,~RTS = 0(USB2SERIAL) MyCOMPort.DtrEnable = true; // DTR = 1,~DTR = 0(USB2SERIAL) Console.Write("\n\t RTS = 1, (~RTS = 0 in USB2SERIAL)"); MyCOMPort.RtsEnable = true; // RTS pin = 1 ,~RTS = 0 Console.Write("\n\t Press Any Key to continue"); Console.ReadLine(); Console.Write("\n\t RTS = 0, (~RTS = 1 in USB2SERIAL)"); MyCOMPort.RtsEnable = false; // RTS pin = 0 ,~RTS = 1 Console.Write("\n\t Press Any Key to continue"); Console.ReadLine(); Console.Write("\n\t DTR = 1, (~DTR = 0 in USB2SERIAL)"); MyCOMPort.DtrEnable = true; // DTR pin = 1, ~DTR = 0 Console.Write("\n\t Press Any Key to continue"); Console.ReadLine(); Console.Write("\n\t DTR = 0, (~DTR = 1 in USB2SERIAL)"); MyCOMPort.DtrEnable = false; // DTR pin = 0, ~DTR = 1 Console.Write("\n\t Press Any Key to continue"); Console.ReadLine(); Console.Write("\n\t Press Any Key to Exit"); Console.ReadLine(); MyCOMPort.Close(); // Close port }//end of Main
static void Main(string[] args) { String RxedData; // String to store received data String COM_PortName; // String to store the name of the serial port int BaudRate = 9600; Display_Banner(); SerialPort MyCOMPort = new SerialPort(); // Create a new SerialPort Object //Get OS/NetCore version Console.WriteLine("\n\t Net Core Version: {0}", Environment.Version.ToString()); Console.WriteLine("\t OS Version: {0}", Environment.OSVersion.ToString()); Console.Write("\n\t Enter the COM port [eg COM32] -> "); COM_PortName = Console.ReadLine(); COM_PortName = COM_PortName.Trim(); // Remove any extra spaces COM_PortName = COM_PortName.ToUpper(); // convert the string to upper case //Setup the SerialPort using Given info try { //COM port settings to 8N1 mode MyCOMPort.PortName = COM_PortName; // Name of your COM port MyCOMPort.BaudRate = BaudRate; // Baudrate MyCOMPort.Parity = Parity.None; // Parity bits = none MyCOMPort.DataBits = 8; // No of Data bits = 8 MyCOMPort.StopBits = StopBits.One; // No of Stop bits = 1 // Set the read/write timeouts MyCOMPort.ReadTimeout = 1500; MyCOMPort.WriteTimeout = 1500; MyCOMPort.Open(); // Open the port } catch (UnauthorizedAccessException Ex) { Console.WriteLine("\tAccess is denied to the port."); Console.WriteLine("\tA process on the system, already has the specified COM port open"); //Console.WriteLine(Ex.ToString()); Console.WriteLine("\tPress ENTER Key to Exit"); Console.Read(); MyCOMPort.Close(); Environment.Exit(0); } catch (ArgumentOutOfRangeException Ex) { Console.WriteLine("\tOne or more of the properties for this instance are invalid"); Console.WriteLine("\tFor Eg,Parity, DataBits,Baudrate are not valid values"); //Console.WriteLine(Ex.ToString()); Console.WriteLine("\tPress ENTER Key to Exit"); Console.Read(); MyCOMPort.Close(); Environment.Exit(0); } catch (ArgumentException Ex) { Console.WriteLine("\tThe port name does not begin with \"COM\". "); //Console.WriteLine(Ex.ToString()); Console.WriteLine("\tPress ENTER Key to Exit"); Console.Read(); MyCOMPort.Close(); Environment.Exit(0); } catch (IOException Ex) //IOException is in System.io; { Console.WriteLine("\tThe port is in an invalid state"); //Console.WriteLine(Ex.ToString()); Console.WriteLine("\tPress ENTER Key to Exit"); Console.Read(); MyCOMPort.Close(); Environment.Exit(0); } catch (InvalidOperationException Ex) { Console.WriteLine("\tThe specified port on the current instance of the SerialPort is already open"); //Console.WriteLine(Ex.ToString()); Console.WriteLine("\tPress ENTER Key to Exit"); Console.Read(); MyCOMPort.Close(); Environment.Exit(0); } Console.WriteLine("\n\t Waiting for Data...."); try { RxedData = MyCOMPort.ReadLine(); // Wait for data reception // Microcontroller should send a \n so that ReadLine() will return Console.WriteLine("\n\t \" {0} \"", RxedData); // Write the data to console } catch (TimeoutException Ex) { //Console.WriteLine(Ex.ToString()); Console.WriteLine($"\tRead timeout happened, Time Out value = {MyCOMPort.ReadTimeout}mS"); Console.WriteLine("\tPress ENTER Key to Exit"); Console.Read(); MyCOMPort.Close(); Environment.Exit(0); } Console.WriteLine("\n\t Press any Key to Exit"); Console.Read(); // Press any key to exit MyCOMPort.Close(); // Close port } //end of main
static void Main(string[] args) { String COM_PortName; String TX_data = "A"; int BaudRate = 9600; Display_Banner(); //Get OS/NetCore version Console.WriteLine("\n\t Net Core Version: {0}", Environment.Version.ToString()); Console.WriteLine("\t OS Version: {0}", Environment.OSVersion.ToString()); SerialPort MyCOMPort = new SerialPort(); // Create a new SerialPort Object // Read COM port from user. Console.Write("\n\t Enter the COM port [eg COM32] -> "); COM_PortName = Console.ReadLine(); COM_PortName = COM_PortName.Trim(); // Remove any extra spaces COM_PortName = COM_PortName.ToUpper(); // convert the string to upper case //Setup the SerialPort using Given info try { //COM port settings to 8N1 mode MyCOMPort.PortName = COM_PortName; // Name of your COM port MyCOMPort.BaudRate = BaudRate; // Baudrate MyCOMPort.Parity = Parity.None; // Parity bits = none MyCOMPort.DataBits = 8; // No of Data bits = 8 MyCOMPort.StopBits = StopBits.One; // No of Stop bits = 1 // Set the read/write timeouts MyCOMPort.ReadTimeout = 500; MyCOMPort.WriteTimeout = 500; MyCOMPort.Open(); // Open the port } catch (UnauthorizedAccessException Ex) { Console.WriteLine("\tAccess is denied to the port."); Console.WriteLine("\tA process on the system, already has the specified COM port open"); //Console.WriteLine(Ex.ToString()); Console.WriteLine("\tPress ENTER Key to Exit"); Console.Read(); MyCOMPort.Close(); Environment.Exit(0); } catch (ArgumentOutOfRangeException Ex) { Console.WriteLine("\tOne or more of the properties for this instance are invalid"); Console.WriteLine("\tFor Eg,Parity, DataBits,Baudrate are not valid values"); //Console.WriteLine(Ex.ToString()); Console.WriteLine("\tPress ENTER Key to Exit"); Console.Read(); MyCOMPort.Close(); Environment.Exit(0); } catch (ArgumentException Ex) { Console.WriteLine("\tThe port name does not begin with \"COM\". "); //Console.WriteLine(Ex.ToString()); Console.WriteLine("\tPress ENTER Key to Exit"); Console.Read(); MyCOMPort.Close(); Environment.Exit(0); } catch (IOException Ex) //IOException is in System.io; { Console.WriteLine("\tThe port is in an invalid state"); //Console.WriteLine(Ex.ToString()); Console.WriteLine("\tPress ENTER Key to Exit"); Console.Read(); MyCOMPort.Close(); Environment.Exit(0); } catch (InvalidOperationException Ex) { Console.WriteLine("\tThe specified port on the current instance of the SerialPort is already open"); //Console.WriteLine(Ex.ToString()); Console.WriteLine("\tPress ENTER Key to Exit"); Console.Read(); MyCOMPort.Close(); Environment.Exit(0); } MyCOMPort.Write(TX_data); // Write an ascii "A" Console.WriteLine("\n\t {0} written to {1}", TX_data, COM_PortName); MyCOMPort.Close(); // Close port Console.WriteLine("\t+------------------------------------------+"); Console.ReadLine(); } //end of Main