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. COM Port in Visual C# 2005

COM Port in Visual C# 2005

Scheduled Pinned Locked Moved C#
csharpcomtutorialquestion
2 Posts 2 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.
  • J Offline
    J Offline
    Juan Pablo G C
    wrote on last edited by
    #1

    There's a new possibility in the System.IO that is accesing to Port Coms: System.IO.Ports, I'm not good with delegates and I'm trying to make a program that when receives a data from the serial port makes an event: private void Form1_Load(object sender, EventArgs e) { ptocom.ReceivedEvent +=new SerialReceivedEventHandler(ptocom_ReceivedEvent); //It's not complete but I dont know how to put it well } void ptocom_ReceivedEvent(object sender, SerialReceivedEventArgs e) { //Type tipo; ? //tipo = e.EventType.GetType(); ? //taken.Text = tipo.ToString(); ? //taken.Text = buffer; //throw new NotImplementedException(); <-thats appear automatic ? } private void button1_Click(object sender, EventArgs e) { SerialPort ptocom = new SerialPort("COM1", 9600, System.IO.Ports.Parity.None, 8, StopBits.One); string buffer = "j"; ptocom.Open(); ptocom.ReadTo(buffer); } Thats all I could discover about using this new possibilities but It doesnt work, any Idea? Juan Pablo García Coello. Electronic Engineer. Projectist at the Electronic Dept.Instituto de Astrofísica de Canarias. Spain

    S 1 Reply Last reply
    0
    • J Juan Pablo G C

      There's a new possibility in the System.IO that is accesing to Port Coms: System.IO.Ports, I'm not good with delegates and I'm trying to make a program that when receives a data from the serial port makes an event: private void Form1_Load(object sender, EventArgs e) { ptocom.ReceivedEvent +=new SerialReceivedEventHandler(ptocom_ReceivedEvent); //It's not complete but I dont know how to put it well } void ptocom_ReceivedEvent(object sender, SerialReceivedEventArgs e) { //Type tipo; ? //tipo = e.EventType.GetType(); ? //taken.Text = tipo.ToString(); ? //taken.Text = buffer; //throw new NotImplementedException(); <-thats appear automatic ? } private void button1_Click(object sender, EventArgs e) { SerialPort ptocom = new SerialPort("COM1", 9600, System.IO.Ports.Parity.None, 8, StopBits.One); string buffer = "j"; ptocom.Open(); ptocom.ReadTo(buffer); } Thats all I could discover about using this new possibilities but It doesnt work, any Idea? Juan Pablo García Coello. Electronic Engineer. Projectist at the Electronic Dept.Instituto de Astrofísica de Canarias. Spain

      S Offline
      S Offline
      Stefan Troschuetz
      wrote on last edited by
      #2

      I think your problem is, that you subscribe your event handler during the handling of the Form.Load event, but the SerialPort object isn't created until button1_Click gets called or more likely is newly created there, cause otherwise the handler subscription would crash with null reference exception. So your event handler isn't subscribed to the SerialPort object you create in click event handler and therefor doesn't fire, but to some instance created before the Form.Load event.


      www.troschuetz.de

      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