whorsfall
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.
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);
}
}
}
Have you tride the same command using Hyper Terminal?
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();
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");
sp.WriteLine("ATI4\r\n");
Instead of:
sp.WriteLine("ATI4\r");
ASKER
Hi,
Thanks for your response same result
Ward
Thanks for your response same result
Ward
Try to put this line
sp.WriteLine("ATE0\r");
before this one
sp.WriteLine("ATI4\r");
and see what happens
sp.WriteLine("ATE0\r");
before this one
sp.WriteLine("ATI4\r");
and see what happens
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
Use this command for disable the echo and try again:
ATE0
ASKER
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
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();
}
}
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.