I coded a program to dial automatically when phone is connected to the laptop and get the last call duration.I used AT+CLCC command to get current call status..Though it should return the Some string value as .......etc i got nothing like that so far...Here is my code..
_serialPort.BaudRate = 9600;
_serialPort.Parity = Parity.None;
_serialPort.DataBits = 8;
_serialPort.StopBits = StopBits.One;
_serialPort.Handshake = Handshake.None;
// Set the read/write timeouts
_serialPort.ReadTimeout = 500;
_serialPort.WriteTimeout = 500;
_serialPort.Open();
_serialPort.DtrEnable = true;
_serialPort.RtsEnable = true;
string phonenr = "";
// string mesaj;
if (!_serialPort.IsOpen)
{
_serialPort.Open();
}
_serialPort.WriteLine("AT\r");
{
Console.WriteLine("Enter the phone number:", phonenr);
phonenr = Console.ReadLine();
_serialPort.WriteLine("ATD" + phonenr + ";" + "\r");
Console.WriteLine("Ring...");
Thread.Sleep(10000);
_serialPort.WriteLine("AT+CLCC");
_serialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
//As a seperate function....
private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string indata = sp.ReadExisting();
Console.WriteLine("Data Received:");
Console.Write(indata);
}
What is the wrong with this code????How can i get the response as the format ...etc ???
AT+CLCC command does not provide information about the last call. It provides information during a call (during dialing/ringing/waiting etc). Read this for detailed information
I think you can make the phone to output last call details automatically to the terminal, when the call is disconnected but I'm not sure if it provides call duration. You might have to monitor/record the time manually with your application
I have seen other posts where you have asked similar questions. I would recommend using a simple serial port terminal (putty or terminal etc.) to communicate with the phone and grasp the AT commands concept, before moving on to controlling the phone using your own code.
Related
EDIT: Okay, so I've moved on and made a fully working console app:
public static void Main(string[] args)
{
SerialPort mySerialPort = new SerialPort("COM4");
mySerialPort.BaudRate = 9600;
mySerialPort.Parity = Parity.None;
mySerialPort.StopBits = StopBits.One;
mySerialPort.DataBits = 8;
mySerialPort.Handshake = Handshake.None;
mySerialPort.RtsEnable = true;
mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
mySerialPort.Open();
Console.WriteLine("Press any key to continue...");
Console.WriteLine();
Console.ReadKey();
mySerialPort.Close();
}
private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
Thread.Sleep(100);
SerialPort sp = (SerialPort)sender;
string indata = sp.ReadExisting();
Console.WriteLine($"Data Received: {indata}");
indata = "";
}
Now the problem is that when I try to do something similar in the UWP app -
public void InitScanner()
{
SerialPort mySerialPort = new SerialPort("COM4")
{
BaudRate = 9600,
Parity = Parity.None,
StopBits = StopBits.One,
DataBits = 8,
Handshake = Handshake.None,
RtsEnable = true
};
mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
mySerialPort.Open();
tBlock_spState.Text = mySerialPort.IsOpen.ToString();
}
void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
Thread.Sleep(100);
SerialPort sp = (SerialPort)sender;
string indata = sp.ReadExisting();
tBlock_test.Text = indata;
}
public MainPage()
{
this.InitializeComponent();
InitScanner();
}
It throws an System.IO.IOException with description "Too many posts on a semaphore" (sorry for the translation, I am using VS with Czech language). Does anyone know the reason why is this happening? Once again, can't find anything anywhere.
So, I've got a task to write a code, which could allow to read data from a barcode scanner that does not act like a keyboard and write it to a textbox. My thought was that it could be possible via System.IO.Ports.SerialPort class, but I have absolutely no idea how to make it work as it should. I am working on this in an UWP. I've tried this so far
SerialPort sp = new SerialPort("COM4", 9600);
void MethodName()
{
string s;
if(!sp.IsOpen)
sp.Open();
while (sp.BytesToRead > 0)
{
s = sp.ReadLine();
tBlock_test.Text = s;
}
sp.Close();
}
And that is where I've ended and don't know what to do next, or even if this is somehow correct. Again, basic question, i know, but i am seriously stuck on this and cannot find solution anywhere. I'd appreciate any kind of help.
I've remade the app in the WPF template and tried the answer from here: SerialPort reading cause error because of not owning thread (Delegate + Invoke method) and it finally worked.
I'm trying to read data from COM port, but I'm stuck with this issue so long.
We're having some USB serial port device which is running on COM4 port, with that we have some predefined commands to execute and also there are some predefined outputs.
Our requirements is to send command to the COM4 port and read result.
Here what I have tried:
class Program
{
[STAThread]
static void Main(string[] args)
{
string cmd = "PING";
SerialPort mySerialPort = new SerialPort("COM4");
mySerialPort.BaudRate = 115200;
mySerialPort.Parity = Parity.None;
mySerialPort.StopBits = StopBits.One;
mySerialPort.DataBits = 8;
mySerialPort.Handshake = Handshake.None;
mySerialPort.DtrEnable = true;
mySerialPort.RtsEnable = true;
mySerialPort.Open();
mySerialPort.Write(cmd);
mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
Console.WriteLine(mySerialPort.ReadChar());
Console.WriteLine("Press any key to continue...");
Console.WriteLine();
Console.ReadKey();
mySerialPort.Close();
}
}
private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string data = sp.ReadExisting();
Console.WriteLine(data);
}
But It's stuck with black console screen, even DataReceivedHandler is not called, and not able to get anything from COM4 port.
I've gone through many similar topics:
.NET SerialPort DataReceived event not firing
How to Read and Write from the Serial Port
And many mores, but no luck so far.
Am I missing something here?
P.S: It's working with RealTerm application, where we pass same command and are able to get output
Hi there I'm a complete newbie and I'm asking dumb questions, so thanks for the time...
I'm trying to send a command that is a string to a device I have - "Robot" that I control via USB -> Serial Port. This string should look like that :01013100010010 and it should tell a certain step motor to make a specific amount of steps. Every time i try to send that string like that :01013100010010 it doesn't do anything, but if I open Hyper Terminal and send it like that it does the command.
Do I need to convert it to something special or the problem is something else ?
Another thing is how can I assign the string ":01013100010010" to be sent when a button is pressed so if someone can show me a sample code of how to do that i.e. send the data on button click I would be grateful !
Here is the code I have for now it's a simple UI :
private void button2_Click(object sender, EventArgs e)
{
if (!serialPort1.IsOpen)
{
serialPort1.PortName = "COM2";
serialPort1.BaudRate = 9600;
serialPort1.Parity = System.IO.Ports.Parity.None;
serialPort1.DataBits = 8;
serialPort1.StopBits = System.IO.Ports.StopBits.One;
serialPort1.Handshake = System.IO.Ports.Handshake.None;
serialPort1.ReadTimeout = 500;
serialPort1.WriteTimeout = 500;
serialPort1.DtrEnable = true;
serialPort1.RtsEnable = true;
serialPort1.Open();
richTextBox1.Text = "Connected";
}
}
private void button1_Click(object sender, EventArgs e)
{
if(serialPort1.IsOpen){
string text = richTextBox1.Text;
serialPort1.WriteLine(text);}
}
}
}
You need to write on the SerialPort. to do that, use something like this
serialPort1.Open();
serialPort1.Write(#":01013100010010");
Thread.Sleep(200);
serialPort1.Close();
I´m using Thread.Sleep on the code because in my work sometimes our device don´t receive the full command if we just write and then close the port.
#edit: Sample code working on client
portaserial = new SerialPort("COM3", 9600, Parity.None, 8, StopBits.One);
portaserial.Open();
portaserial.Write(mensagem.ToString());
Thread.Sleep(500);
portaserial.Close();
Source: myself
Add:
serialPort1.NewLine = "\r\n";
I'm writing a serialport app to talk to a Bluetooth module over serial port. The first At command I send to the device runs fine and I get a response of the module version. All subsequent commands fail with a response of ERROR.
Part of the code is here:
namespace PhoneApp
{
public partial class Form1 : Form
{
//SerialPort myport = OPenPort.OpenIt();
SerialPort myport = new SerialPort();
public Form1()
{
InitializeComponent();
myport.PortName = "COM3";
myport.BaudRate = 115200;
myport.Parity = Parity.None;
myport.DataBits = 8;
myport.StopBits = StopBits.One;
myport.NewLine = System.Environment.NewLine;
myport.ReadTimeout = 500;
myport.WriteTimeout = 500;
myport.DtrEnable = false;
myport.RtsEnable = false;
myport.WriteBufferSize = 4096;
myport.ReadBufferSize = 4096;
myport.Handshake = Handshake.None;
myport.Encoding = System.Text.Encoding.ASCII;
if (!myport.IsOpen)
{
myport.Open();
}
calling.Visible = false;
myport.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
mycommand.Text = #"AT+BGVER";
the button which sends the command. The device requires a newline after each comand.
private void button2_Click(object sender, EventArgs e)
{
try
{
myport.WriteLine(mycommand.Text.Trim());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Not sure what I'm missing here.
Thanks for the replies. i found the problem. In fact I had to use myport.Write instead of myport.WriteLine. I deleted the line myport.NewLine and I appended "\r" to every command. Now the device responds as expected. As for DTR and RTS they are not required by the device according to the vendor
Not sure if this solves your problem, but I noticed that you don't have flow control enabled (e.g. myport.RtsEnable = false; myport.DtrEnable = false;).
Have you checked the documentation to make sure that the Bluetooth module doesn't require it? Typically devices with 115kbps and higher need flow control.
Another thing to check is the NewLine constant. You set it to the sys default which is likely Cr+Lf. Make sure that the module expects that.
I have to write a program that writes on a serial port but sometimes the call to the Write method hangs and the WriteTimeout is never fired so my program hangs indefinitely.
Here is the port creation code:
void DetectX1BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
String[] ports = SerialPort.GetPortNames();
int i = 0;
foreach (string PortName in ports)
{
try
{
Console.WriteLine("Trying to open:" + PortName);
SerialPort port = openSerial(PortName);
Console.WriteLine("Port is open:" + PortName);
port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
port.Write("$ST+IMEI=0000\r\n");
if (IMEIFoundEvent.WaitOne(250))
{
Console.WriteLine("IMEI Found:[" + imei + "]");
if (addresses.ContainsKey(imei))
{
((BackgroundWorker)sender).ReportProgress(0, new X1Model(imei, PortName, addresses[imei]));
}
else
Console.WriteLine("imei not in file: " + imei);
}
port.Close();
}
catch (Exception ex)
{
Console.WriteLine("Erreur port " + PortName + ex.Message);
}
finally
{
i++;
((BackgroundWorker)sender).ReportProgress(i * 100 / ports.Length);
}
}
}
private SerialPort openSerial(string PortName)
{
SerialPort port = new SerialPort(PortName);
port.BaudRate = 57600;
port.DataBits = 8;
port.StopBits = StopBits.One;
port.Parity = Parity.None;
port.ReceivedBytesThreshold = 1;
port.Handshake = Handshake.None;
port.DtrEnable = true;
port.RtsEnable = true;
port.WriteTimeout = 5000;
port.ReadTimeout = 5000;
if (!port.IsOpen)
port.Open();
return port;
}
Is there anything I'm missing ?
I don't know if it's relevant but I'm using Serial To USB Adapters.
Edit: I'm using Windows XP with .Net 4.0. The line doesnt't exceed 50 characters and ends by a EOL character.
I know it's an old question, which you've probably solved by now, but there's no accepted answer yet. I was having the same issue yesterday and seem to have fixed it -- were you setting the Write Timeout?
_serialPort.WriteTimeout = 500;
http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.writetimeout.aspx
You also need to set the PortName. You can get the list of ports from GetPortNames. This will typically be like COM1 or COM2