Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Only one reading can be taken from a sensor at a time. I used the concept of asynchronous sockets, that is, it is only reading the processor temperature of a single sensor at a time. When you add more, it shows that there is an error. Any ideas would be appreciated.

After executing the jobs, all of the threads started successfully and then there was a problem in initializing for the 2nd sensor, displaying "Error TemperatureMesurement-Job initialization 4_cam2_temperature:Normally, each socket address (protocol, network address or connection) may only be used once"(refering to initializejob on the top )and then a problem in requestdata() for sensor 2 stating ""Error TemperatureMesurement-Job requestData 4_cam2_temperature:The object reference was not set to an object instance". The exception class used is Class System.exception.

And only using temperatureGauge.udp not the temperatureGauge.GMH3700. It is only requesting the temperature from a particular sensor and not from others as seen in the ASCII code –

To make it simple, problem is mainly here:
The one shows an error stating:
"Error TemperatureMesurement-Job initialization 4_cam2_temperature:Normally, each socket address (protocol, network address or connection) may only be used once"

C#
public override void initializeJob()
{
try
{
if (TempDevice == temperatureGauge.udp)
{
camIP = parameters[0];
socketCmdText = new UdpClient(cmdTextPort);
socketCmdText.BeginReceive(new AsyncCallback(OnUdpDataCmdText), socketCmdText);
socketCmd = new UdpClient(cmdPort);
socketCmd.BeginReceive(new AsyncCallback(OnUdpDataCmd), socketCmd);
}
if (TempDevice == temperatureGauge.GMH3700)
{
PortGMH3700.PortName = ComPortbyUser;
PortGMH3700.BaudRate = 4800; /* Baudrate '4800' oder '38400' */
PortGMH3700.Parity = Parity.None;
PortGMH3700.DataBits = 8;
PortGMH3700.StopBits = StopBits.One;
PortGMH3700.DtrEnable = true;
PortGMH3700.RtsEnable = false;
PortGMH3700.Open();
}
}
catch (Exception e)
{
Console.WriteLine("Error TemperatureMesurement-Job initialization " + jobName + " : " + e.Message);
}




The one shows an error stating:
""Error TemperatureMesurement-Job requestData 4_cam2_temperature:The object reference was not set to an object instance"

C#
private void requestData()
{
try
{
IPAddress _camip = IPAddress.Parse(camIP);
IPEndPoint _ep = new IPEndPoint(_camip, cmdTextPort);
byte[] _sendbuf = Encoding.ASCII.GetBytes("Cmd_ISL_GetCurrentSensorTemperature");
socketCmdText.Send(_sendbuf, _sendbuf.Length, _ep);
    byte[] _sendbuf2 = Encoding.ASCII.GetBytes("Get_Temp");
    socketCmdText.Send(_sendbuf2, _sendbuf2.Length, _ep);
    /*
    byte[] _sendbuf3 = Encoding.ASCII.GetBytes("APP_Version");
    socketCmdText.Send(_sendbuf3, _sendbuf3.Length, _ep);

    IPEndPoint _ep2 = new IPEndPoint(_camip, cmdPort);
    Byte[] _sendbuf4 = { 0x02, 0x00, 0x00, 0x10, 0x01, 0x00, 0x00, 0x00, 0xED };
    socketCmd.Send(_sendbuf4, _sendbuf4.Length, _ep2);
    */
}
catch (Exception e)
{
    Console.WriteLine("Error TemperatureMesurement-Job requestData " + jobName + " : " + e.Message);
}


What I have tried:

Any ideas would be appreciated. Thanks
Posted
Updated 18-Sep-19 10:15am
v4

Quote:
The object reference was not set to an object instance

This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.

Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterdays shirt when you took it off last night.

We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!

Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - and Visual Studio will help you here. Run your program in the debugger and when it fails, VS will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, VS will stop before the error, and let you examine what is going on by stepping through the code looking at your values.

But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!
 
Share this answer
 
Comments
Member 14589606 19-Sep-19 5:05am    
When i call intializeJob() it shows an error stating; Normally, each socket address (protocol, network address or connection) may only be used once"

public void ExecuteJob()
{
int measureEventTime = 0;
intervalComparison = measurementEvent.repetitionIntervalTime * 0.75;
if (measurementEvent.eventRepetitionCount > 1)
{
measureEventTime = (int)(measurementEvent.eventIntervalTime * measurementEvent.eventRepetitionCount);
intervalComparison = measurementEvent.eventIntervalTime * 0.75;
}

initializeJob();
try
{
if (IsRepeatable())
{
// execute the job in intervals determined by the methd
// GetRepetionIntervalTime()

Thread.Sleep(measurementEvent.startDelay);
while (jobActive)
{
for (int i = 0; i < measurementEvent.eventRepetitionCount; i++)
{
//Thread thread = new Thread(new ThreadStart(DoJob));
//thread.Start();
System.Threading.Tasks.Task tStart = System.Threading.Tasks.Task.Run(() => { DoJob(); });
Thread.Sleep(measurementEvent.eventIntervalTime);
}
Thread.Sleep(measurementEvent.repetitionIntervalTime - measureEventTime);
}
}
// since there is no repetetion, simply execute the job
else
{
DoJob();
}
}
catch (ThreadInterruptedException e)
{
Console.WriteLine(String.Format("The Job \"{0}\" has been interrupted successfully.", jobName));
}
finally
{
deinitializeJob();
}
}
Quote:
Can only read one temperature reading from the processor of the sensor, for other sensors it does not work

The usage of try/catch in your code prevent the program from telling you where the problem arise, and this is an important information.
A try/catch is used to hide an error that you expect and then give an appropriate answer.
Example: You have a function than mainly do a division and you expect divisor to be zero sometimes. If you do nothing, the program will crash, the try/catch will allow you to build a proper answer (say zero) and the program will complete without problem.
One thing to do is also to check if the error is what you expect, because otherwise, you must pop an error message.

Advice: remove the try/catch and launch the program in debugger mode, ypu will be shown where the error is and you will be able to inspect variables ans then you will know what exact request failed, it is the first step to solution.

Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
C#
public override void initializeJob()
{
    try
    {
        if (TempDevice == temperatureGauge.udp)
        {
            camIP = parameters[0];
            socketCmdText = new UdpClient(cmdTextPort);
            socketCmdText.BeginReceive(new AsyncCallback(OnUdpDataCmdText), socketCmdText);
            socketCmd = new UdpClient(cmdPort);
            socketCmd.BeginReceive(new AsyncCallback(OnUdpDataCmd), socketCmd);
        }
        if (TempDevice == temperatureGauge.GMH3700)
        {
            PortGMH3700.PortName = ComPortbyUser;
            PortGMH3700.BaudRate = 4800; /* Baudrate '4800' oder '38400' */
            PortGMH3700.Parity = Parity.None;
            PortGMH3700.DataBits = 8;
            PortGMH3700.StopBits = StopBits.One;
            PortGMH3700.DtrEnable = true;
            PortGMH3700.RtsEnable = false;
            PortGMH3700.Open();
        }
    }
    catch (Exception e)
    {
        Console.WriteLine("Error TemperatureMesurement-Job initialization " + jobName + " : " + e.Message);
    }
}

Indentation style - Wikipedia[^]

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]
 
Share this answer
 

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