Solved

Serial Port Drama

Posted on 2010-09-08
10
497 Views
Last Modified: 2013-12-17
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

0
Comment
Question by:whorsfall
  • 3
  • 3
  • 3
  • +1
10 Comments
 
LVL 10

Expert Comment

by:effx
ID: 33625853
Have you tride the same command using Hyper Terminal?
0
 

Author Comment

by:whorsfall
ID: 33625880
well i am using different terminal software but it does work.
0
 
LVL 10

Expert Comment

by:effx
ID: 33625934
Try:

string received_data = Console.ReadLine();

instead of:

string received_data = sp.ReadLine();

0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 10

Expert Comment

by:effx
ID: 33625995
Scrub my last try:

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

Instead of:

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

Author Comment

by:whorsfall
ID: 33626062
Hi,

Thanks for your response same result

Ward
0
 
LVL 3

Expert Comment

by:aledev
ID: 33626654
Try to put this line

sp.WriteLine("ATE0\r");

before this one

sp.WriteLine("ATI4\r");

and see what happens
0
 
LVL 3

Accepted Solution

by:
vusov earned 250 total points
ID: 33636281
Try to convert "ATI4" string to byte array and use sp.Write method
0
 
LVL 3

Expert Comment

by:aledev
ID: 33636501
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
0
 

Author Comment

by:whorsfall
ID: 33655650
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

0
 
LVL 3

Assisted Solution

by:aledev
aledev earned 250 total points
ID: 33656358
when use ReadLine method look at the NewLine property and be sure that it is set to "\r" instead of "\n"
0

Featured Post

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
location of a form 2 25
Help with preventing selection from a combobox 11 35
Help with error in Query 2 31
need to convert function to c# 5 31
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question