Link to home
Start Free TrialLog in
Avatar of whorsfall
whorsfallFlag for Australia

asked on

Serial Port Drama

Hi,

I am trying to do some simple comms via C# and a serial port however all is not quite working
as I expect.

I am trying to talk to my inbuilt modem and do a ATI4 <return> and I should get
back "HDA CX11270 Soft Modem" instead I get back the string I have
just sent.

I have included the code below.

Any ideas,

Thanks,

Ward.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;

namespace SerialTest1
{
    class Program
    {
        static void Main(string[] args)
        {

            SerialPort sp = new SerialPort();
            sp.BaudRate = 9600;
            sp.PortName = "COM3";
            sp.DataBits = 8;
            sp.StopBits = StopBits.One;
            sp.Parity = Parity.None;
            sp.Open();
            sp.WriteLine("ATI4\r");
            string received_data = sp.ReadLine();
            sp.Close();

            // I should get back:  HDA CX11270 Soft Modem
            // but instead I get back ATI4

            Console.WriteLine("Data: {0}", received_data);
        }
    }
}

Open in new window

Avatar of effx
effx
Flag of United Kingdom of Great Britain and Northern Ireland image

Have you tride the same command using Hyper Terminal?
Avatar of whorsfall

ASKER

well i am using different terminal software but it does work.
Try:

string received_data = Console.ReadLine();

instead of:

string received_data = sp.ReadLine();

Scrub my last try:

sp.WriteLine("ATI4\r\n");

Instead of:

sp.WriteLine("ATI4\r");
Hi,

Thanks for your response same result

Ward
Avatar of aledev
aledev

Try to put this line

sp.WriteLine("ATE0\r");

before this one

sp.WriteLine("ATI4\r");

and see what happens
ASKER CERTIFIED SOLUTION
Avatar of vusov
vusov
Flag of Ukraine image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If the echo of your modem is enabled, i think is normal that you read "ATI4" instead of  "HDA CX11270 Soft Modem" because the modem sends you back what you write (in this case "ATI4\r") so when you use ReadLine method it retrieves the first line in the buffer and probably the next one could be what you are looking for
Use this command for disable the echo and try again:

ATE0
Hi,

Ok I have made some changes and got it more succesfully working, maybee I could get an explination.

Here is the output.


Data:
HDA CX11270 Soft Modem
OK
 Length: 30


Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;

namespace SerialTest1
{
    class Program
    {
        static void Main(string[] args)
        {

            SerialPort sp = new SerialPort();
            sp.BaudRate = 9600;
            sp.PortName = "COM3";
            sp.DataBits = 8;
            sp.StopBits = StopBits.One;
            sp.Parity = Parity.None;
            sp.Open();
            sp.WriteLine("ATI4\r");

            int bytes = sp.BytesToRead;
            string received_data = sp.ReadExisting();
            int length = received_data.Length;
            sp.Close();

            // I should get back:  HDA CX11270 Soft Modem
            // but instead I get back ATI4

            Console.WriteLine("Data: {0} Length: {1}", received_data,length);

            //Console.ReadKey();
        }
    }
}

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial