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 ...