I am trying to make a small program that can send commands to a GSM modem but for some reason the commands i sent dose not seem to get through.
i have tested to see if it was my baud rate or any of the connection properties but they all mach the once i'm using in tera term and i have no problem Communication with the modem there.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.IO.Ports; using System.Threading; namespace Serilportapptest { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { terminaladapter myterminal = new terminaladapter(); //SerialPort myport = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One); //private SerialPort myport; public MainWindow() { InitializeComponent(); } //private void myport_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) //{ // txtblock1.Text = myport.ReadExisting(); //} private void Button_Click(object sender, RoutedEventArgs e) { myterminal.Connect(); myterminal.Write(txtbox1.Text); txtblock1.Text = myterminal.Read(); myterminal.Disconnect(); txtbox1.Text = ""; //myport = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One); //Thread.Sleep(1000); //myport.Open(); //Thread.Sleep(1000); //myport.WriteLine("AT+CSCS=UCS2"); //Thread.Sleep(1000); //myport.Close(); } } }
And the class
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO.Ports; using System.Threading; namespace Serilportapptest { /// <summary> /// This Class sends data to the serialport /// </summary> public class terminaladapter { public SerialPort serport = new SerialPort(); public string DataReceived = ""; public void Connect() { serport.ReadTimeout = 2000; serport.WriteTimeout = 2000; serport.BaudRate = 9600; serport.DataBits = 8; serport.PortName = "COM1"; serport.Handshake = System.IO.Ports.Handshake.None; serport.Parity = Parity.None; serport.StopBits = StopBits.One; try { if (!serport.IsOpen) { serport.Open(); Thread.Sleep(1000); } } // else already open catch { } } public void Disconnect() { try { if (serport.IsOpen) { serport.Close(); } } // else not open catch { } } /// <summary> /// This method sends a string to the Serialport /// </summary> public void Write(string scmd) { if (serport.IsOpen) { try { serport.WriteLine(scmd); Thread.Sleep(1000); } catch { } } } public string Read() { try { System.Threading.Thread.Sleep(500); this.DataReceived = serport.ReadExisting().ToString(); Thread.Sleep(1000); //this.DataReceived += serport.ReadLine().ToString(); return (this.DataReceived); } catch(Exception e) { return e.Message; } } } }
no matter what AT command i try to send nothing happens. even if i go to tere term and look up the properties on the modem that should have changed. so i assume the command never gets there but why?