-1

I am communicating with a machine by serial port connected through RS232 cable. After passing the credentials I am able to get the data from the machine as client if the buffer storage of the machine is empty (clean). As soon as I close my application, the data comes into the buffer storage of the machine as machine is in continuous running mode. After this if I try to get the data with the same way by starting my application and passing credentials I do not get buffer as well as live data from the machine.

Now again when I try to log in by passing credentials into hyperterminal.exe and after I am able to get the buffer as well as live data..

So my question is why am I not getting the buffer data from program when data is there in the buffer as we are getting from Hyperterminal.exe

I have struggled a lot searching for the solution for this but no luck ..

I request to please guide me on this.. any suggestion will be like a life savior for me..

Here is the code that I am using..

        port1.RtsEnable = true;
        port1.DtrEnable = true;
        port1.PortName = "COM1";
        port1.BaudRate = 9600;
        port1.DataBits = 8;
        port1.Parity = System.IO.Ports.Parity.None;
        port1.StopBits = System.IO.Ports.StopBits.One;
        port1.Handshake = Handshake.RequestToSend;
        port1.Handshake = Handshake.RequestToSendXOnXOff;
        port1.Handshake = Handshake.None;
        port1.Handshake = Handshake.XOnXOff;
        port1.Open();

        port1.Write("Username\r\n");
        port1.Write("Password\r\n");
        port1.Write("Command\r\n");
        port1.DataReceived += new SerialDataReceivedEventHandler(port1_DataReceived);

    }
    public void port1_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {

        Thread.Sleep(500);
        while (port1.IsOpen)
        {
            //string s = port1.ReadExisting();
            string s = port1.ReadLine();
        }
        }

I have used ReadLine() as well as ReadExisting() but with no luck..

I/O Code..

        public void getData() {

        byte[] buffersize = new byte[port1.ReadBufferSize];

        int bytesRead = port1.Read(buffersize, 0, buffersize.Length);

        byte[] buffer = new byte[bytesRead];

        File.AppendAllText(text, "Inside 1\r\n");

        Action kickoffRead = delegate
        {
            port1.BaseStream.BeginRead(buffer, 0, buffer.Length, delegate(IAsyncResult ar)
            {
                try
                {
                    File.AppendAllText(text, "Inside 2\r\n");

                    int actualLength = port1.BaseStream.EndRead(ar);
                    byte[] received = new byte[actualLength];
                    Buffer.BlockCopy(buffer, 0, received, 0, actualLength);

                    string mybuffer = "";
                    //ASCII data. 
                    mybuffer += Encoding.ASCII.GetString(received, 0, bytesRead);
                 }

I have invoked this method just after the login credentials...Still have no luck in receiving the data ...

user3725571
  • 55
  • 1
  • 2
  • 8

1 Answers1

0
public void port1_DataReceived(object sender, SerialDataReceivedEventArgs e) 
{ 
    //Initialize a buffer to hold the received data 
    byte[] buffer = new byte[port1.ReadBufferSize]; 

    //get the number of bytes read 
    int bytesRead = port1.Read(buffer, 0, buffer.Length); 

    //ASCII data. 
    mybuffer += Encoding.ASCII.GetString(buffer, 0, bytesRead);

    if(mybuffer.IndexOf('\r') > -1)
    {
        //Found a carriage return, do something with buffer?
    }
}

you are going to get bits and pieces, so you might want to buffer it all up and look for a return character (or whatever terminator you are getting from the other side) to extract the packet.

Kevin Cook
  • 1,922
  • 1
  • 15
  • 16
  • One query ,Do i need to write the code before `port1_DataReceived` or after because `port1_DataReceived` will be called once any live data will come as per my understanding .. – user3725571 Jun 13 '14 at 03:52
  • I tested by putting before and after both .Incase of before just after passing the credentials it is going inside but not reading any data while in after case it is not going inside only as data is there in the buffer of the machine.. – user3725571 Jun 13 '14 at 07:21
  • If I am trying to save the buffer details into text by doing this `byte[] buffer = new byte[port1.ReadBufferSize];File.AppendAllText(text,buffer.ToString());`. then i am getting `System.Byte[]` as text output And flow of the programs stops here only.. – user3725571 Jun 13 '14 at 07:30