Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to Read and Write to Serial Port using C#

How to Read and Write to Serial Port using C#

Scheduled Pinned Locked Moved C#
csharptutorialquestion
10 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TJS4u
    wrote on last edited by
    #1

    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

    S G L 3 Replies Last reply
    0
    • T TJS4u

      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

      S Offline
      S Offline
      Simon P Stevens
      wrote on last edited by
      #2

      Did you even try google? I found this in about 2 seconds: MSDN Serial Port[^]

      Simon

      T 1 Reply Last reply
      0
      • T TJS4u

        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

        G Offline
        G Offline
        Giorgi Dalakishvili
        wrote on last edited by
        #3

        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

        T 1 Reply Last reply
        0
        • T TJS4u

          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

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Seriously, aren't there enough articles about this topic here on CP? That's what the search function is for.

          1 Reply Last reply
          0
          • S Simon P Stevens

            Did you even try google? I found this in about 2 seconds: MSDN Serial Port[^]

            Simon

            T Offline
            T Offline
            TJS4u
            wrote on last edited by
            #5

            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();

            L S 2 Replies Last reply
            0
            • G Giorgi Dalakishvili

              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

              T Offline
              T Offline
              TJS4u
              wrote on last edited by
              #6

              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();

              H 1 Reply Last reply
              0
              • T TJS4u

                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();

                L Offline
                L Offline
                leppie
                wrote on last edited by
                #7

                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))

                T 1 Reply Last reply
                0
                • T TJS4u

                  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();

                  S Offline
                  S Offline
                  Simon P Stevens
                  wrote on last edited by
                  #8

                  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

                  1 Reply Last reply
                  0
                  • L leppie

                    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))

                    T Offline
                    T Offline
                    TJS4u
                    wrote on last edited by
                    #9

                    hello leppie my requirement is to connect a microcontrolelr to the serial port . i tried this to see if data is in the buffer ... then it can't be read back Thanks

                    1 Reply Last reply
                    0
                    • T TJS4u

                      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();

                      H Offline
                      H Offline
                      Himadri Majumdar
                      wrote on last edited by
                      #10

                      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

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups