1 Replies - 1912 Views - Last Post: 25 June 2009 - 10:29 PM Rate Topic: -----

#1 Lecutus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 24-June 09

how to change the modifier of a serial port in c# from public to stati

Posted 24 June 2009 - 12:07 PM

I'm using VS2005, C#

The situ is I'm building an app that has five children, each gets a time slice of the rs232. Each child form represents a truck, so each situation is slightly different because of the monitoring/control factors. But each form is built the same.

I would like to keep all the send/recieve/process in the childform itself and use the Parent form as the conduit to the rs232

The code for the child form is as follows:
public void SEND_SETTINGS_Click() //object sender, EventArgs e
		{
			SerialStart_Check(); //this,e
			//Gear_Translator();
			Date_Update.Text = Convert.ToString(DateTime.Now);
			dt = DateTime.Now;
			TRK_ID_pac = this.Name;
			trx.TRUCK_ID = this.Name;
			trx.TRUCK_NUM = this.Name;
			//s_trxx = 0;
			Performance_Log_File(">>>>>>>>>>>>>>>>>>>>>" + this.Text, " ENTERING SEND_SETTINGS_Click \r\n");
			MainControllerMDI.Performance_Log_File(" TO >>>>>>>>>>>>>>>>>>>>>" + this.Text, " ENTERING SEND_SETTINGS_Click \r\n");
			Pac_Diff_Check(); // used to check difference of incoming and present data
	  
			/* below is for the individual trucks/child*/
			// this goes out to controller/controls
			port_out = "!!!" + RPM_pac + ";" + GEAR_pac + ";" + OVER_PRESS_pac + ";" + SWITCH_pac + ";" + TRK_ID_pac + "&&&";
			MessageBox1.Text = port_out + " ENGINE INFO SENT " + dt + "\r\n" + Serial_Port1.PortName + Globals.ErrorMess41 + archTime;
			Performance_Log_File(this.Name, MessageBox1.Text);
			MainControllerMDI.Performance_Log_File(this.Name, MessageBox1.Text);
			
			Serial_Port1.Write(port_out); // <<< this should also be to rs232 of parent

			Diag_Box.Text = "B4 Parse" + trx.TRUCK_ID + " trx.RPM_var = " + trx.RPM_var + " RPM_pac= " + RPM_pac + " trx.gear_var = " + trx.GEAR_var + " GEAR_pac = " + GEAR_pac + " trx.OVER_PRESS_var = " + trx.OVER_PRESS_var + " ev.OVER_PRESS_pac = " + OVER_PRESS_pac;
			/* above is for the individual trucks/child*/

			//port_in = Serial_Port1.ReadLine(); <<<------ DON"T USE THIS
			//port_in = Serial_Port1.ReadTo("!");
			//port_in = Serial_Port1.ReadExisting();
			  port_in = MainControllerMDI.m  <<< RIGHT HERE is where  I  would like to call the parent form


			 //Serial_Port1.Read(port_in);
			if (port_in == "" | port_in == null)
			{
				Performance_Log_File(this.Name+" is empty", " port_in = " + port_in);
				MainControllerMDI.Performance_Log_File(this.Name + " is empty", " port_in = " + port_in);
			}
			else
			{
				Performance_Log_File(this.Name, " port_in = " + port_in);
				MainControllerMDI.Performance_Log_File("FROM " + this.Text, " has in port_in = " + port_in);

				Process_Cycle();//this, e
				
			}
   
			//Process_Cycle(this,e);
			trck_id_pac_box.Text = Convert.ToString(TRK_ID_pac); Truck_id_Box.Text = trx.TRUCK_ID; Truck_Num_box.Text = Convert.ToString(trx.TRUCK_NUM);
			Serial_Port1.DiscardInBuffer();
			Serial_Port1.DiscardOutBuffer();

			Serial_Port1.Dispose();
			GC.Collect();
			port_in = "";
			Performance_Log_File("<<<<<<<<<<<<<<<<<<<<<<<<" + this.Text, " LEAVING SEND_SETTINGS_Click \r\n");
			MainControllerMDI.Performance_Log_File("<<<<<<<<<<<<<<<<<<<<<<<<" + this.Text, " LEAVING SEND_SETTINGS_Click \r\n");
		}




The parent form looks like this:

 public void SerialStart_Check()//object sender, EventArgs e
		{
			// Makes sure serial port is open before trying to write 

			dt = DateTime.Now;
			try
			{
				if (!Main_Serial_Port1.IsOpen)
				{
					Main_Serial_Port1.Dispose();
					Main_Serial_Port1.Open();
					Communication_Display.Text = Main_Serial_Port1.PortName + " " + dt;
					archTime = dt;
				}
				else
				{
					if (Main_Serial_Port1.IsOpen)
					{
						Communication_Display.Text = "port " + Main_Serial_Port1.PortName + " already opened " + archTime;
						//Performance_Log_File(this.Name, Communication_Display.Text);
					}
				}
			}
			catch (Exception ex)
			{
				Communication_Display.Text = "Error opening/writing to serial port :: " + ex;
				Performance_Log_File(this.Name, Communication_Display.Text);
			}
		}



   public void Truck_Send_Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) //System.Timers.ElapsedEventArgs e
		{
			// If there are child forms in the parent form, this rotates thru them.
			DirEnd_Display.Text = Convert.ToString(DateTime.Now);
			SerialStart_Check();
			Act_Chld_Frm_Display.Text = Convert.ToString(this.MdiChildren.Length);
			Indiv_Truck_Cntrl.child_cnt = this.MdiChildren.Length;
			if (trxx < trx_ttl_cnt)
			{
				// Create a temporary instance of a child form (Indiv_Truck_Control and Monitor in this case).
				Indiv_Truck_Cntrl tempChild = (Indiv_Truck_Cntrl)this.MdiChildren[trxx];
				Truck_Active.Text = tempChild.Name;
				
				tempChild.SEND_SETTINGS_Click();  //tempChild.Name,e, goes to Indiv_Truck rat here!!!!! Pixup port_out
				trxx++;
			}
			else
			{
				//this resets the counters
				trxx = 0;
			  }
		   
			Main_Serial_Port1.DiscardInBuffer();
			Main_Serial_Port1.DiscardOutBuffer();
			////Main_Serial_Port1.Dispose();
			GC.Collect();

		}



At this point from the child form I can't seem to ask for the serial port. If I could get the port itself to be static that would be great.

I've tried calling each child from inside the parent and that didn't go so well: I send from one child and get info for another as rsr232 is asychronous.

Any and all suggestions are welcome. This is probably one of those "bleery eyed, I should have saw that" kind of a thing.

Thnaks
L.

Is This A Good Question/Topic? 0
  • +

Replies To: how to change the modifier of a serial port in c# from public to stati

#2 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: how to change the modifier of a serial port in c# from public to stati

Posted 25 June 2009 - 10:29 PM

View PostLecutus, on 24 Jun, 2009 - 01:07 PM, said:

I'm using VS2005, C#

The situ is I'm building an app that has five children, each gets a time slice of the rs232. Each child form represents a truck, so each situation is slightly different because of the monitoring/control factors. But each form is built the same.

I would like to keep all the send/recieve/process in the childform itself and use the Parent form as the conduit to the rs232

The code for the child form is as follows:
public void SEND_SETTINGS_Click() //object sender, EventArgs e
		{
			SerialStart_Check(); //this,e
			//Gear_Translator();
			Date_Update.Text = Convert.ToString(DateTime.Now);
			dt = DateTime.Now;
			TRK_ID_pac = this.Name;
			trx.TRUCK_ID = this.Name;
			trx.TRUCK_NUM = this.Name;
			//s_trxx = 0;
			Performance_Log_File(">>>>>>>>>>>>>>>>>>>>>" + this.Text, " ENTERING SEND_SETTINGS_Click \r\n");
			MainControllerMDI.Performance_Log_File(" TO >>>>>>>>>>>>>>>>>>>>>" + this.Text, " ENTERING SEND_SETTINGS_Click \r\n");
			Pac_Diff_Check(); // used to check difference of incoming and present data
	  
			/* below is for the individual trucks/child*/
			// this goes out to controller/controls
			port_out = "!!!" + RPM_pac + ";" + GEAR_pac + ";" + OVER_PRESS_pac + ";" + SWITCH_pac + ";" + TRK_ID_pac + "&&&";
			MessageBox1.Text = port_out + " ENGINE INFO SENT " + dt + "\r\n" + Serial_Port1.PortName + Globals.ErrorMess41 + archTime;
			Performance_Log_File(this.Name, MessageBox1.Text);
			MainControllerMDI.Performance_Log_File(this.Name, MessageBox1.Text);
			
			Serial_Port1.Write(port_out); // <<< this should also be to rs232 of parent

			Diag_Box.Text = "B4 Parse" + trx.TRUCK_ID + " trx.RPM_var = " + trx.RPM_var + " RPM_pac= " + RPM_pac + " trx.gear_var = " + trx.GEAR_var + " GEAR_pac = " + GEAR_pac + " trx.OVER_PRESS_var = " + trx.OVER_PRESS_var + " ev.OVER_PRESS_pac = " + OVER_PRESS_pac;
			/* above is for the individual trucks/child*/

			//port_in = Serial_Port1.ReadLine(); <<<------ DON"T USE THIS
			//port_in = Serial_Port1.ReadTo("!");
			//port_in = Serial_Port1.ReadExisting();
			  port_in = MainControllerMDI.m  <<< RIGHT HERE is where  I  would like to call the parent form


			 //Serial_Port1.Read(port_in);
			if (port_in == "" | port_in == null)
			{
				Performance_Log_File(this.Name+" is empty", " port_in = " + port_in);
				MainControllerMDI.Performance_Log_File(this.Name + " is empty", " port_in = " + port_in);
			}
			else
			{
				Performance_Log_File(this.Name, " port_in = " + port_in);
				MainControllerMDI.Performance_Log_File("FROM " + this.Text, " has in port_in = " + port_in);

				Process_Cycle();//this, e
				
			}
   
			//Process_Cycle(this,e);
			trck_id_pac_box.Text = Convert.ToString(TRK_ID_pac); Truck_id_Box.Text = trx.TRUCK_ID; Truck_Num_box.Text = Convert.ToString(trx.TRUCK_NUM);
			Serial_Port1.DiscardInBuffer();
			Serial_Port1.DiscardOutBuffer();

			Serial_Port1.Dispose();
			GC.Collect();
			port_in = "";
			Performance_Log_File("<<<<<<<<<<<<<<<<<<<<<<<<" + this.Text, " LEAVING SEND_SETTINGS_Click \r\n");
			MainControllerMDI.Performance_Log_File("<<<<<<<<<<<<<<<<<<<<<<<<" + this.Text, " LEAVING SEND_SETTINGS_Click \r\n");
		}




The parent form looks like this:

 public void SerialStart_Check()//object sender, EventArgs e
		{
			// Makes sure serial port is open before trying to write 

			dt = DateTime.Now;
			try
			{
				if (!Main_Serial_Port1.IsOpen)
				{
					Main_Serial_Port1.Dispose();
					Main_Serial_Port1.Open();
					Communication_Display.Text = Main_Serial_Port1.PortName + " " + dt;
					archTime = dt;
				}
				else
				{
					if (Main_Serial_Port1.IsOpen)
					{
						Communication_Display.Text = "port " + Main_Serial_Port1.PortName + " already opened " + archTime;
						//Performance_Log_File(this.Name, Communication_Display.Text);
					}
				}
			}
			catch (Exception ex)
			{
				Communication_Display.Text = "Error opening/writing to serial port :: " + ex;
				Performance_Log_File(this.Name, Communication_Display.Text);
			}
		}



   public void Truck_Send_Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) //System.Timers.ElapsedEventArgs e
		{
			// If there are child forms in the parent form, this rotates thru them.
			DirEnd_Display.Text = Convert.ToString(DateTime.Now);
			SerialStart_Check();
			Act_Chld_Frm_Display.Text = Convert.ToString(this.MdiChildren.Length);
			Indiv_Truck_Cntrl.child_cnt = this.MdiChildren.Length;
			if (trxx < trx_ttl_cnt)
			{
				// Create a temporary instance of a child form (Indiv_Truck_Control and Monitor in this case).
				Indiv_Truck_Cntrl tempChild = (Indiv_Truck_Cntrl)this.MdiChildren[trxx];
				Truck_Active.Text = tempChild.Name;
				
				tempChild.SEND_SETTINGS_Click();  //tempChild.Name,e, goes to Indiv_Truck rat here!!!!! Pixup port_out
				trxx++;
			}
			else
			{
				//this resets the counters
				trxx = 0;
			  }
		   
			Main_Serial_Port1.DiscardInBuffer();
			Main_Serial_Port1.DiscardOutBuffer();
			////Main_Serial_Port1.Dispose();
			GC.Collect();

		}



At this point from the child form I can't seem to ask for the serial port. If I could get the port itself to be static that would be great.

I've tried calling each child from inside the parent and that didn't go so well: I send from one child and get info for another as rsr232 is asychronous.

Any and all suggestions are welcome. This is probably one of those "bleery eyed, I should have saw that" kind of a thing.

Thnaks
L.


Can you post the variables in your parent form? Is port_out static in the parent form? If it is static in the parent form you should be able to access it from the child form as: ParentFormName.port_out
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1