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
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);
}
public double Read(int serno, SerialPort objport)
{
double tempread = 0;
{
try
{
tempread = new General().funReadCOM(0, objport);
}
catch (TimeoutException) { }
}
return tempread;
}
public double funReadCOM(int intCOMContNo, SerialPort objport)
{
if (objport.IsOpen == false)
{
}
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.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;
}