Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey Im creating the UDP Client on the top and then creating it again in initialise job, when I remove the try and catch, the socket has a value of null, I communicate thru the socket and then close it after that I REPEAT but either the port it not closed properly as it displays an error stating that each socket address to be used only once.


private const int cmdTextPort = 6666;
   private const int cmdPort = 6676;
   private double tempSensTop = 0;
   private double tempSensBottom = 0;
   private double tempProcessor = 0;
   private String firmware = "";
   private String streamType = "";
   private String camIP = "";





   public override void initializeJob()
   {
       try
       {
           if (TempDevice == temperatureGauge.udp)
           {
               camIP = parameters[0];
               socketCmdText1 = new UdpClient(cmdTextPort);
               socketCmdText1.BeginReceive(new AsyncCallback(OnUdpDataCmdText), socketCmdText1);
               socketCmd1 = new UdpClient(cmdPort);
               socketCmd1.BeginReceive(new AsyncCallback(OnUdpDataCmd), socketCmd1);
           }
           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);
       }
   }

   public override void deinitializeJob()
   {
       try
       {
           if (TempDevice == temperatureGauge.udp)
           {
               socketCmd1.Close();
               socketCmdText1.Close();
           }
           if (TempDevice == temperatureGauge.GMH3700)
           {
               PortGMH3700.Close();
           }
       }
       catch (Exception e)
       {
           Console.WriteLine("Error TemperatureMesurement-Job deinitialization " + jobName + " : " + e.Message);
       }
   }


What I have tried:

This is how I execute the Job



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();
            }
        }
Posted
Updated 1-Oct-19 3:53am
v2
Comments
[no name] 1-Oct-19 10:45am    
I don't know why you need so many sockets ... masters and slaves communicate via addresses; either IP addresses and / or a device id in the header; where a "zero device" might be a broadcast to all.
Member 14589606 1-Oct-19 12:29pm    
it's unicast

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