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. serial port + data receiving event

serial port + data receiving event

Scheduled Pinned Locked Moved C#
helpquestion
6 Posts 4 Posters 1 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.
  • D Offline
    D Offline
    DKalepu
    wrote on last edited by
    #1

    Hi All, I have developed a windows application using serialport. I am able to send the data to the terminal but not receiving it.. here is my code SerialPort port = new SerialPort("COM4", 9600, Parity.None, 8, StopBits.One); private void port_DataReceived(object sender, SerialDataReceivedEventArgs e) { port.DtrEnable = true; port.Handshake = Handshake.RequestToSend; txtData.Text = txtData.Text + port.ReadExisting(); } Why i'm not able to receive data?:confused: :(( whats wrong in my code... Could anyone please help me on this issue... Thanks in Advance/..

    M 1 Reply Last reply
    0
    • D DKalepu

      Hi All, I have developed a windows application using serialport. I am able to send the data to the terminal but not receiving it.. here is my code SerialPort port = new SerialPort("COM4", 9600, Parity.None, 8, StopBits.One); private void port_DataReceived(object sender, SerialDataReceivedEventArgs e) { port.DtrEnable = true; port.Handshake = Handshake.RequestToSend; txtData.Text = txtData.Text + port.ReadExisting(); } Why i'm not able to receive data?:confused: :(( whats wrong in my code... Could anyone please help me on this issue... Thanks in Advance/..

      M Offline
      M Offline
      Mir_As
      wrote on last edited by
      #2

      write instead of your code like this code. use: create a delegate like this: private delegate void Update(); private void port_DataReceived(object sender, SerialDataReceivedEventArgs e) { port.DtrEnable = true; port.Handshake = Handshake.RequestToSend; this.invoke(new invoker Update(this.refresh)); } private void refresh() { txtData.Text = txtData.Text + port.ReadExisting(); }

      M 1 Reply Last reply
      0
      • M Mir_As

        write instead of your code like this code. use: create a delegate like this: private delegate void Update(); private void port_DataReceived(object sender, SerialDataReceivedEventArgs e) { port.DtrEnable = true; port.Handshake = Handshake.RequestToSend; this.invoke(new invoker Update(this.refresh)); } private void refresh() { txtData.Text = txtData.Text + port.ReadExisting(); }

        M Offline
        M Offline
        Mir_As
        wrote on last edited by
        #3

        sorry this code was wrong: write this: private delegate void Updatedatas(); private void port_DataReceived(object sender, SerialDataReceivedEventArgs e) { port.DtrEnable = true; port.Handshake = Handshake.RequestToSend; this.invoke(new Updatedatas(this.refresh)); } private void refresh() { txtData.Text = txtData.Text + port.ReadExisting(); }

        D 1 Reply Last reply
        0
        • M Mir_As

          sorry this code was wrong: write this: private delegate void Updatedatas(); private void port_DataReceived(object sender, SerialDataReceivedEventArgs e) { port.DtrEnable = true; port.Handshake = Handshake.RequestToSend; this.invoke(new Updatedatas(this.refresh)); } private void refresh() { txtData.Text = txtData.Text + port.ReadExisting(); }

          D Offline
          D Offline
          DKalepu
          wrote on last edited by
          #4

          Hi, Thanks for your help.. but, still it isn't receiving any thing.. could anyone plz help me to resolve this issue.... :(( :confused: :confused: -- modified at 7:03 Wednesday 27th June, 2007

          K L 2 Replies Last reply
          0
          • D DKalepu

            Hi, Thanks for your help.. but, still it isn't receiving any thing.. could anyone plz help me to resolve this issue.... :(( :confused: :confused: -- modified at 7:03 Wednesday 27th June, 2007

            K Offline
            K Offline
            kristmun
            wrote on last edited by
            #5

            I think you have to set the RtsEnable flag. port.RtsEnable = true; try it out

            1 Reply Last reply
            0
            • D DKalepu

              Hi, Thanks for your help.. but, still it isn't receiving any thing.. could anyone plz help me to resolve this issue.... :(( :confused: :confused: -- modified at 7:03 Wednesday 27th June, 2007

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              Hi, this is my 4-step advise to you: 1. first make sure you dont get anything in your app: try to read one byte (a raw byte, not a char, not a string) and show it on the Console (this avoids all GUI trouble, crossthread problems, etc). 2. if that fails make sure there is something to receive on the serial cable: try another program, such as hyperterminal. As long as that does not receive anything, check your cable, your port settings, your target device; if necessary, use an oscilloscope or whatever to debug the hardware/firmware. 3. Once hyperterminal shows something reasonable, return to your app. Again I recommend reading bytes (a byte array) rather than ReadExisting() until you start getting data, since I expect that has better chance of delevering something (it avoids Encoding problems and the like). Check your handshaking; and try a different one. 4. When things are getting alive, now start reading chars and/or strings. Remark: in another thread I got the impression serial port was NOT using ASCII encoding by default, it seemed to be using Unicode instead. Good hunting !

              Luc Pattyn [My Articles] [Forum Guidelines]

              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