I have to call phone number and detect if the modem at the op开发者_JS百科posite side is hang-on. How can I do this in C# with SerialPort?
Yes, System.IO.Ports.SerialPort is the class to use.
Something like this:
// Set the port name, baud rate and other connection parameters you might need
SerialPort port = new SerialPort("COM1", 9600 );
port.Open();
port.ReadTimeout = 1000;
port.NewLine = "\r";
port.WriteLine("ATZ"); // reset the modem
port.ReadTo("OK\r\n"); // wait for "OK" from modem
port.WriteLine("ATDT 12345678"); // dial number with dialtone
string response = port.ReadTo("\r").Trim(); // read until first newline
port.Close();
It's not tested as I don't have a modem at hand.
You could create a connection in windows that is configured correctly (so you could manually dial it). Then use the RAS API to dial the connection an check the result.
精彩评论