Use the MSComm control to display incoming calls through the serial port moden-C # source code

Source: Internet
Author: User

Use the MSComm control to display incoming calls through the serial port moden-C # source code
Note: This code has been debugged

Processing Method
The MSComm control provides two communication methods: one is event-driven, which is equivalent to the interruption Method in general programming. When an event or error occurs in the serial port, the MSComm control will generate an oncomm event.

To capture the event for corresponding processing. Both examples in this article use this method. The other is the query method. In the user program, you can design a timed or irregular query to check whether certain attributes of the MSComm control have changed, so as to determine the corresponding processing.

This method can be used when the program is idle for a long time.
Common attributes and Methods
The key to using the MSComm control to implement computer communication is to understand and correctly set many attributes and methods of the MSComm control. The following are common attributes and methods of the MSComm control:
● Comport: set or return the serial port number.
● Settings: set or return serial communication parameters in the form of strings.
● Portopen: set or return the serial port status.
● Inputmode: set or return the type of received data.
● Inputlen: sets or returns the number of bytes read from the receiving buffer.
● Inbuffersize: set or return the size of the receiving buffer, the default value is 1024 bytes.
● Inbuffercount: set or return the number of characters waiting for receiving by the computer in the receiving buffer.
● Input: reads data from the receiving buffer and clears the buffer. This attribute is designed to be invalid and read-only during runtime.
● Outbuffersize: set or return the size of the sending buffer, which defaults to 512 bytes.
● Outbuffercount: set or return the number of characters waiting for sending by the computer in the sending buffer.
● Output: sends data to the sending buffer. This attribute is designed to be invalid and read-only during running.
● Rthreshold: This attribute is a threshold value. When the number of characters in the receiving buffer reaches this value, the MSComm control sets the commevent attribute to comevreceive and generates an oncomm event. You can perform phase

Should be handled. If the rthreshold attribute is set to 0, no oncomm event is generated. For example, if you want to receive one character in the buffer, you can set rthreshold to 1. In this way, the receiving buffer receives a word.

To generate an oncomm event.
● Sthreshold: This attribute is also a threshold value. When the number of characters in the sending buffer is less than this value, the MSComm control sets the commevent attribute to comevsend and generates an oncomm event. If the sthreshold attribute is set to 0, no

Oncomm event. Note that the oncomm event is generated only when the number of characters in the sending buffer is smaller than the value. For example, sthreshold is set to 3, only when the number of characters in the sending buffer drops from 3

When the value is 2, the MSComm control sets the commevent attribute to comevsend and generates an oncomm event. If the character in the sending buffer is always 2, no oncomm event will be generated. This avoids reverse transmission when data in the sending buffer is not sent.

The oncomm event is resumed.
● Commevent: This is a very important attribute. This attribute is invalid during design and is read-only during running. Once a communication event or error occurs in the serial port, the MSComm control assigns a different generation for the commevent Attribute Based on the event and error.

And generate an oncomm event. The user program can process different codes in the oncomm event handler. For the code, constants, and meanings of the commevent attribute, see table 1 and table 2.
Table 1 meanings of code constants of commevent communication events
1 comevreceive receives rthreshold characters. This event continues until the characters are read and deleted from the accept buffer using the INPUT attribute.
2. The comevsend buffer contains fewer data than sthreshold, indicating that some data has been sent through the serial port. The program can use the output attribute to continue sending data.
3 The status of the comevcts clear to send signal line changes.
4 comevdsr data set ready signal line status changes from 1 to 0.
5. The status of the comevcd carrier detect signal line changes.
6 comevring detects the ringing signal.
7. comeveof receives the file Terminator.

Table 2 commevent communication error code constant meaning
1001 comevntbreak receives an interruption signal.
1002 comevntctsto clear to send times out.
1003 comevntdsrto data set ready signal timed out.
1004 comevntframe frame error.
1006 comevntoverrun serial speed.
1007 comevntcdto Carrier Detection times out.
1008 comevntrxover accept buffer overflow, and there is no space in the buffer.
1009 comevntrxparity error.
1010 the comevnttxfull sending buffer overflows and there is no space in the buffer.
1011 An error occurred while retrieving the Device Control Block of the serial port in comevntdcb.

How to determine whether the modem has the call display function
1. Open the "Super Terminal" software and click "start"-> "program"-> "accessories"-> "communication"-> "Super Terminal"
2. Next, "create a connection". Enter a few characters here.
3. Next, "enter the details of the phone number to be called". You must select the modem you want to test. Do not enter the phone number.

4. Click "dial". Click "cancel". Do not click "dial ".
5. Enter the modem at command
Atz press ENTER
Result: The "OK" prompt is displayed. If you do not see the input content when entering the "atz" command, do not close it. Follow the instructions in the input to see the OK prompt.
At + vcid = 1 Press ENTER
Result: If your modem supports this command, an "OK" prompt is displayed on the screen. If not, an "error" prompt is displayed.
If the error message is displayed, you can change "at + vcid = 1" to "at # cid = 1" and try again. Generally, you can see the "OK" prompt.
If you still receive the "error" command after executing the preceding two commands, check the modem manual to see what commands are used to activate the modem call function.

6. Call another phone number to check whether the incoming call number is displayed on the screen. If yes, your modem supports incoming calls.

Implementation of incoming call display
The at command CID and vcid of modem are used to set whether to display incoming calls.
You can use modem to display the phone number of the other party. Set cid = 1 or vcid = 1

Generally, the following information is displayed:
Date = mmdd "Incoming call date mmdd
Time = hhmm "call time hhmm
Nmbr ######## "Incoming call number

The following code implements the call display function:
Using system;
Using system. drawing;
Using system. collections;
Using system. componentmodel;
Using system. Windows. forms;
Using system. Data;
Using system. Text. regularexpressions;
Using system. xml;

Namespace incomingtelegramdisplay
{Www.elivn.com
/// <Summary>
/// Summary of form1.
/// </Summary>
Public class form1: system. Windows. Forms. Form
{
Private axmscommlib. axmscomm com;
Private system. Windows. Forms. RichTextBox rtfterminal;
/// <Summary>
/// Required designer variables.
/// </Summary>
Private system. componentmodel. Container components = NULL;
Private system. Windows. Forms. Label lblphonenum;
Private system. Windows. Forms. Button button1;
Private system. Windows. Forms. Button button2;
Private system. Windows. Forms. Label label1;
Private string callinfo = "";

Public form1 ()
{
//
// Required for Windows Form Designer support
//
Initializecomponent ();

//
// Todo: add Any constructor code after initializecomponent calls
//
// Initialize the COM port control
Initcomport ();

// Send data out through the COM port
// Com. Output = "Serial terminal initialized ";
Com. Output = "at + vcid = 1" + "\ r ";

}

Private void initcomport ()
{
Try
{
// Set the COM port to be 1
Com. commp ORT = 3;

// This port is already open, close it to reset it.
If (COM. portopen) COM. portopen = false;

// Trigger the oncomm event whenever data is wrongly ed
Com. rthreshold = 1;

Com. sthreshold = 0;
// Set the port to 9600 baud, no parity bit, 8 data bits, 1 stop bit (all standard)
Com. settings = "9600, N ";

// Force the DTr Line High, used sometimes to hang up modems
Com. dtrenable = true;

// No handshaking is used
Com. handshaking = mscommlib. handshakeconstants. comnone;

// Don't mess with byte arrays, only works with simple data (characters A-Z and numbers)
Com. inputmode = mscommlib. inputmodeconstants. cominputmodetext;
// Com. inputmode = mscommlib. inputmodeconstants. cominputmodebinary;


// Use this line instead for byte array input, best for most communications
// Com. inputmode = mscommlib. inputmodeconstants. cominputmodetext;

// Read the entire waiting data when com. input is used
Com. inputlen = 0;

// Don't discard Nulls, 0x00 is a useful byte
Com. nulldiscard = false;

// Attach the event handler
Com. oncomm + = new system. eventhandler (this. oncomm );

// Open the COM port
Com. portopen = true;

// Com. Output = "Serial terminal initialized ";

}
Catch (exception E)
{
MessageBox. Show (E. Message );
}


}

Private void oncomm (Object sender, eventargs e) // mscommlib oncomm event handler
{

This. callinfo = callinfo + COM. Input. tostring ();
This. rtfterminal. Text = This. callinfo;
Infotoken (this. rtfterminal. Text );


}

Private void infotoken (string temp)
{
// Date = 0106
// Time = 0219
// Nmbr = 21156896
// Build a regular expression to match data that

RegEx r = new RegEx (@ "nmbr \ s = \ s [0-9] *");
String num = "";
// Cycle through the matches
For (Match m = R. Match (temp); M. success; M = M. nextmatch ())
{
// Display the result to the 'output' debug window
// This. lblphonenum. Text = M. value;
Num = M. value;

}
Num = num. substring (7 );
This. lblphonenum. Text = num;

}

Private void processcomdata (string input)
{
// Send incoming data to a rich text box
Rtfterminal. appendtext (input + "\ n ");
}

/// <Summary>
/// Clear all resources in use.
/// </Summary>
Protected override void dispose (bool disposing)
{
If (disposing)
{
If (components! = NULL)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}

# Region code generated by Windows Form Designer
/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{
System. Resources. ResourceManager resources = new system. Resources. ResourceManager (typeof (form1 ));
This.com = new axmscommlib. axmscomm ();
This. rtfterminal = new system. Windows. Forms. RichTextBox ();
This. lblphonenum = new system. Windows. Forms. Label ();
This. button1 = new system. Windows. Forms. Button ();
This. button2 = new system. Windows. Forms. Button ();
This. label1 = new system. Windows. Forms. Label ();
(System. componentmodel. isupportinitialize) (this.com). begininit ();
This. suspendlayout ();
//
// Com
//
This.com. Enabled = true;
This.com. Location = new system. Drawing. Point (16,192 );
This.com. Name = "com ";
This.com. ocxstate = (system. Windows. Forms. axhost. State) (resources. GetObject ("com. ocxstate ")));
This.com. size = new system. Drawing. Size (38, 38 );
This.com. tabindex = 0;
//
// Rtfterminal
//
This. rtfterminal. Location = new system. Drawing. Point (24, 24 );
This. rtfterminal. Name = "rtfterminal ";
This. rtfterminal. size = new system. Drawing. Size (240,120 );
This. rtfterminal. tabindex = 1;
This. rtfterminal. Text = "richtextbox1 ";
//
// Lblphonenum
//
This. lblphonenum. Location = new system. Drawing. Point (104,160 );
This. lblphonenum. Name = "lblphonenum ";
This. lblphonenum. size = new system. Drawing. Size (160, 23 );
This. lblphonenum. tabindex = 2;
This. lblphonenum. Text = "label1 ";
//
// Button1
//
This. button1.location = new system. Drawing. Point (96,208 );
This. button1.name = "button1 ";
This. button1.tabindex = 3;
This. button1.text = "button1 ";
This. button1.click + = new system. eventhandler (this. button#click );
//
// Button2
//
This. button2.location = new system. Drawing. Point (192,208 );
This. button2.name = "button2 ";
This. button2.tabindex = 4;
This. button2.text = "Record Number ";
This. button2.click + = new system. eventhandler (this. button2_click );
//
// Label1
//
This. label1.location = new system. Drawing. Point (24,160 );
This. label1.name = "label1 ";
This. label1.size = new system. Drawing. Size (72, 23 );
This. label1.tabindex = 5;
This. label1.text = "Incoming call number :";
//
// Form1
//
This. autoscalebasesize = new system. Drawing. Size (6, 14 );
This. clientsize = new system. Drawing. Size (288,246 );
This. Controls. Add (this. label1 );
This. Controls. Add (this. button2 );
This. Controls. Add (this. button1 );
This. Controls. Add (this. lblphonenum );
This. Controls. Add (this. rtfterminal );
This. Controls. Add (this.com );
This. Name = "form1 ";
This. Text = "form1 ";
This. Load + = new system. eventhandler (this. form#load );
(System. componentmodel. isupportinitialize) (this.com). endinit ();
This. resumelayout (false );

}
# Endregion

/// <Summary>
/// Main entry point of the application.
/// </Summary>
[Stathread]
Static void main ()
{
Application. Run (New form1 ());
}

Private void form1_load (Object sender, system. eventargs E)
{

}

Private void button#click (Object sender, system. eventargs E)
{
String A = "date = 0106 time = 0219 nmbr = 21156896 ";
Infotoken ();
}

Private void button2_click (Object sender, system. eventargs E)
{
String filename = ".. \ incomingphonenumber. xml ";
Xmltextwriter Tw = new xmltextwriter (filename, null );
Tw. Formatting = formatting. indented;
Tw. writestartdocument ();

Tw. writestartelement ("incomingcall ");
Tw. writeelementstring ("phonenumber", this. lblphonenum. Text );

Tw. writeendelement ();
Tw. writeenddocument ();
Tw. Flush ();
Tw. Close ();
}
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.