2

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.

4

2 回答 2

1

尝试:

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$
于 2014-03-05T16:44:24.997 回答
1

此片段描述了使用 GET/PUT 访问在 QB 中为 RANDOM 打开的文件:

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
于 2016-12-11T04:40:19.437 回答