Explanation of QBASIC code

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
JJ
Newbie
Posts: 2
Joined: Mon Nov 26, 2007 7:15 am

Explanation of QBASIC code

Post by JJ »

Hi, I need to find out what these lines of code are doing? The purpose of the code is to get information from a machine via RS232. The string of information this prog is getting, is being manipulated but I can't figure out what hell its doing to the string? I'm not a QBasic programmer so Any help explaining these 6/7 lines would be appreciated. Thanks in advance.

'Read from inst
'---------------------------------------
OPEN "COM1:9600,S,7,1,CD0,CS0,DS0,OP0,RS" FOR RANDOM AS #1
c1! = 0
c1$ = ""
s1$ = ""

a$ = CHR$(27) + "P": PRINT #1, a$
s1$ = "": c1$ = ""
a$ = INPUT$(15, #1)
CLOSE

s1$ = LEFT$(a$, 1)
c1$ = MID$(a$, 3, 8 )
c1! = VAL(c1$) 'inst value
IF in = 2 THEN c1! = c1! * 10
IF s1$ = "-" THEN c1! = -c1! 'Convert to -ve val if -sign

RETURN
Mac
Veteran
Posts: 151
Joined: Mon Aug 06, 2007 2:00 pm

Re: Explanation of QBASIC code

Post by Mac »

JJ wrote:from a machine
The result of the lines from OPEN to CLOSE is
1) to send ESC followed by "P" to the device
2) to load into variable a$ a 15-character response send back.

Presumably you have a manual that explains what ESC-P means for the device you are working with and possible data it returns. This is not QBasic, it is user-manual data of that device.

But anyway, the program appears to expect
S?12345678?????
where S is either a "-" sign or something else
and 12345678 is an 8-digit number
and ? represents characters returned by the device that are ignored.

The variable C1! (! is just a flag for type of variable - ignore it)
is set to the value of 12345678 and if S is "-", the value is negative.

For example, if this 15 character string
- 23.12435 xyzz
returned from the device, it would be interpreted as
S! = -23.12435

That said, I have no idea about this instruction:
IF in = 2 THEN c1! = c1! * 10

Unless there are other instructions you didn't post that explain what the value of "in" is, I have to assume it is zero and thus this instruction has no meaning.

If there is some place that sets in=2, then the instruction would change the final value to S! = -231.2345

Mac
JJ
Newbie
Posts: 2
Joined: Mon Nov 26, 2007 7:15 am

Great

Post by JJ »

Thats incredibly helpful, thank you, very much appreciated.
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 »

You can find out if there is data in the receive buffer by using LOC(1).

Code: Select all

IF LOC(1) THEN a$ = INPUT$(15, 1)  'tries to get 15 bytes of data
However, 15 bytes of data is really a lot to grab at once just because LOC says there is some data. I would use the code in a loop and use:

Code: Select all

DO
    IF LOC(1) THEN 
       a$ = INPUT(1, 1)  'gets 1 byte at a time
       total$ = total$ + a$               'construct data string
    END IF
LOOP UNTIL LEN(total$) = 15
I also don't care for routines that use two same named variables for different types of data. There are a lot of good Serial port programs on the Web to try!

Ted
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
Post Reply