ghjk guru

ghjk guru

  • NA
  • 1
  • 0

Read data from GSM modem using visual c# 2005

Jun 4 2008 6:36 AM
I'm write code to read data from GSM modem. but it couldn't read modem response. Could you please help me? I'll post my code.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;
using System.Timers;
using System.Diagnostics;

namespace rainfall
{
    public partial class Form1 : Form
    {
        //create an Serial Port object
        SerialPort sp = new SerialPort();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void btn_Start_Click(object sender, EventArgs e)
        {
            sp.BaudRate = 19200;
            sp.ReceivedBytesThreshold = 1;


            try
            {
                //Open serial port
                sp.PortName = "COM1";
                sp.Open();

            }
            catch (System.Exception ex)
            {
                lblError.Text = ex.Message;
            }
        }

        private void btn_Write_Click(object sender, EventArgs e)
        {
             try
             {
                 //Write line to serial port
                 //sp.Write(texSms.Text + "\r\n");
                 sp.Write(txt_Sms.Text + "\r\n");
                 //texSms.Text = "";
                 txt_Sms.Text = "";
             }
             catch (System.Exception ex)
             {
                 lblError.Text = ex.Message;
             }
             //Thread.Sleep(200);
             //Set time delay
             //System.Threading.Thread.Sleep(30);
             //sp.WriteTimeout = 200;    
        }

        private void btn_Read_Click(object sender, EventArgs e)
        {
             try
             {
            //Cler text box
            //Read serial port and displayed the data in text box
                 //Thread current = Thread.CurrentThread;
                 //Thread.Sleep(1000);
                 txt_Sms.Text = sp.ReadLine();
             //texSms.Text = sp.ReadExisting();
                //sp.Write("Connected" + "\nS");

             }
             catch (System.Exception ex)
             {
              lblError.Text = ex.Message;
            }
        }
    }
}