2
votes

I try to migrate an old QBasic program, for reading from a serial device (COM-port), to Visual Basic 6.

I use this code (this original code should work for VB6 also):

RESET
OPEN "COM1:2400,E,7,2,CS,DS,CD" FOR RANDOM AS #1
PRINT #1, "SND1"
LINE INPUT #1, P$

This works fine with QBasic (sending 'SND1' gives me the data from the device), but VB6 gives an error at the PRINT-command: 'Bad file mode' (error 54).

If I change FOR RANDOM to FOR OUTPUT the PRINT-commands works, but then the LINE INPUT-command gives the same error (of course).

UPDATE:

The only options for 'mode' (see: http://msdn.microsoft.com/en-us/library/aa266177(v=vs.60).aspx) are Append, Binary, Input, Output, or Random.

2
can't you open the COM1 port for read and write? - T McKeown
msdn.microsoft.com/en-us/library/aa266177(v=vs.60).aspx only gives the options: Append, Binary, Input, Output, or Random - waanders
can you do INPUT|OUTPUT, OR'ing? - T McKeown
You need to use GET and PUT with a file opened for RANDOM. - eoredson

2 Answers

1
votes

Try:

OPEN "COM1:2400,E,7,2,CS,DS,CD" FOR OUTPUT AS #1
PRINT #1, "SND1"
CLOSE #1
OPEN "COM1:2400,E,7,2,CS,DS,CD" FOR INPUT AS #1
LINE INPUT #1, P$
1
votes

This snip describes using GET/PUT to access a file opened for RANDOM in QB:

OPEN "COM1:9600,N,8,1,BIN,CS0,DS0" FOR RANDOM AS #1
DO
    IF LOC(1) THEN
        GET 1, , x
        PRINT CHR$(x);
    END IF
    x$ = INKEY$
    IF LEN(x$) THEN
        IF x$ = CHR$(27) THEN END
        x = ASC(x$)
        PUT 1, , x
    END IF
LOOP
END