'===========================================================================
' Subject: SIMPLE NULMODEM CHAT PROGRAM       Date: 02-21-98 (20:31)       
'  Author: Bert van Dam                       Code: QB, QBasic, PDS        
'  Origin: bd1rsd@usa.net                   Packet: MODEM.ABC
'===========================================================================
'CHAT.BAS                                                  feb 1998

'Simple chat program for two PC's connected with nulmodem cable.
'This is meant as an example for the technique, not as a fool proof
'program. This source is donated to the public domain by Bert van Dam.
'I can be reached at BD1RSD@USA.NET or in the Fido QUIK_BAS area.

'Connect two PC's with a nulmodem cable on com port 2. Start this
'program on both PC's at the same time (more or less). Now you can
'type something to the other PC, and receive at the same time. This
'program has been tested on a 486DX4 and a 386SX with QB4.5

'Have fun!

'Go to a subroutine as soon as something is detected on the com2 port
ON COM(2) GOSUB Buffer
COM(2) ON

'Pepare the screen layout
CLS
PRINT "             ---===[ simple nulmodem chat program ]===---"
PRINT "                          (pres q to quit)"
COLOR 15, 0
Xpos = 6
Ypos = 10
Xp = 9
Yp = 10

'Open com port 2 for communications
OPEN "com2:9600,N,8,1" FOR RANDOM AS #1
DO
        'Loop which displays the time, normally this would be the
        'place for your own program
        LOCATE Xpos, Ypos
        COLOR 15, 0
        PRINT TIME$

        'Check wether the user wants to write something
        AnyKey$ = INKEY$
        IF AnyKey$ <> "" THEN
                'New entry started, erase the previous line
                LOCATE Xp, Yp
                PRINT SPC(70);
                DO
                        SELECT CASE AnyKey$
                        CASE ""
                                'Nothing received yet
                                AnyKey$ = INKEY$
                        CASE CHR$(8)
                                'Backspace key detected, make the string
                                'one position shorter, erase the line and
                                'print again
                                Entry$ = LEFT$(Entry$, LEN(Entry$) - 1)
                                LOCATE Xp, Yp
                                PRINT SPC(70);
                                LOCATE Xp, Yp
                                PRINT Entry$
                                AnyKey$ = ""
                        CASE CHR$(13)
                                'Return detected, sent the string to
                                'the other PC
                                PRINT #1, Entry$
                                Result$ = Entry$
                                Entry$ = ""
                                EXIT DO
                        CASE ELSE
                                'Simply add the character to the string and
print again
                                LOCATE Xp, Yp
                                Entry$ = Entry$ + AnyKey$
                                COLOR 4, 0
                                PRINT Entry$
                                AnyKey$ = ""
                        END SELECT
                LOOP
        END IF
LOOP WHILE Result$ <> "q"
CLOSE #1
END

Buffer:
'Get the data at the comport and add to a temporary variable
Vino$ = Vino$ + INPUT$(LOC(1), #1)
IF RIGHT$(Vino$, 1) = CHR$(13) THEN
        'If a carriage return has been received print the variable
        'to the screen, clear it and put the cursor back at the place
        'the main loop expects it to be
        X = CSRLIN
        Y = POS(0)
        LOCATE 12, 10
        PRINT SPC(70);
        LOCATE 12, 10
        COLOR 2, 0
        PRINT LEFT$(Vino$, LEN(Vino$) - 1)
        Vino$ = ""
        LOCATE X, Y
END IF
RETURN
