回 帖 发 新 帖 刷新版面

主题:求救!关于RS485在QB下的通讯问题

用仪表生产厂家提供的测试通讯程序能读出仪表的PV值,但生产厂家未能提供QB下的测试程序,通讯协议如下
1.        RS485通讯协议
1.1.        主从式半双工通讯,主机呼叫从机地址,从机应答方式通讯。串行通讯,数据帧11位,1个起始,8个数据位,2个停止位

1.2.         数据传输格式采用标准ASCⅡ码
1.2.1.    通讯数据字符集
0(30H)    1(31H)    2(32H)     3(33H)    4(34H)5(35H)     6(36H)    7(37H)
    8(38H)    9(39H) A(41H)    B(42H)C(43H)    D(44H)    E(45H)
F(46H)     .(2EH)     -(2DH)    +(2BH)

1.2.2.    通讯控制字符集
DC1(11H):读瞬时值            DC2(12H):读参数
DC3(13H):写参数                    DC4(14H):读写FCC5000
STX(02H):从机起始符            ETX(03H):主机结束符
ETB(17H):从机结束符                RS (1EH):数据间隔符
uS (1FH):参数间隔符                ACK(06H):接收正确
NAK(15H):接收错误                CAN(18H):通讯复位
SP (20H):空白符

1.3.        通讯协议
1.3.1.    读瞬时值
1.3.1.1.  读单通道瞬时值
            主机发送:    DC1 AAA CC ETX
                        DC1(11H)    :    读瞬时值
                        AAA           :    从机地址码(=001~254)
                        CC               :    通道号(=01-99)
                        ETX(03H)    :    主机结束符
        从机回送:    STX AAA CC US MM US DDDDDDD US EEEE US SSSSS ETB
                        STX(02H)    :     从机起始符
                        AAA           :      从机地址码(=001~254)
                        CC            :      通道号(=01-99)
                        US(1FH)     :      参数间隔符
                        MM               :      表型字(=00~99)
DDDDDDD        :    瞬时值(-32167~32767,32767=brok,16000=H.oFL,-2000=L.oFL,
小数点在实际位置)
                        EEEE          :     报警1~4报警状态(E=0:OFF  E=1:ON)
                        SSSSS         :      校验和5位十进制=00000~65535,从STX到最后一个US间每个
字符ASC值的和,再除以65536的余数)
                        ETB(17H)    :      从机结束符
        例子:
        主机发送    :    11H 30H 30H 31H 30H 31H 03H(读001号表01通道瞬时值)
        从机回送    :      02H 30H 30H 31H 30H 31H 1FH 30H 36H 1FH 2DH 30H 31H 32H 33H 2EH 34H 1FH 31H 30H 30H 30H 1FH 30H 31H 30H 30H 34H
17H(001号表为XMA5000系列,01号通道瞬时值=-0123.4,报警1
动作,报警2不动作,校验和=1004)


我写的测试程序无法运行,提示 I/O 设备错误,请大家帮我看看,有什么问题?
OPEN “COM2:9600,N,8,2” FOR RANMDOM AS#1
PRINT #1,CHR$(17)+CHR$(48)+CHR$(48)+CHR$(49)+CHR$(48)+CHR$(49)+CHR$(3)
FOR I= 1 TO 10000:I=I+1:NEXT (延时)
LINE [color=FF0000]input[/color] #1,A$
PRINT A$
CLOSE #1
END

回复列表 (共16个回复)

沙发

[em2][em2]
对设备执行OPEN后,设备控制数据的读写由IOCTL 语句和IOCTL$ 函数来处理。
1.IOCTL 语句
  格式:IOCTL [#]文件号,字符串
  功能:把一个控制字符串传送到设备驱动程序。
2.IOCTL$ 函数
  格式:IOCTL$ ([#]文件号)
  功能:从设备驱动程序中返回一个控制字符串。

试试下面的程序,看行不行
OPEN "COM2: 9600, N, 8, 2" FOR RANDOM AS #1
IOCTL #1,CHR$(17)+ CHR$(48) + CHR$(48) + CHR$(49) + CHR$(48) + CHR$(49) + CHR$(3)
FOR i = 1 TO 10000: NEXT i
a$ = IOCTL$ (#1)
PRINT a$
CLOSE #1
END

板凳

改成这样试一下:
OPEN "COM2:9600,N,8,2" FOR RANDOM AS #1
PRINT #1, CHR$(17) + CHR$(48) + CHR$(48) + CHR$(49) + CHR$(48) + CHR$(49) + CHR$(3)
FOR I = 1 TO 10000: I = I + 1: NEXT
LINE INPUT #1, A$
PRINT A$
CLOSE #1
END

3 楼

楼上的,是LINE INPUT 吗

4 楼

如果你确定是接在COM2口,可以下载一个软件<串口监视器>,然后发送指令,看看这个监视软件里有没有返回信息,
被通讯的表也要把通讯速度设置成9600,而且表号要设置成程序中需要的读取的表号!
另外,希望你把有关的VB源程序发来看看!

网友讨论了你的这个问题
请看
汶峪(12290320) 16:58:57
QB45老兄,LINE PRINT 是不是写指令?
汶峪(12290320) 16:59:41
把 LINE PRINT 换成 LINE INPUT 才对吧?

所以请你把LINE PRINT 改一下

5 楼

谢谢大家!,LINE PRINT 是我笔误 用IOCTL改以后,提示非法功能调用,表的地址等其他都没问题,用厂家提供的测试程序,可以实现通讯,厂家的源代码及协议我已上传到http://upload.programfan.com/upfile/200506280714885.rar,请大家帮我看看

6 楼

你上传的VB源程序中,有几条握手信号的语句,而且在多个地方出现,是不是这个地方出的问题?

MSComm1.SThreshold = 5
MSComm1.RThreshold = 1


Select Case Combo3.Text
  Case "RTS=0"
   MSComm1.RTSEnable = False
  Case "RTS=1"
   MSComm1.RTSEnable = True
  Case "DTR=0"
   MSComm1.DTREnable = False
  Case "DTR=1"
   MSComm1.DTREnable = True
  Case "MODEM"
   MSComm1.DTREnable = True
End Select

我没全列出来,如果仪表在通讯过程中需要一些握手确认的过程,此部分的处理是不能缺的

7 楼

谢谢汶峪兄,能详细解释一下吗?

另外,我作了如下修改,还是提示I/O错误

OPEN “COM2:9600,N,8,2” FOR RANMDOM AS#1
PRINT #1,CHR$(17)+CHR$(48)+CHR$(48)+CHR$(49)+CHR$(48)+CHR$(49)+CHR$(3)+[color=FF0000]CHR$(13)+CHR$(10)[/color]
FOR I= 1 TO 10000:I=I+1:NEXT (延时)
LINE input #1,A$
PRINT A$
CLOSE #1
END

8 楼

我对通讯设备不懂的,

只是好像在QB的程序里,
print 语句并不一定会在你执行的时候马上写到设备去的,
语句执行后,会把数据写到缓冲区去,等到一个"合适的时机"才
会把数据写到设备去的,
我建议你把那延时的地方改成把文件关闭再打开试试.

楼主有没有试过飞鸟的方法?

再如果,假如说提示是I/O设备错误,
我估计是文件错误,
要看一下出错是在哪一句.
文件方式 random 应该不太好,应该用二进制方式 binary

还有能不能确定文件名的参数有没有问题?
端口,速率等

读数据的时候延时会不会有关系?
有时候设备不一定会帮你存在缓冲区的.

9 楼

这是秋水发的帮助文档

OPEN (Communications) Statement Details
    打开通讯设备的语法.
Syntax
  OPEN "COMn: optlist1 optlist2" [FOR mode] AS [#]filenum [LEN=reclen]
    这里的len=指的是随机文件方式random


COMn: is the name of the device to be opened. The n argument is the
number of a legal communications device, such as COM1: or COM2:. The
first list of options, optlist1, has the following form:
    端口com

  [speed][,[parity] [,[data][,[stop]]]]
  速率,  ,数据位,停止位

The following list describes the possible options:

  Option   Description
  speed    The "baud" rate (baud means "bits per second") of the
           device to be opened. Valid speeds are 75, 110, 150, 300,
           600, 1200, 1800, 2400 and 9600.
  parity   The parity of the device to be opened. Valid entries for
           parity are: N (none), E (even), O (odd), S (space), or
           M (mark).
  data     The number of data bits per byte. Valid entries are 5,
           6, 7, or 8.
  stop     The number of stop bits. Valid entries are 1, 1.5, or 2.

Options from this list must be entered in the order shown; moreover,
if any options from optlist2 are chosen, comma placeholders must
still be used even if none of the options from optlist1 are
chosen. For example:

  OPEN "COM1: ,,,,CD1500" FOR INPUT AS #1

If you set the data bits per byte to eight, you must specify no parity
(N). Because QuickBASIC uses complete bytes (eight bits) for numbers,
you must specify eight data bits when transmitting or receiving
numeric data.

The choices for optlist2 are described in the following list. The
argument m is given in milliseconds; the default value for m is 1000.

  Option   Description
  ASC      Opens the device in ASCII mode. In ASCII mode, tabs are
           expanded to spaces, carriage returns are forced at the
           end-of-line, and CTRL+Z is treated as end-of-file. When
           the channel is closed, CTRL+Z is sent over the RS-232 line.
  BIN      Opens the device in binary mode. This option supersedes
           the LF option. BIN is selected by default unless ASC is
           specified.
           In the BIN mode, tabs are not expanded to spaces, a
           carriage return is not forced at the end-of-line, and
           CTRL+Z is not treated as end-of-file. When the channel is
           closed, CTRL+Z will not be sent over the RS-232 line.
  CD[m]    Controls the timeout on the Data Carrier Detect line (DCD).
           If DCD is low for more than m milliseconds, a device
           timeout occurs.
  CS[m]    Controls the timeout on the Clear To Send line (CTS). If
           CTS is low (there is no signal) for more than m
           milliseconds, a device timeout occurs.
  DS[m]    Controls the timeout on the Data Set Ready line (DSR). If
           DSR is low for more than m milliseconds, a device timeout
           occurs.
  LF       Allows communication files to be printed on a serial line
           printer. When LF is specified, a line-feed character (0AH)
           is automatically sent after each carriage-return character
           (0DH). This includes the carriage return sent as a result
           of the width setting. Note that INPUT and LINE INPUT, when
           used to read from a COM file that was opened with the LF
           option, stop when they see a carriage return, ignoring the
           line feed.
  OP[m]    Controls how long the statement waits for the open to be
           successful. The parameter m is a value in the range 0 to
           65,535 representing the number of milliseconds to wait for
           the communications lines to become active. If OP is
           specified without a value, the statement waits for ten
           seconds. If OP is omitted, OPEN COM waits for ten times
           the maximum value of the CD or DS timeout values.
  RB[n]    Sets the size of the receive buffer to n bytes. If n is
           omitted, or the option is omitted, the current value is
           used. The current value can be set by the /C option on the
           QuickBASIC or BC command line. The default is 512 bytes.
           The maximum size is 32,767 bytes.
  RS       Suppresses detection of Request To Send (RTS).
  TB[n]    Sets the size of the transmit buffer to n bytes. If n is
           omitted, or the option is omitted, the current value is
           used. The default size is 512 bytes.

The options from the list above can be entered in any order, but they
must be separated from one another by commas. For CS[m], DS[m], and
CD[m], if there is no signal within m milliseconds, a timeout occurs.
The value for m may range from 0 to 65,535, with 1000 as the default
value. The CD default is 0.) If m is equal to 0 for any of these
options the option is ignored. The CTS line is checked whenever there
is data in the transmit buffer if the CS option is specified. The
DSR and DCD lines are continuously checked for timeouts if the
corresponding options (DS, CD) are specified.

The mode argument is one of the following string expressions:

  Mode     Description
  OUTPUT   Specifies sequential output mode
  INPUT    Specifies sequential input mode
  RANDOM   Specifies random-access mode

If the mode expression is omitted, it is assumed to be random-access
input/output. The filenum is the number used to open the file. The
OPEN COM statement must be executed before a device can be used for
communication using an RS-232 interface.

If the device is opened in RANDOM mode, the LEN option specifies the
length of an associated random-access buffer. The default value for
length is 128. You can use any of the random-access I/O statements,
such as GET and PUT, to treat the device as if it were a random-
access file.

The OPEN COM statement performs the following steps in opening a
communications device:

  1. The communications buffers are allocated and interrupts are
     enabled.
  2. The Data Terminal Ready line (DTR) is set high.
  3. If either of the OP or DS options is nonzero, the statement waits
     up to the indicated time for the Data Set Ready line (DSR) to be
     high. If a timeout occurs, the process goes to step 6.
  4. The Request To Send line (RTS) is set high if the RS option is
     not specified.
  5. If either of the OP or CD options is nonzero, OPEN COM waits up
     to the indicated time for the Data Carrier Detect line (DCD) to
     be high. If a timeout occurs, the process goes to step 6.
     Otherwise, OPEN COM has succeeded.
  6. The open has failed due to a timeout. The process deallocates
     the buffers, disables interrupts, and clears all of the control
     lines.

  Note: Use a relatively large value for the OP option compared to the
        CS, DS, or CD options. If two programs are attempting to establish
        a communications link, they both need to attempt an OPEN during at
        least half of the time they are executing.

Any syntax errors in the OPEN COM statement produce an error message that
reads "Bad file name."

[color=0000FF]看了一下,才发觉random文件要用get,put语句[/color]

10 楼

狂汗,怎么评分给我了?
45该骂我剽窃了.

今天翻看了一下好多年前的教材.
不知道你用不用得着.

OPEN “COM2:9600,N,8,2” FOR RANMDOM AS#1

b$=CHR$(17)+CHR$(48)+CHR$(48)+CHR$(49)+CHR$(48)+CHR$(49)+CHR$(3)

do
  PRINT #1,b$
loop while eof(1)

do while loc(1)
  if loc(1)>128 then
    print #1,chr$(19);
    a$=input$(loc(1),#1)
    print len(a$);a$
  else
    print #1,chr$(17);
  end if
loop
PRINT A$
CLOSE #1
END

我来回复

您尚未登录,请登录后再回复。点此登录或注册