Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I tried to read the values from comport with 2 Channels concurrently. currently i am getting value from only one channel ,how to get the value from second channel, please advise.

Below is the code I tried using threading with delegates.please help me

Thanks in advance
Sheethal

C#
delegate double delcompvalue(int num, SerialPort objports);
delcompvalue fncompvalue = null;
protected IAsyncResult asyncfncompvalue = null;

fncompvalue = new delcompvalue(Read);
Thread t = new Thread(delegate() { fncompvalue.BeginInvoke(0, objport, null, null); });
t.Start();

asyncfncompvalue = fncompvalue.BeginInvoke(0, objport, null, null);
while (true)
{
    if (asyncfncompvalue.IsCompleted)
    break;
}
if (asyncfncompvalue.IsCompleted)
{
    tempread = fncompvalue.EndInvoke(asyncfncompvalue);
}


C#
public double Read(int serno, SerialPort objport)
{
    double tempread = 0;
    // while (_continue)
    {
        try
        {
           tempread = new General().funReadCOM(0, objport);   
        }
        catch (TimeoutException) { }
    }
    // null;
    return tempread;
}


C#
public double funReadCOM(int intCOMContNo, SerialPort objport)
{
    if (objport.IsOpen == false)
    {
        // objport.Open();
    }

    double read1 = 0;
    objport.DiscardInBuffer();
    byte[] buff = new byte[8];
    buff[0] = 1;
    string readValue = "";
    string[] readValue1 = new string[1];
    int readcount = 1;
    int offset = 1;
    string sWord1 = "", sWord2 = "";
    double dResult1 = 0;
    double dMulFactor = 0;
    double dResult = 0;
    //  objport.ReadBufferSize = 0;
    objport.Write(buff, offset, readcount);
    
    do
    {
    
    } while (objport.BytesToRead < 2);

    byte[] buffer = new byte[objport.ReadBufferSize];
    try
    {
        int bytesRead = objport.Read(buffer, 0, 2);
        tString = BitConverter.ToString(buffer.ToArray(), 0, 2);
        sWord2 = tString.Substring(0, 2);
        sWord1 = tString.Substring(3, 2);
        int value = Convert.ToInt32(sWord2, 16);
        
        if (dResult1 == 0)
        {
            dResult = (-1 * (Convert.ToInt32(sWord1, 16) + dMulFactor));
        }
        else if (dResult1 == 2)
        {
            dResult = ((Convert.ToInt32(sWord1, 16) + dMulFactor));
        }
    }
    catch
    {
    }
    return dResult;
}
Posted
Updated 25-Aug-15 22:46pm
v2
Comments
George Jonsson 26-Aug-15 4:49am    
You need to explain in more detail what it is you are trying to do in order to get some help.
For example, what kind channels, what is the device you are communicating with?
Not many have the hardware setup you have so we cannot run your code.
CPallini 26-Aug-15 5:22am    
What kind of serial communication are you using (e.g. RS485)?
What are 'the channels'?
smsheethal 27-Aug-15 7:57am    
Dear Palani

We are using RS 232

Regards
Sheethal

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900