Send sms to asp.net using gsm modem

I developed an asp.net application to send sms from a gsm modem to the target base using a URL from a browser. I used the library developed by codeproject http://www.codeproject.com/articles/20420/how-to-send-and-receive-sms-using-gsm-modem

but I get the problem when I request the form two browsers at the same time, and I want my code to detect that the modem is being used by another process at that time, here is my code:

DeviceConnection deviceConnection = new DeviceConnection(); protected void Page_Load(object sender, EventArgs e) { try { if (Request.QueryString["destination"] != null && Request.QueryString["text"] != null) { deviceConnection.setBaudRate(9600); deviceConnection.setPort(12); deviceConnection.setTimeout(200); SendSms sendSms = new SendSms(deviceConnection); if (deviceConnection.getConnectionStatus()) { sendSms.strReciverNo = Request.QueryString["destination"]; sendSms.strTextMessage = Request.QueryString["text"]; if (sendSms.sendSms()) { Response.Write("Mesage successfuly sent to " + Request.QueryString["destination"]); } else { Response.Write("Message was not sent"); } } } } catch (Exception ex) { Console.WriteLine("Index "+ex.StackTrace); } } 

This is the SendSms class:

  class SendSms { DeviceConnection deviceConnection; public SendSms(DeviceConnection deviceConnection) { this.deviceConnection = deviceConnection; } private string reciverNo; private string textMessage; private delegate void SetTextCallback(string text); public string strReciverNo { set { this.reciverNo = value; } get { return this.reciverNo; } } public string strTextMessage { set { this.textMessage = value; } get { return this.textMessage; } } public bool sendSms() { try { CommSetting.Comm_Port = deviceConnection.getPort();//GsmCommMain.DefaultPortNumber; CommSetting.Comm_BaudRate = deviceConnection.getBaudRate(); CommSetting.Comm_TimeOut = deviceConnection.getTimeout();//GsmCommMain.DefaultTimeout; CommSetting.comm = new GsmCommMain(deviceConnection.getPort() , deviceConnection.getBaudRate(), deviceConnection.getTimeout()); // CommSetting.comm.PhoneConnected += new EventHandler(comm_PhoneConnected); if (!CommSetting.comm.IsOpen()) { CommSetting.comm.Open(); } SmsSubmitPdu smsSubmitPdu = new SmsSubmitPdu(strTextMessage, strReciverNo, ""); smsSubmitPdu.RequestStatusReport = true; CommSetting.comm.SendMessage(smsSubmitPdu); CommSetting.comm.Close(); return true; } catch (Exception exception) { Console.WriteLine("sendSms " + exception.StackTrace); CommSetting.comm.Close(); return false; } } public void recive(object sender, EventArgs e) { Console.WriteLine("Message received successfuly"); } } } 
+4
source share
1 answer

I'm not sure how ASP works, but if every process uses the same memory space, you can have a static lock for single access. In any case, it’s worth a try.

 private static readonly object CommLock = new object(); public bool sendSms() { lock(CommLock) //second request should wait here until first request finishes { //all the same comm code from sendSms as before } } 

I agree with the idea of ​​a global Dai mutex as an improvement, see What is a good template for using Global Mutex in C #?

0
source

Source: https://habr.com/ru/post/1435925/


All Articles