1

How can I get or wait for the result code of an AT Command without using the Sleep() function in PowerBuilder? I am using the MSComm OLE Control for PowerBuilder. Could I do a While Loop unti the MSCOmm.Input property returns the proper buffer?(e.g, OK) However, I tried this but the Input returns an empty string.

Here is my PB Code:

String numbers[]

numbers[1] = "+639394854493"
numbers[2] = "+639394854491"
numbers[3] = "+639394854492"
numbers[4] = "+639394854493"
numbers[5] = "+639394854494"

int cnt = 1 
for cnt = 1 to 5
    Sleep(0.5)
    ole_1.object.Output = "AT" + Char(13) + Char(10)
    Sleep(0.5)
    ole_1.object.Output = "AT+CMGF=1" + Char(13) + Char(10)
    Sleep(0.5)
    ole_1.object.Output = "AT+CMGS=" + Char(34) + numbers[cnt] + Char(34) + Char(13) + Char(10)
    Sleep(0.5)
    ole_1.object.Output = sle_2.text + " - " + string(cnt) + Char(26)
next

And here is the port settings:

ole_1.object.CommPort = 7
ole_1.object.Settings = "115200,n,8,1"
ole_1.object.RThreshold = 1
ole_1.object.InputLen = 0
ole_1.object.InputMode = 0
ole_1.object.PortOpen = True

Any suggestions? Or am I doing something wrong?

Dac
  • 210
  • 3
  • 19

1 Answers1

0

Indeed instead of waiting for an amount of time, you could read some returned data from the Input property. The ATxx command set to communicate with modems is a far memory to me, but in most cases the modem returns an OK if the command succeded. Maybe that you have to tweak the input parameters

After some data was sent, wait until OK is returned to the serial line (idea adapted from the official MSComm example, untested):

ole_1.object.InputLen = 0 //Tell the control to read entire buffer when Input is used.
ole_1.object.Output = "AT+CMGF=1" + Char(13) + Char(10)
string ls_inp
Do
  Yield()
  ls_inp += ole_1.object.Input
Loop Until Right(ls_inp, 5) = " OK" + Char(13) + Char(10)

You would also need to adapt that code in cases where the AT command fails, I dunno how the error is returned to the serial line.

Edit: used the Right() func as suggested by OP in case where the commands are echoed in the modem answers

Seki
  • 11,135
  • 7
  • 46
  • 70
  • 1
    The loop won't stop. I tried to output `ls_inp` after it has been initialized in the loop and it returns blank. – Dac Oct 10 '15 at 01:18
  • 1
    I got it to work, but the `ls_inp` is now `AT+CMGF=1 OK` how do make the `ls_inp` to return only OK? – Dac Oct 10 '15 at 03:57
  • 1
    It seems that your modem is echoing the commands. Perhaps that sending an `ATE0` command to disable echo after the initial `AT` command can disable it. Another option is to replace the loop until test with `left(ls_inp,5) = ' OK + Char(13) + Char(10)`? – Seki Oct 10 '15 at 13:11
  • @JaymarkDacpano: glad to see that it helped you! How did you eventually fix your problem? – Seki Oct 13 '15 at 10:50
  • 1
    I used the `Right()` function instead, like so, `Right(ls_inp, 5)` so that it would just get the last line. Because the `Input` inputs the modems response per line. – Dac Oct 14 '15 at 02:13
  • @JaymarkDacpano: gosh! of course I meant `right()` instead of `left()` in my comment. (never do two things at once :oS) I updated my answer to reflect your used solution for any newcomer. Thanks for your feedback. – Seki Oct 14 '15 at 07:04