0

I have an Electric meter connected over USB on COM5.

I want to read data from the meter but first check whether it's working or not. Means if I write something over the port I will send and receive again.

So, I am using the SerialPort class and the DataReceived event handler.

My code is below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;

namespace Communication
{
    class Program
    {
        static void Main(string[] args)
        {
            const int bufSize = 2048;
            Byte[] but = new Byte[bufSize]; // to save receive data

            SerialPort sp = new SerialPort("COM5");
           sp.BaudRate = 9600;
           sp.Parity = Parity.None;
           sp.StopBits = StopBits.One;
           sp.DataBits = 8;
           sp.Handshake = Handshake.None;
           sp.DtrEnable = true;
           sp.RtsEnable = true;
           sp.Open(); //open the port
            sp.DataReceived += port_OnReceiveDatazz; // event handler

           sp.WriteLine("$"); //start data stream
           Console.ReadLine();
           sp.WriteLine("!"); //stop data  stream
           sp.Close(); //close the port
        }
        //event handler method
        public static void SerialDataReceivedEventHandler(object sender, SerialDataReceivedEventArgs e)
        {
            SerialPort srlport = (SerialPort)sender;
            const int bufSize = 12;
            Byte[] buf = new Byte[bufSize];
            Console.WriteLine("Data Received!!!");
            Console.WriteLine(srlport.Read(buf,0,bufSize));
        }

    }
}

When compiling I get this error:

port_OnReceivedDatazz does not exist in the current context

Please give some suggestion.

Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
Salman Mushtaq
  • 341
  • 4
  • 23
  • check the example below: https://msdn.microsoft.com/cs-cz/library/system.io.ports.serialport.datareceived(v=vs.110).aspx – Erik Šťastný Nov 29 '16 at 09:26
  • I try this but not working, I get idea from this post http://stackoverflow.com/questions/466474/how-do-i-use-datareceived-event-of-the-serialport-port-object-in-c – Salman Mushtaq Nov 29 '16 at 09:32
  • you did a nice mixture of the two posts. You should follow one of them. Then it will work. – Mong Zhu Nov 29 '16 at 09:36
  • I follow Msdn example and run it but it shows Press any key to continue and then nothing. – Salman Mushtaq Nov 29 '16 at 09:45
  • And if I follow Stackoverflow post then it shows Data Received and next line 2. It not shows what it receive. – Salman Mushtaq Nov 29 '16 at 09:48
  • You are not printing what you receive to the console. look at the return value of the [Read method](https://msdn.microsoft.com/en-us/library/ms143549(v=vs.110).aspx) it returns the number of bytes that you read from the serial port. You need to print out the entire `byte[]` buf to the console to see what you received – Mong Zhu Nov 29 '16 at 10:39
  • can i use loop for it, please give some idea, – Salman Mushtaq Nov 29 '16 at 10:48
  • @SalmanMushtaq I made an edit to my answer. It shows how you can print the `byte[]`. Did you remove the answer because I did not solve all of your problems yet? ;) – Mong Zhu Nov 29 '16 at 11:35
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/129356/discussion-between-mong-zhu-and-salman-mushtaq). – Mong Zhu Nov 29 '16 at 14:14

1 Answers1

1

There is error port_OnReceivedDatazz does not exist in the current context

The name of your event handler and your event handler method have to correspond!

You have basically 2 Options either rename this line:

sp.DataReceived += port_OnReceiveDatazz; // event handler

to :

sp.DataReceived += SerialDataReceivedEventHandler;

OR rename the method

public static void port_OnReceiveDatazz(object sender, SerialDataReceivedEventArgs e)
{

EDIT:

if you still don't see the desired output it might be that Console.ReadLine() blocks the console and prevents it from printing.

In the MSDN Example they use

Console.ReadKey();

For Reference see this answer.

Just as last remark, you never save your received data permanently since you use a local variable for storing the input:

Byte[] buf = new Byte[bufSize];
srlport.Read(buf,0,bufSize);

You should use the array from this line:

Byte[] but = new Byte[bufSize]; // to save receive data

When you read your data take the but array:

srlport.Read(but,0,bufSize);

EDIT 2:

If you want to print out what you have received you need to print out the content of the array that you fill with the Read method:

//event handler method
public static void SerialDataReceivedEventHandler(object sender, SerialDataReceivedEventArgs e)
{
    SerialPort srlport = (SerialPort)sender;
    const int bufSize = 12;
    Byte[] buf = new Byte[bufSize];
    Console.WriteLine("Data Received!!!");
    int bytes_read = srlport.Read(buf,0,bufSize)
    Console.WriteLine("Bytes read: " + bytes_read);

    // you can use String.Join to print out the entire array without a loop
   Console.WriteLine("Content:\n" + String.Join(" ", bud));


}
Community
  • 1
  • 1
Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
  • thanks for your response, now I write initial command for communication with device i.e. /?!\r\n and wait for device response but device sends back /?!. I need or expect device sends back its name or serial name , do have any idea? – Salman Mushtaq Nov 29 '16 at 13:00
  • why do you expect that? is it written in the manual? Do you have a communication protocol specification by the manufacturer or something like this? – Mong Zhu Nov 29 '16 at 13:14
  • Yes, actually i want to communicate with serial device AS3500 with USB. I use SerialMon to detect its working now I need actual response from device and for this purpose I write initial command (may be its wrong) and needs device response and according to manual it should like ELS........etc – Salman Mushtaq Nov 29 '16 at 13:16
  • how do you translate the incoming bytes to ascii? – Mong Zhu Nov 29 '16 at 13:29
  • I'm not translate it to ascii. Ihave no idea about it. I write sp.write("/?!\r\n") and the result is /?! – Salman Mushtaq Nov 29 '16 at 13:44
  • and if i send hello or any other sentence it will back, but i need what device have i.e. the data, but initially i try to start the communication so that i am able to receive data – Salman Mushtaq Nov 29 '16 at 13:45
  • @SalmanMushtaq it seems as if the device gives you an echo on your "hello". you need a list of commands that the device can interpret as requests or something alike. usually these are codes in HEX like: 0x3F. if you use the Read method you read out bytes. These are pure numbers 0-255. if you Expect to read a string like "ELS........" you need either to use ReadExisting() or translate the bytes into chars. search for ASCII code on the web – Mong Zhu Nov 29 '16 at 14:53