How to Read and Write to Serial Port using C#
-
hello all I want to write some data to a serial port and Read from the Port the data and display in a textbox ... how can i do this
Did you even try google? I found this in about 2 seconds: MSDN Serial Port[^]
Simon
-
hello all I want to write some data to a serial port and Read from the Port the data and display in a textbox ... how can i do this
You can use SerialPort class in System.IO.Ports namespace for communicating with serial port. It provides various methods/event to read and write data.
Giorgi Dalakishvili #region signature My Articles Asynchronous Registry Notification Using Strongly-typed WMI Classes in .NET [^] My blog #endregion
-
hello all I want to write some data to a serial port and Read from the Port the data and display in a textbox ... how can i do this
-
Did you even try google? I found this in about 2 seconds: MSDN Serial Port[^]
Simon
hello Simon Stevens i treid the code below but nothing happens.... serialPort1.PortName = "COM1"; serialPort1.BaudRate = 9600; serialPort1.DataBits = 8; serialPort1.Parity = Parity.None; serialPort1.StopBits = StopBits.One; serialPort1.Open(); serialPort1.Write("A"); serialPort1.Close(); serialPort1.Open(); textBox1.Text = serialPort1.ReadLine();
-
You can use SerialPort class in System.IO.Ports namespace for communicating with serial port. It provides various methods/event to read and write data.
Giorgi Dalakishvili #region signature My Articles Asynchronous Registry Notification Using Strongly-typed WMI Classes in .NET [^] My blog #endregion
hello i treid the code below but nothing happens.... serialPort1.PortName = "COM1"; serialPort1.BaudRate = 9600; serialPort1.DataBits = 8; serialPort1.Parity = Parity.None; serialPort1.StopBits = StopBits.One; serialPort1.Open(); serialPort1.Write("A"); serialPort1.Close(); serialPort1.Open(); textBox1.Text = serialPort1.ReadLine();
-
hello Simon Stevens i treid the code below but nothing happens.... serialPort1.PortName = "COM1"; serialPort1.BaudRate = 9600; serialPort1.DataBits = 8; serialPort1.Parity = Parity.None; serialPort1.StopBits = StopBits.One; serialPort1.Open(); serialPort1.Write("A"); serialPort1.Close(); serialPort1.Open(); textBox1.Text = serialPort1.ReadLine();
TJS4u wrote:
i treid the code below but nothing happens....
That is because you are not doing anything! What are you expecting? Do you know how to use a serial port?
xacc.ide - now with TabsToSpaces support
IronScheme - 1.0 beta 1 - out now!
((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x)) -
hello Simon Stevens i treid the code below but nothing happens.... serialPort1.PortName = "COM1"; serialPort1.BaudRate = 9600; serialPort1.DataBits = 8; serialPort1.Parity = Parity.None; serialPort1.StopBits = StopBits.One; serialPort1.Open(); serialPort1.Write("A"); serialPort1.Close(); serialPort1.Open(); textBox1.Text = serialPort1.ReadLine();
What are you expecting to happen? You can't just write "A" to a serial port and then expect to read it back. You need to have some hardware connected to the serial port that either responds to data, or transmits data in.
Simon
-
TJS4u wrote:
i treid the code below but nothing happens....
That is because you are not doing anything! What are you expecting? Do you know how to use a serial port?
xacc.ide - now with TabsToSpaces support
IronScheme - 1.0 beta 1 - out now!
((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x)) -
hello i treid the code below but nothing happens.... serialPort1.PortName = "COM1"; serialPort1.BaudRate = 9600; serialPort1.DataBits = 8; serialPort1.Parity = Parity.None; serialPort1.StopBits = StopBits.One; serialPort1.Open(); serialPort1.Write("A"); serialPort1.Close(); serialPort1.Open(); textBox1.Text = serialPort1.ReadLine();
Hi, I think you are going in wrong direction. Serial port are buffered port and it has got two seperate data line, which are TX (Transmit) and RX(Receive). Now if you send any data to your serial port it's there in TX buffer. So which ever device connected to this serial port suppose to get that. So, you want to see whether this code working for you or not. So do one thing. Get one RS232 connector and short both RX pin and TX pin. Now try the same code, it should work. This is called loopback mechanism in hardware style. If you don't have this connector then there are few utility software which create virtual serial port and same utility can create loopback mechanism also. This is loopback in software style. I am sure either of two approach should solve your problem. Please let me know if you still face issues. Himadri