简体   繁体   English

错误文件模式错误OPEN语句使用VB6读/写到串行(COM)端口

[英]'Bad file mode' error OPEN statement read/write to serial (COM) port using VB6

I try to migrate an old QBasic program, for reading from a serial device (COM-port), to Visual Basic 6. 我尝试将旧的QBasic程序(用于从串行设备(COM端口)读取)迁移到Visual Basic 6。

I use this code (this original code should work for VB6 also): 我使用以下代码(原始代码也应适用于VB6):

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). 这在QBasic上可以正常工作(发送“ SND1”会给我来自设备的数据),但是VB6在PRINT命令上显示了错误:“错误的文件模式”(错误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). 如果我改变FOR RANDOMFOR OUTPUTPRINT -命令的作品,但随后的LINE INPUT -command给出了同样的错误(当然)。

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. “模式”的唯一选项(请参阅: http : //msdn.microsoft.com/zh-cn/library/aa266177 (v= vs.60 ) .aspx )是追加,二进制,输入,输出或随机。

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$

This snip describes using GET/PUT to access a file opened for RANDOM in QB: 此片段描述了如何使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM