Available online at www.sciencedirect.com Physics Procedia 25 (2012 ) 1397 1404 2012 International Conference on Solid State Devices and Materials Science The Application of Multi-threaded Socket and Picture Field Technology of DataGridView in the Development of Alarm Receiver Software Guo hao The Computer Science and Technology College,Taiyuan University of Technology,Shanxi, China 030024 Abstract In this paper, we achieve the alarm receiving software and discuss programming ideas of the multi-threaded asynchronous receiver about SOCKET and picture field technology of DataGridView in the process of development. 2012 2011 Published by Elsevier Ltd. B.V. Selection and/or peer-review under responsibility of of [name Garry organizer] Lee Open access under CC BY-NC-ND license. Keywords-VS 2005, C#, SOCKET, multi-thread, asynchronous reception, DataGridView, picture field 1. Introduction Alarm receiving software is a kind of common functional block and used in many systems. Its main function typically includes receiving the alarm information, displaying alarm information, sounding the alarm tone, prohibiting alarm tone, processing alarm status, poping up window, generating log, reminding email/sms, inquiring police record and so on. In this paper, we focuse on the implement of receiving and displaying alarm information, playing and stopping sound, and several other features. 2. Main Functions 2.1 Receiving the alarm information The alarm information usually has a number of alarm generated sources, these sources randomly generate alarm information, send to the receiver. Sending process is as follows: alarm generated source sends SOCKET connection requests with IP and specified port to the receiver; After a successful connection, it sends a data packet; After the success of packet transmission, the connection is disconnect. 1875-3892 2012 Published by Elsevier B.V. Selection and/or peer-review under responsibility of Garry Lee Open access under CC BY-NC-ND license. doi:10.1016/j.phpro.2012.03.252
1398 Guo hao / Physics Procedia 25 ( 2012 ) 1397 1404 Because generation sources may send alarm information at the same time, the receiver must take multithreaded asynchronous receiver to treat information. Multi-threaded asynchronous receiver refers to that the receiver listens port in the loop, when it receives a connection request, it starts a new thread to receive the data, analyze data packet, display alarm information, write the log records, etc., the main thread continues to monitor port. In the realization of more complex data receiver, data receiving and processing need to be separated into two threads, that is to say, it will start another asynchronous process. In this paper, the length of data package is short, the structure is relatively simple, so we eliminate confirming IP, establishing SESSION, verifying the checksum of data packets, parsing the data packet, merging related data packet and other steps, don't to start new thread. 2.2 Displaying the alarm information Successfully received alarm information will be displayed in the DataGridView, including processing status, alarm sound status, receiving time, alarm content and so on. Processing status and alarm sound status are image field, the initial state are (pending alarm), (alarm sound). 2.3 Playing and stopping the alarm tone The field of alarm sound state is a picture field, default is to send alarm tone, this is to stop the alarm tone, The field of alarm sound status is changed to stop the alarm tone, the field's Tag property is set to False. When all the records of alarm sound status in DataGridView are banned status, it will stop the alarm sound, or play alarm sound. The control of sound uses System.Media class in the SoundPlayer. This class is a new class in the. NET Framework 2.0 version. It is easy to include sound in the application software by using the SoundPlayer class. SoundPlayer class provides simple interface to load and play.wav files. SoundPlayer class supports Stream to contain.wav file from the file path, URL, or load.wav file from embedded resource which includes.wav files. 2.4 Status of alarm processing The field of alarm processing status is image field, default is raw alarm information which is need to be displayed. Clicking on the icon means that the operator has dealt with this alarm, the system will log the processing time and remove this record from the DataGridView. 3. Implementation 3.1 Boot screen The boot screen includes two Textbox of user name and password, the login button, DataGridView (control name dg, four fields: processing, sound, time, alarm content)which display the alarm information. Figure 1 is a form of startup screen.
Guo hao / Physics Procedia 25 (2012 ) 1397 1404 1399 Figure 1. A form of startup screen 3.2 The main thread You enter the user name, password and click login, if the information is right, the button at the top changes. It initiates the Socket instance of receive, and starts two threads, one thread of ththreadsound detects whether to play the alarm sound, another thread of ththreadlisten monitors a specified port. receive = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //start a Socket instance IPe = new IPEndPoint(IPAddr, port); //initate IPEndPoint instance receive.bind(ipe);//connect Socket with IPEndPoint receive.listen(15);//listen ththreadsound = new Thread(new ThreadStart(SoundPlay)); ththreadsound.start(); //start the thread of playing alarm sound ththreadlisten = new Thread(new ThreadStart(Listen)); ththreadlisten.start();//start listening thread 3.3 The thread of playing alarm sound SoundPlay () which is the thread of playing alarm sound looply detects every Tag property of sound field in DataGridView to determine to play or stop alarm sound. It calculates number that which Tag property is True in per cycle, if the number is greater than 0, sound file is played, if the number is equal to 0, sound file is stopped. Each test was suspended 5 seconds. public static SoundPlayer sound = new SoundPlayer();//initiate playing sound instance private void SoundPlay() Thread.CurrentThread.IsBackground = true;
1400 Guo hao / Physics Procedia 25 ( 2012 ) 1397 1404 // Specify the current thread as background thread DataGridViewRowCollection ls; int acount = 0; sound.soundlocation = "Alarm.wav"; //specify sound file while (runtype) ls = dg.rows; acount = 0; foreach (DataGridViewRow dgr in ls) //check each row in DataGridView if (dgr.cells[1].tag == null) //if this is a new row dgr.cells[1].tag = true;//assign True to Tag if ((bool)dgr.cells[1].tag) acount++; //calculate records number which should play sound if (acount > 0) sound.play();//play sound else sound.stop();//stop sound Thread.Sleep(5 * 1000);//sleep 5s 3.4 Monitor thread Monitor thread looply detects the new connection of Socket instance, it will start ReceiveTh which is the processing thread after receiving the connection request. private void Listen() Thread.CurrentThread.IsBackground = true; // Specify the current thread as background thread // while (true) Socket accept = receive.accept(); //start Socket for the new connection Thread receiveth = new Thread(new ParameterizedThreadStart(ReceiveTh)); receiveth.start(accept); //start receiving and processing thread 3.5 Receiving and processing thread Receiving and processing thread detects data receive buffer of Socket connection, receive the data transfers received data to adddg which is displaying and processing program in DataGridView and then closes the connection. Here we should note the received data 's encoding, we can force the specified
Guo hao / Physics Procedia 25 (2012 ) 1397 1404 1401 string to be encoded as GB2312 by GetEncoding ("GB2312"), if you do not specify the source code information, some warning alarms message may be displayed as garbled. private void ReceiveTh(object asocket) Thread.CurrentThread.IsBackground = true; // Specify the current thread as background thread Socket receive = (Socket)asocket; byte[] buffer = new byte[1024]; // Establish a data receive buffer string rstr; int scount = 0; while (true) try scount = receive.receive(buffer); // Get length of receive data catch if (scount > 0) rstr = Encoding.GetEncoding("GB2312").GetString(buffer, 0, scount);// Access to //alarm information data adddg(rstr);//process data else receive.close();//close Socket connection break; 3.6 Display processing of DataGridView Display processing program of DataGridView runs in ReceiveTh which is a receiving and processing thread, while DataGridView runs in the main thread. The program after NET2.0 enhances security mechanisms, does not allow WINFORM to access control properties across threads. So when you need to update DataGridView, you should use delegate. Figure 2 is the screen that DataGridView is updated after receiving alarm information,the system starts to play the alarm sound at the moment. delegate void SetDgvCallback(string text); private void adddg(string disp) if (this.dg.invokerequired) // Check whether the control is idle //The control is busy SetDgvCallback d = new SetDgvCallback(adddg); //Wait for rerunning this.invoke(d, new object[] disp ); //Pass parameters
1402 Guo hao / Physics Procedia 25 ( 2012 ) 1397 1404 else //The control is idle try dg.rows.insert(0, new object[] dispalarm.properties.resources.alarm, dispalarm.properties.resources.sound, DateTime.Now, disp );//Add a new record in DataGridView dg.refresh(); //Refresh the display of DataGridView control catch (Exception e) MessageBox.Show(e.ToString()); //Display error message Figure 2. display screen 3.7 Stopping the alarm tone The CellContentClick events in DataGridView can trigger to stop the alarm tone. The operator clicks the sound field in a row and sets the picture in the sound field as, and tag to false. Figure 3 is the display screen after the operator clicks, then the sound is stopped. if (e.columnindex == 1) // Determine whether the alarm status field is clicked dg.rows[e.rowindex].cells[e.columnindex].value = dispalarm.properties.resources.nosound; // update the display image in the field dg.rows[e.rowindex].cells[e.columnindex].tag = false;//update Tag property
Guo hao / Physics Procedia 25 (2012 ) 1397 1404 1403 Figure 3. display screen after the sound field is clicked 3.8 Changing the alarm process status When the operator clicks the status bar of a row, the system writes the processing time of the alarm record to the log and deletes this record from the DataGridView. Figure 4 is the display screen after the operator clicks. if (e.columnindex == 0) // Determine whether the treatment status field was clicked writelog(dg.rows[e.rowindex].cells[3]); // write the processing time of the alarm to log dg.rows.remove(dg.rows[e.rowindex]); // Delete the record in the DataGridView Figure 4. display screen after the status bar is clicked 4. Conclusion
1404 Guo hao / Physics Procedia 25 ( 2012 ) 1397 1404 The alarm receiving software has been tested and no packet loss, functions could meet demands. I believe that through this practice, multi-threaded asynchronous receiver technology of SOCKET can deal with complex receive requirements. Of course, there are some details that need to pay attention to the reader in practical applications, such as: data in DataGridView written to the database, the mode of written logs, due to space limitations, we can not discuss here. References [1] Zhou Cunjie, C# network programming tutorial examples, Beijing Hope Electronic Press, 2002.7 [2] Su Guiyang, ASP.NET network programming from basic to practice, Electronic Industry Press, 2005.10 [3] Ye Shuhua, Gao Zhihong, practical tutorials of network programming, Posts & Telecom Press, 2006.10