xudajun 发表于 2019-8-22 16:13:54

怎么使用Codesys系统自带的串行口库?(自由协议)

如果你需要使用C35/C36/C37的RS485口做自由口协议收发用,可以使用Codesys自带串口库,如下是使用的教程。

首先要使用自带串口库,有一些参数需要我们在程序中设定,如下是codesys中的英文帮助说明。(没有中文的,抱歉哈)


In order to configure the COM port a pointer on an array of ⇘ COM.PARAMETER is transmitted. The array contains pairs of parameter IDs and associated values corresponding to the configuration parameters. These constants depend on the platform and the implementation. The following constants are Windows specific in terms of their employment within the implementation of SysCom. The table indicates which member of the Windows structure DCB the parameter is assigned to.
Id
Nameof CAA.Parameter_Constants
corresponds to DCB value

0x00000001
udiPort
Port number(C35、C36、C37串口 应固定为2)

0x00000002
udiStopBits
StopBits (停止位)
COM.ONESTOPBIT      0   1 stop bit
COM.ONE5STOPBITS   1   1.5 stop bits
COM.TWOSTOPBITS      2   2 stop bits

0x00000003
udiParity
Parity(奇偶校验位)
COM.EVEN   0   even
COM.ODD      1   odd
COM.NONE   2   none

0x00000004
udiBaudrate
Baudrate波特率   960019200 115200等等

0x00000005
udiTimeout
Timeout

0x00000006
udiBufferSize
Buffersize of the serial interface

0x00000007
udiByteSize
Number of data bits/BYTE, 4-8

0x00000008
udiBinary
binary mode, no EOF check

0x00000009
udiOutxCtsFlow
CTS handshaking on output

0x0000000A
udiOutxDsrFlow
DSR handshaking on output

0x0000000B
udiDtrControl
DTR Flow control

0x0000000C
udiDsrSensitivity
DSR Sensitivity

0x0000000D
udiRtsControl
Rts Flow control

0x0000000E
udiTXContinueOnXoff
XOFF continues Tx

0x0000000F
udiOutX
XON/XOFF out flow control

0x00000010
udiInX
XON/XOFF in flow control

0x00000011
udiXonChar
Tx AND Rx XON character

0x00000012
udiXoffChar
Tx AND Rx XOFF character

0x00000013
udiXonLim
Transmit XON threshold                              

0x00000014
udiXoffLim
Transmit XOFF threshold



为了配置COM端口,需要用户去配置com.parameter指针数组中的参数。该数组包含参数ID和对应于配置参数的关联值。
这些常量依赖于平台实现。以下常量是Windows在syscom实现过程中的特定用途。该表指示将参数分配给Windows结构DCB成员。

首先在程序中,需要先对串口进行参数初始化。

在全局变量中变量
VAR_GLOBAL
      aCom1Params: ARRAY OF COM.PARAMETER;
      udiListLength: USINT;
      Frist: BOOL := 1;
END_VAR


进行参数初始化赋值
IF Frist THEN
      aCom1Params.udiParameterId := COM.CAA_Parameter_Constants.udiPort;
      aCom1Params.udiValue := 2;
      aCom1Params.udiParameterId := COM.CAA_Parameter_Constants.udiStopBits;
      aCom1Params.udiValue := COM.STOPBIT.ONESTOPBIT;//停止位
      aCom1Params.udiParameterId := COM.CAA_Parameter_Constants.udiParity;
      aCom1Params.udiValue := COM.PARITY.NONE;//检验位
      aCom1Params.udiParameterId := COM.CAA_Parameter_Constants.udiBaudrate;
      aCom1Params.udiValue := 19200;//波特率
      aCom1Params.udiParameterId := COM.CAA_Parameter_Constants.udiTimeout;
      aCom1Params.udiValue := 0;
      aCom1Params.udiParameterId := COM.CAA_Parameter_Constants.udiByteSize;
      aCom1Params.udiValue := 8;
      aCom1Params.udiParameterId := COM.CAA_Parameter_Constants.udiBinary;
      aCom1Params.udiValue := 0;
      udiListLength := SIZEOF(aCom1Params)/SIZEOF(COM.PARAMETER);
      Frist:=0;
END_IF



运行上述程序即完成串口初始化,因为涉及到codesys串口库,所以在工程中添加Serial_Communication库。


打开串口,生成COM连接的句柄。(生成的句柄在读和写的操作中,需要调用到。)

如下是串口读写的操作指令,COM.Open和COM.Write

建立发送数组和接收缓存数组,将地址指针赋值到读和写的输入端,即可完成读写操作。
如下是与串口工具的调试过程截屏:

接收缓存区全是0

串中工具发送数据。

每隔1秒去读缓存区,读到报文后读的库会显示收到的报文字节长度。

这是写的数组,里边存了发送的值。

以上是串口工具接收到PLC发的报文。





szons2019 发表于 2020-3-21 23:30:54

今天测试了一下485串口自由口的的调试,用调试软件分别与PLC和扫码枪测试都正常,可是扫码枪与PLC测试时发现数据总是少几个,这个是什么情况,希望能得到帮助

xudajun 发表于 2020-3-23 09:22:19

szons2019 发表于 2020-3-21 23:30
今天测试了一下485串口自由口的的调试,用调试软件分别与PLC和扫码枪测试都正常,可是扫码枪与PLC测试时发 ...

少的数据是中间的,还是最后几个?
页: [1]
查看完整版本: 怎么使用Codesys系统自带的串行口库?(自由协议)