Querying a modem

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
n.allan
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 153
Joined: Mon Mar 12, 2007 12:06 pm

Querying a modem

Post by n.allan » Fri Nov 30, 2007 1:20 pm

I am trying to query a com port to see if a modem is present but I just cant get my head round the read from file command. it always returns empty and the result is always "eof"

i am using somthing along thlines of:

open file "COM1:" for update
send "AT" to file "COM1:"
read from file "COM1:" until crlf --I have tried until empty, "OK" , for 1 line , in 5seconds etc.. to no avail
if it contains "OK" then answer information "Modem found!" else answer error "Modem not found!"
close file "COM1:"

I always end up with "Modem not found!"

any help would be appreciated

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Fri Nov 30, 2007 3:41 pm

hi n.allan,

What does "it" contain?

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

n.allan
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 153
Joined: Mon Mar 12, 2007 12:06 pm

Post by n.allan » Fri Nov 30, 2007 3:44 pm

"it" always returns empty. and the result is always "eof"

I am expecting the usual:

OK<cr><lf>

from the modem (if it's connected)

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Fri Nov 30, 2007 3:52 pm

n.allan,

Has the modem been installed properly? Does it work with other software? Can you actually make a connection with it to an internet provider or ehm... what do people use modems for these days? Are you sure that the modem is supposed to understand AT commands?

Mark
Last edited by Mark on Fri Nov 30, 2007 4:49 pm, edited 1 time in total.
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

n.allan
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 153
Joined: Mon Mar 12, 2007 12:06 pm

Post by n.allan » Fri Nov 30, 2007 4:03 pm

To explain a little further It is a siemens gsm modem connected to com1. I can communicate with it no probs at all using hyperterminal. It fully supports AT commands.

so typing AT then hitting return using hyperterminal returns "OK" like thus:

AT
OK

I think it has something to do with timing. I am obviously reading com1: at the wrong time. Reading the documentation it states that read from file will continue to read until eof, but even if I don't have the modem connected to com 1, it still returns empty and the result is still "eof"

I can't understand why rev takes it upon itself to timeout even when no data is present on the com port.

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Fri Nov 30, 2007 4:28 pm

Hi n.allan,

Does this work?

Code: Select all

on checkModem
  open file "COM1:" for update
  write "AT" to file "COM1:"
  read from file "COM1:" until cr -- or lf but not crlf
  close file "COM1:" 
  answer it
end checkModem
Best,

Mark
Last edited by Mark on Fri Nov 30, 2007 4:49 pm, edited 1 time in total.
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

n.allan
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 153
Joined: Mon Mar 12, 2007 12:06 pm

Post by n.allan » Fri Nov 30, 2007 4:41 pm

just tried your script mark

"it" returned empty :(

I think I will try a seperate read loop to constantly read the incoming data from the com port then send the "AT" command seperately.

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Fri Nov 30, 2007 4:53 pm

n.allan,

Have you already tried adding a line to wait for the modem to process the data?

write...
wait 250 millisecs with messages
read....

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

n.allan
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 153
Joined: Mon Mar 12, 2007 12:06 pm

Post by n.allan » Fri Nov 30, 2007 5:37 pm

Yes, I have tried that with different times in milliseconds and ticks, still nothing.

I am having some success using a seperate handler doing the reading and putting it after a field "Output". But I am finding that the seperate handler is putting the SENT Commands after the field as well as the RECIEVED commands? so when I send "AT" and recieve "OK" the field "Output" contains:

-------------------------------
AT

OK
AT

OK
-------------------------------

The recieving handler is as follows:
on CheckModem
open file "COM1:" for update
repeat
read from file "COM1:" until lf
put it after field "Output"
wait 1 tick with messages
next repeat

if tCancel is true then exit repeat --escape from the repeat loop

end repeat
close file "COM1:"
end CheckModem


(In the above example I sent "AT" to com1: twice)

n.allan
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 153
Joined: Mon Mar 12, 2007 12:06 pm

Post by n.allan » Tue Dec 11, 2007 5:42 pm

I am positive there is a bug in the read from file/driver "COMx:" command.

No matter what type of line ending I read "to" rev takes it upon itself to stop reading the string and return it. This is particularly unhandy if you you are tryig to interface GPS etc..

I have layed the modem handler above aside to work a "simpler" bit.
I am reading a simple 13 char string from "com1:" it begins with "#" and is terminated with <cr><lf> (ascii 13,10.)

so we have #S1O000000300150<cr><lf> tx every second

I have tried read from file/driver "comx:" until lf --cr --return etc...
I have even tried

put CharToNum(13) & CharToNum(10) into tLineTerminator
read from file "comx:" until tLineTerminator

sometimes it will return the whole string, then it will return partial strings.

I realy need it to read the whole string in order for me to split into chunks and make decisions based on those chunks. If I only get a partial read of the string, The chunks will be useless.

Post Reply