0

I'm building a windows service to read SMS from 3G dongle storage set as SIM card using AT commands. It's working good but some times it fails and response shows +CPMS: 0,0,0,0,0,0 .

Code

        gsmPort.WriteLine("AT+CMGF=1"); //Set mode to Text(1) or PDU(0)
        gsmPort.WriteLine("AT+CPMS=\"SM\""); //Set storage to SIM(SM), MODEM(ME), BOTH(MT)
        gsmPort.WriteLine("AT+CMGL=\"ALL\""); //What catagory to read ALL, REC READ, or REC UNREAD

        response = gsmPort.ReadExisting();

When i move dongle into another port it starts working. I've tride it on multiple systems with different dongle but the result is always same. I've also changed my SIM card but no effect. Can any one have any idea why after some time SIM storage shows 0/0.

Community
  • 1
  • 1
Ashish Rathore
  • 2,546
  • 9
  • 55
  • 91
  • 2
    Just for my knowledge why we need Thread.Sleep()? – Anil Feb 28 '17 at 11:01
  • 2
    Using `Thread.Sleep` is not the best way to write state machines in C# as @Anil wrote. Also, don't these other commands also return status (`"OK"` or something)? – vgru Feb 28 '17 at 11:25
  • I'm using `Thread.Sleep` here to provide some time in registering commands. Is this practice causes any issue? Also above commands return response containing received message details and storage details. – Ashish Rathore Feb 28 '17 at 12:03
  • What is the brand and model of the dongle? – i486 Feb 28 '17 at 13:25
  • Is the `gmPort.WriteLine` an async command, that is why you are using `Thread.Sleep`? If so, I believe that you should try to use the `await` keyword or try to implement a callback to know when the data has finished writing. – Felipe Feb 28 '17 at 14:14
  • 1
    `Is this practice causes any issue?` This is the **single worst** beginner mistake you can do with regards to AT command handling. You should *never, never, never, ever* use Thread.Sleep or anything similar to [wait for response from a modem](http://stackoverflow.com/a/31688517/23118). It's as useful as kicking dogs that stand in your way in order to get them to move. Yes you might be lucky and have it actually work sometimes, but at some point you will be sorry for taking that approach... – hlovdal Mar 09 '17 at 16:43
  • Gotten around to solve this? – SunnySonic Oct 24 '18 at 14:51

0 Answers0