SerialPort enables the display of incoming calls from the modem to the SerialPort to display incoming calls from the modem.

Source: Internet
Author: User
SerialPort enables modem incoming call display

A friend took a case, but had to go on a business trip temporarily, so I called some functions for me to do it. I have always paid attention to WEB programming, but I am still unfamiliar with serial programming.ArticleA lot. Of course, it is better to count the articles in cnblogs for help. Write this into your blog for future reference, hoping to help those who need help.
This section mainly describes how to use serailport to display incoming calls of modem.
The incoming call display depends on the specific modem. Different modem may have different AT commands. Therefore, you need to define an interface to applyProgramDecoupling from hardware.


Public   Interface Imodem
{
Void Open ();
Void Close ();
Bool Supportat
{
Get ;
}
Bool Supportcid
{
Get ;
}
Event Eventhandler < Ringeventargs > Ring;
}
This interface defines the Open and Close Methods, whether at commands are supported, whether incoming call display is supported, and a bell event. Ringeventargs contains the phone number of the incoming call.
When writing the implementation class of this imodem, we use the modem of the notebook. If we do not know the model, we can name it directly using the modem.

Public   Class Modem: imodem
{
Public   Event Eventhandler < Ringeventargs > Ring;
System. Io. Ports. SerialPort =   Null ;

Public Modem ()
{
Port =   New System. Io. Ports. SerialPort ();
Port. portname = Settings. instance. getvalue ( " Portname " , " Com3 " );
Port. baudrate = Settings. instance. getvalue ( " Baudrate " , 460800 );
Port. databits =   8 ;
Port. Parity = Parity. None;
Port. stopbits = Stopbits. One;
Port. readtimeout =
Port. writetimeout =   1000 ; // 1 s

Port. readbuffersize =
Port. writebuffersize =   1024 ; // 1 K

Port. handshake = Handshake. None;
Port. receivedbytesthreshold =   10 ;
Port. rtsenable =   True ;
Port. dtrenable =   True ;
Port. newline =   " \ R " ;

This. Port. datareceived+ = NewSystem. Io. Ports. serialdatareceivedeventhandler (port_datareceived );
}

The above is the port configuration. Different modem configurations may be different and must be set according to the actual modem.


Public   Void Open ()
{
If ( ! This . Port. isopen)
This . Port. open ();
If ( ! Supportat)
Throw   New Exception ( " The device does not support the AT command. " );
If ( ! Supportcid)
Throw   New Exception ( " The specified device does not support explicit display. " );
}
Public   Void Close ()
{
If ( This . Port. isopen)
This . Port. Close ();
}
Public   Bool Supportat
{
Get
{
This . Port. writeline ( " At " );
System. Threading. thread. Sleep ( 500 );
String Result =   This . Port. readexisting ();
Return Result. toupper (). Contains ( " OK " );
}
}
Public   Bool Supportcid
{
Get
{
String [] Commandlist = Settings. instance. sectionvalues ( " CID " );
String Result =   String . Empty;
Foreach ( String Command In Commandlist)
{
This . Port. writeline (command );
System. Threading. thread. Sleep ( 500 );
Result =   This . Port. readexisting ();
If (Result. toupper (). Contains ( " OK " ))
{
Return   True ;
}
}
Return   False ;
}
}
This is the implementation of some methods and attributes. It should be noted that after the instruction is sent, it is best to block the thread for a period of time to ensure that the hardware has enough time to respond, so as to ensure that the complete response information can be received. Here system. Threading. thread. Sleep ( 500 .


Void Port_datareceived ( Object Sender, system. Io. Ports. serialdatareceivedeventargs E)
{
String Phonenumber =   This . Port. readexisting ();
RegEx R =   New RegEx ( @" Nmbr \ s * = \ s * [0-9] * " );
Match m = R. Match (phonenumber );

String Result =   String . Empty;
If (M ! =   Null   && M. Success)
{
Result = M. value;
Result = Result. substring (result. indexof ( " = " ) +   1 ). Trim ();
}
If ( String . Isnullorempty (result ))
Return ;
If (Ring ! =   Null )
{
Ringeventargs eventargs =   New Ringeventargs ();
Eventargs. phonenumber = Result;
Foreach (Eventhandler < Ringeventargs > Handler In Ring. getinvocationlist ())
{
Handler. Invoke ( This , Eventargs );
If (Eventargs. Handled)
Break ;
}
}
}
Ring
The Ring Event is actually handled based on the data received by datareceived, such as determining whether it is a call, intercepting the incoming call number, and then bubbling to trigger the Ring Event. Different
The values returned by modem may be different. The above regular expression may need to be modified again. (I found a space on the Internet between the left and right of =, but the modem I debugged does not exist)

SerialPort is. NET 2.0. MSComm may be required for. NET 1.1.

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.