Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

QBasic program

Status
Not open for further replies.

wingbar

New Member
Hi ,
I need help on this QBasic program. Can anyone please tell me how did this programmer send and read data from the external device ??

40 Screen 0: Cls
50 Print "Serial Test Program 1 - (C) Paul Stenning, 1994"
60 Print
61 INPUT ; "Which Serial Port (1 or 2) "; p$
62 If Len(p$) <> 1 Then Beep: Print " INVALID INPUT": GoTo 61
63 If Val(p$) < 1 Or Val(p$) > 2 Then Beep: Print " INVALID INPUT": GoTo 61
64 On Error GoTo 500
70 Open "COM" + p$ + ":9600,N,8,1,CS0,CD0,DS0" For Random As #1 Len = 1
72 Print " ok": Print: Print
74 On Error GoTo 600
80 INPUT ; a$
90 If a$ = "" Then Print "QUIT": Close #1: End
100 If Len(a$) <> 2 Then Print Tab(10); "INPUT ERROR": GoTo 80
101 a1% = Asc(UCase$(Left$(a$, 1))): a2% = Asc(UCase$(Right$(a$, 1)))
102 If (a1% > 47 And a1% < 58) Or (a1% > 64 And a1% < 71) Then GoTo 105
103 Print Tab(10); "INPUT ERROR": GoTo 80
105 If (a2% > 47 And a2% < 58) Or (a2% > 64 And a2% < 71) Then GoTo 110
106 Print Tab(10); "INPUT ERROR": GoTo 80
110 Print #1, Chr$(Val("&h" + a$));
120 TIMEOUT = Timer + 0.1
130 If EOF(1) And Timer < TIMEOUT Then GoTo 130
140 If Timer >= TIMEOUT Then Print Tab(10); "**": GoTo 80
150 a$ = Hex$(Asc(Input$(1, #1)))
160 If Len(a$) < 2 Then a$ = "0" + a$
170 Print Tab(10); a$
180 GoTo 80
 
Hello wingbar,

The programmer accessed the serial port via this line:


Code:
Open "COM" + p$ + ":9600,N,8,1,CS0,CD0,DS0" For Random As #1 Len = 1
All this line does is Open the COM port for random input and output (print and input just like a text file). p$ is the port number that the users sets.
------------------------------------------------------------------------------
Data is written to the device via this line of code:
Code:
Print #1, Chr$(Val("&h" + a$));
This line prints the the com port. Note that the syntax for the Print # is the same as for standard text files.
------------------------------------------------------------------------------
The programmer used the following line to input the data:
Code:
150 a$ = Hex$(Asc(Input$(1, #1)))
All this does it receive 1 character and hex it.
------------------------------------------------------------------------------
Remember you must close the com port after you are finished with it:
Code:
Close #1
------------------------------------------------------------------------------

Qbasic's accessing of the com port is simple and very easy to work with. I remember back when I was 11. I made a simple BBS with the phone line in my house. It allowed me to control stuff in my room (a fan and a light.) Of course now in days all this sort of stuff is being put into microcontrolers with ethernet and internet accses.

Hope this helps,
Harrison
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top