BestTechie Forums: Qbasic Rs232 Code Explanation - BestTechie Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Qbasic Rs232 Code Explanation Need to find out what this code os doing??


#1 User is offline   jjos 

  • Member
  • Pip
  • Group: Members
  • Posts: 1
  • Joined: 26-November 07

Posted 26 November 2007 - 07:36 AM

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

#2 User is offline   iccaros 

  • UberTechie
  • Group: Linux Experts
  • Posts: 1292
  • Joined: 31-August 04
  • Location:Great State of Washingtion
  • Operating System:Gentoo,Iccaros-Linux(of course),Slackware,GentooX,Red Hat, Windows (3.1 to VISTA BETA), MAC OSX (10.4 currently),LFS, Solaris 8,9,10, Trusted Solaris, FreeBSD, OPENBSD, NETBSD

Posted 30 November 2007 - 04:39 PM

Line one of the "Function" is opening the rs232 FIFO and assigning its buffer the name #1 reading in random mode and not sequencal
next they are creating an integer and two strings.


next comes the work

a$ = CHR$(27) + "P": PRINT #1, a$
this is really two lines in one see the :

so we assign ascii char 27 (see http://game-editor.c...ages/ascii.jpg) followed by the P
so we have a$ = "^[P"
and the second half puts that string to the serial port (in basic you print to a port like you do the screen, and read from it like a keyboard)

s1$ = "": c1$ = ""

this is also two lines that assign blank strings to s1$ and c1$


it looks like (with out the rest of the code this is just a guess)
a$ = INPUT$(15, #1)
is assigning the first 15 chars from the serial port to a$
or INPUT$ is an Array and we are looking up a value that is at position 15 x the value on the serial port

then close does what it says , it closes the serial port

the rest is simple

s1$ = LEFT$(a$, 1)

assign the value of the array LEFT$ at position of value of a$ x 1

c1$ = MID$(a$, 3, 8 )
again assign c1$ the value from an array based on the value of a$

c1! = VAL(c1$) 'inst value
change string c1$ to an integer

IF in = 2 THEN c1! = c1! * 10
I assume this is correct for qbasic but in other languages its IF in == 2 Then, so we evaluate the value of 'in' and if it equals 2 then we multiply 10 to c1! and assign that value back to its self

IF s1$ = "-" THEN c1! = -c1! 'Convert to -ve val if -sign
check if s1$ = - and if so make c1! a negative number



hope that helps..

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users