Results 1 to 12 of 12

Thread: SerialDataReceivedEventArgs not working

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2014
    Posts
    6

    SerialDataReceivedEventArgs not working

    Hello,

    i want to receive data from serial COM port and show received data in TextBox. As i'm beginner, i can't understand why it's not working, seems SerialDataReceivedEventArgs e not happening.

    Any suggestions please?
    Attached Files Attached Files

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: SerialDataReceivedEventArgs not working

    Please post the relevant code and only the relevant code directly in your post, within formatting tags to make it easy to read.

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2014
    Posts
    6

    Re: SerialDataReceivedEventArgs not working

    Here is code:

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO.Ports;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            string RxString;
            public Form1()
            {
                InitializeComponent();
                serialPort1.Open();
            }
    
    
            private void DisplayText(object sender, EventArgs e)
            {
    
                textBox1.AppendText(RxString);
    
            }
    
    
            private void serialPort1_DataReceived
             (object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
            {
               
                RxString = serialPort1.ReadExisting();
                this.Invoke(new EventHandler(DisplayText));
            }
    
    
    
            private void button1_Click(object sender, EventArgs e)
            {
                byte[] bytesToSend = new byte[5] { 0x44, 0x35, 0x01, 0x00, 0x00 };
                serialPort1.Write(bytesToSend, 0, 5);
                textBox1.AppendText("Start command sent!"+ "\r\n");
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                byte[] bytesToSend = new byte[5] { 0x44, 0x35, 0x02, 0x00, 0x00 };
                serialPort1.Write(bytesToSend, 0, 5);
                textBox1.AppendText("Stop command sent!" + "\r\n");
            }
    
    
          
        }
    }

  4. #4
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: SerialDataReceivedEventArgs not working

    Have you verified using PUTTY or some other terminal type program (such as hyperterm) that you can see some interaction on the port.
    You have no setup code, so the SerialPort object should be defaulting to COM1, 9600 baud, 8n1 (8 data bits, parity none, 1 stop bit) and Handshake = none.
    Do you know if these are correct for your setup?
    Sometimes you might have to set the DtrEnable to True, but I haven't done a lot of serial in .Net (a little in VB.Net) (mostly in VB6 years ago), and none in C#, so can't be definitive.
    I almost always want to use a terminal program like PUTTY first to verify that the hardware and serial protocols are known to work, then setup the software to use the verified port and settings, and work on issues if any.
    If you don't verify the hardware connection first (perhaps the cable is a cross-over, or needs to be a cross-over), you're just shooting in the dark when things don't work.
    You can't fix a hardware connection problem in your software, so check that first, then work on your code.

  5. #5

    Thread Starter
    New Member
    Join Date
    Oct 2014
    Posts
    6

    Re: SerialDataReceivedEventArgs not working

    Thanks Passel for your response.

    I tried to use Rs232 terminals to send/receive commands, so i know that device works and communication cable connected correctly.
    Device connected to the COM port has been developed by me some years ago, there is also LCD display, so i see that it receive correct commands and also it sends response to each received command. Also i have another programm made not by me, this works perfect with my device.

    So i'm 100% sure that problem is in my code, but i don't understand what exactly is wrong.
    I made serial port settings by clicking on serial port feature in VB, so settings (such as baudrate, COM number, RTS and DTR) must be correct. Do you think i must add port settings in my code?

  6. #6
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: SerialDataReceivedEventArgs not working

    I wouldn't think you would have to set the settings in code since you've preset them in the IDE.
    Unfortunately, I don't have time to play with this at the moment.
    Also, looking back at code with the little bit of serial I've done in .Net, it was transmit only. I didn't need or expect any data coming back from the device I was talking to.
    Did a lot more two way comms in VB6, but that wouldn't be of any use here.
    Doesn't sound like you're exactly a beginner, but perhaps just a beginner when it comes to C# and Serial comms, but I'm sorry I can't participate further at the moment.

  7. #7
    Frenzied Member Lightning's Avatar
    Join Date
    Oct 2002
    Location
    Eygelshoven
    Posts
    1,611

    Re: SerialDataReceivedEventArgs not working

    In the code you posted the event methode serialPort1_DataReceived is never bound to the event.
    VB6 & C# (WCF LINQ) mostly


    If you need help with a WPF/WCF question post in the NEW WPF & WCF forum and we will try help the best we can

    My site

    My blog, couding troubles and solutions

    Free online tools

  8. #8

    Thread Starter
    New Member
    Join Date
    Oct 2014
    Posts
    6

    Re: SerialDataReceivedEventArgs not working

    Well, i have more then 10 years experience in hardware development, but i'm total beginner in application development, so here i have lot of things to learn...
    Thanks Passel for your attention, hope i will get some suggestions from someone else here.

  9. #9

    Thread Starter
    New Member
    Join Date
    Oct 2014
    Posts
    6

    Re: SerialDataReceivedEventArgs not working

    Hello Lightning,

    could you write me sample of correct part of this line, please?
    Thanks in advance.

  10. #10
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: SerialDataReceivedEventArgs not working

    I'm not that experienced in C#, but I'm not sure how you can tell by looking at the code in form1.cs whether a handler has been "bound" to the event or not. I put the same code in a project and assigned event handlers for the two buttons and the serialPort1.DataReceived, and there is no indication of that in the form1.cs file.
    But you can see from the attached clip from the IDE that the function serialPort1_DataReceived is associated with the DataReceived event. That association obviously must be detailed in other files in C#.
    Name:  SerialDataReceivedHandler.jpg
Views: 1880
Size:  41.0 KB

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: SerialDataReceivedEventArgs not working

    Quote Originally Posted by passel View Post
    I'm not that experienced in C#, but I'm not sure how you can tell by looking at the code in form1.cs whether a handler has been "bound" to the event or not. I put the same code in a project and assigned event handlers for the two buttons and the serialPort1.DataReceived, and there is no indication of that in the form1.cs file.
    But you can see from the attached clip from the IDE that the function serialPort1_DataReceived is associated with the DataReceived event. That association obviously must be detailed in other files in C#.
    Name:  SerialDataReceivedHandler.jpg
Views: 1880
Size:  41.0 KB
    C# has no equivalent to WithEvents and Handles from VB. C# only has one way to attach a method to an event and it is equivalent to AddHandler from VB. The statements that attach the event handlers are in the designer code file, just as they are for VB if you select not to create a member variable for a control or component that you add in the designer. In the case you show, there would be a statement like this:
    csharp Code:
    1. this.serialPort1.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(this.serialPort1_DataReceived);
    In VB, if you set GenerateMember to False for the SerialPort component in the designer then you'll get this corresponding line in the designer code file:
    vb.net Code:
    1. AddHandler SerialPort1.DataReceived, AddressOf Me.SerialPort1_DataReceived
    Note that, while probably not many people do, you should be setting GenerateMember to False if you don't intend to access that component via a member variable in code.

  12. #12

    Thread Starter
    New Member
    Join Date
    Oct 2014
    Posts
    6

    Re: SerialDataReceivedEventArgs not working

    Thanks Jmcilhinney, you solve my problem.
    Have a nice day!!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width