drs
drs

Reputation: 133

When sending command to serial port it doesnt work

I'm using a Fax-Modem to get Telephone number in a string.I'm trying to send a command in a serial port. I'm using this code below:

private static string PhoneNumber;
private SerialPort mySerialPort;

private void Form1_Load(object sender, EventArgs e)
{
mySerialPort = new SerialPort("COM3");
mySerialPort.BaudRate = 115200;
mySerialPort.Parity = Parity.None;
mySerialPort.StopBits = StopBits.One;
mySerialPort.DataBits = 8;
mySerialPort.Handshake = Handshake.None;

mySerialPort.Open();

mySerialPort.WriteLine("at#cid=1");//It doesnt recognise this line



mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);     
    }

    private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
    {
        SerialPort sp = (SerialPort)sender;
        string indata = sp.ReadExisting();

       messageBox.Show(indata);
    }

I dont take any error, but the problem is that the command which i type it doesnt work. Normally if it will work it will get me the telephone number, now it doesnt show anything.

If i will type this command in hyperterminal it works fine! So i guess something is wrong with my code.

Upvotes: 1

Views: 711

Answers (2)

drs
drs

Reputation: 133

I used \r after my command and it worked perfectly.thank you for advices!

Here is my code:

    mySerialPort.WriteLine("at#cid=1\r");       

Upvotes: 2

Cooleshwar
Cooleshwar

Reputation: 417

You may want to wire the DataReceived event before opening the port and sending any command.

Upvotes: 0

Related Questions