合信论坛

快捷导航
查看: 16604|回复: 2

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

[复制链接]
发表于 2019-8-22 16:13:54 | 显示全部楼层 |阅读模式
如果你需要使用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
Name  of 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  波特率   9600  19200 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[1..7] OF COM.PARAMETER;
        udiListLength: USINT;
        Frist: BOOL := 1;
END_VAR


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



运行上述程序即完成串口初始化,因为涉及到codesys串口库,所以在工程中添加Serial_Communication库。
企业微信截图_20190822155403.png

打开串口,生成COM连接的句柄。(生成的句柄在读和写的操作中,需要调用到。)
企业微信截图_20190822155014.png
如下是串口读写的操作指令,COM.Open和COM.Write
企业微信截图_20190822155253.png
建立发送数组和接收缓存数组,将地址指针赋值到读和写的输入端,即可完成读写操作。
如下是与串口工具的调试过程截屏:
企业微信截图_20190822155838.png
接收缓存区全是0
企业微信截图_20190822155857.png
串中工具发送数据。
企业微信截图_20190822155924.png
每隔1秒去读缓存区,读到报文后读的库会显示收到的报文字节长度。
企业微信截图_20190822160246.png
这是写的数组,里边存了发送的值。
企业微信截图_20190822161012.png
以上是串口工具接收到PLC发的报文。





Serial485.project

159.23 KB, 下载次数: 1939

0

主题

1

帖子

18

积分

新手上路

Rank: 1

积分
18
发表于 2020-3-21 23:30:54 | 显示全部楼层
今天测试了一下485串口自由口的的调试,用调试软件分别与PLC和扫码枪测试都正常,可是扫码枪与PLC测试时发现数据总是少几个,这个是什么情况,希望能得到帮助
回复

使用道具 举报

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

少的数据是中间的,还是最后几个?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

客服热线
400-700-4858 周一至周五:09:00 - 18:00
深圳市南山区打石一路深圳国际创新谷6栋A座9层

深圳市合信自动化技术有限公司(简称“合信技术”)成立于2003年,高新技术企业,专注于工业自动化产品的研发、生产、销售和技术服务,依靠高质量、高性能的自动化控制产品与方案为客户创造最大价值,立志于成为全球领先的工业自动化解决方案供应商。

Archiver|手机版|小黑屋|COTRUST Inc. ( 粤ICP备13051915号 )

GMT+8, 2024-3-29 16:17 , Processed in 0.089424 second(s), 23 queries .

快速回复 返回顶部 返回列表