1、概述飞控单板PLC通信口485通讯规约本文描述了飞控PLC上报的Modbus通讯规约标准,应用于向上级监控设备上报数据时的通讯规约。2、适用范围规约适用于深圳市飞控自动化有限公司开发的可编程控制器,是开发、测试PLC模块通讯软件的依据。4、PLC通信口定义D8120的各位对应的通信参数如下:位号名称内容0(位OFF)1(位ON)B0数据长7位8位B1B2奇偶位b2b1(0,0):无校验(0,1):奇数ODD(1,1):偶校验EVENB3停止位1位2位B4B5B6B7传送速率bpsb7b6b5b4b7b6b5b4{0,0,1,1}:300{0,1,1,1}:4800{0,1,0,0}:600{1,0,0,0}:9600{0,1,0,1}:1200{1,0,0,1}:19200{0,1,1,0}:2400{1,0,1,0}:38400B8起始符无有(D8124)B9终止符无有(D8125)B10B11不可使用B12B13B14B15通信协议B15b14b13b12{0,0,0,0}:三菱FX2N协议(从机){0,1,0,0}:MODBUSRTU(从机){1,0,0,0}:MODBUSRTU(主机IVRD,IVWR指令){1,1,0,0}:自由通信(RS指令,用CCD校验)例子:设PLC为MODBUSRTU从机,站号1,19200,8,N,15、PLC站号设定特殊寄存器D8121为PLC485通信时的地址,设定范围:0-31。此设定与D8120,PLC上电时执行义此就可以了。6、帧结构8Bit地8Bit功能nX8Bit数16BitCRC校验采用Modbus规约的RTU(RemoteTerminalUnit)方式,每个字节以2个十六进制数,有效的数据范围为0~9,A~F。地址指PLC模块的地址,范围:0~31功能码PLC支持功能码01(读M中间继电器)、03(读D寄存器数据)、05(设置单个M继电器)、06(设置D寄存器数据)。数据上报或下设的数据,按寄存器(数据地址)进行发送,每一个寄存器由两个字节组成,关于寄存器号的定义,请参阅附录A。CRC校验码CRC(CyclicalRedundancyCheck)对地址、功能码和数据进行校验,由两字节组成,CRC由传输设备生成,附加在数据帧中,如果由接收到数据计算出来的校验和与附加在数据后的校验和不一致,则有错误发生。关于CRC生成函数,请参阅附录B内容。7.命令解释7.1读取D寄存器,功能码03上位机发送数据查询命令信息帧,PLC模块接收到正确的查询命令后,对命令进行响应回送数据给上位机。格式如下:读取D寄存器命令帧格式(8个字节)字段字段说明0地址10功能码30起始地址高字节起始地址低字节,起始地址000数据个数高字节数据个数低字节,读3个数0CRCHiCRC高字节CRC低字节CRCLoPLC模块响应帧格式字段字段说明0地址10功能码30应答数据字节数D0Hi第1个数据高字节D0LoD1Hi第2个数据高字节D1LoD2Hi第3个数据高字节D2LoCRCHiCRC高字节CRCLoCRC低字节7.2设置单个D寄存器,功能码06上位机发送设置数据查询命令和设置数据,PLC模块接收到正确的设置命令后,将指定的数据设置成命令帧中携带的数据,并将数据原样返回进行响应,若设置不成功时,不应答。格式如下:设置命令帧格式字段字段说明0地址010功能码060设置数据地址高字节设置数据地址低字节,地址00设置数据高字节设置数据低字节,数据值为0CRCHiCRC高字节CRC低字节CRCLoPLC模块响应帧格式字段字段说明0地址010功能码060设置数据地址高字节设置数据地址低字节,地址200设置数据数据高字节设置数据数据低字节,数据30CRCHiCRC高字节CRC低字节CRCLo7.3读取M中间继电器,功能码01读取M命令帧格式,下面是读取M0-M7的例子:字段字段说明0地址010功能码010读取数据地址高字节读取数据地址低字节,地址00读取个数高字节读取个数低字节,数据值为0CRCHiCRC高字节CRC低字节CRCLoPLC模块响应帧格式字段字段说明0地址010功能码0101返回数据的字节数00返回的数据,字节的低位对应M0,最高位对应M7,0为触点断CRCHiCRC高字节CRC低字节CRCLo7.4写入M中间继电器,功能码05置位M命令帧格式,下面是置位M0的例子:字段字段说明0地址010功能码050置位M的地址高字节置位M的地址低字节,地址0F置位M0,数据是:FF00复位M0时该值为:00000CRCHiCRC高字节CRC低字节CRCLoPLC模块响应帧格式字段字段说明0地址010功能码050置位M的地址高字节置位M的地址低字节,地址0F置位M0,数据是:FF00复位M0时该值为:00000CRCHiCRC高字节CRC低字节CRCLo附录A数据地址定义数据类型地址范围寄存器D0~4095开关量M0-1023上位机如果读取设备不支持的数据地址或不存在的数据地址,设备不响应。上位机如果设置设备不支持的数据地址或不存在的数据地址,设备不响应。上位机如果下发设备不支持的功能码,则设备不响应。附录B:CRC16校验的计算方法CRC(CyclicalRedundancyCheck)由两字节组成,生成函数如下:1、CRC计算函数WORDModbusCRC(BYTE*pData,BYTElen){BYTEbyCRCHi=0xff;BYTEbyCRCLo=0xff;BYTEbyIdx;WORDcrc;while(len--){byIdx=byCRCHi^*pData++;byCRCHi=byCRCLo^gabyCRCHi[byIdx];byCRCLo=gabyCRCLo[byIdx];}crc=byCRCHi;crc=8;crc+=byCRCLo;returncrc;}CRC码表高字节BYTEgabyCRCHi[]={0x00,0xc1,0x81,0x40,0x01,0xc0,0x80,0x41,0x01,0xc0,0x80,0x41,0x00,0xc1,0x81,0x40,0x01,0xc0,0x80,0x41,0x00,0xc1,0x81,0x40,0x00,0xc1,0x81,0x40,0x01,0xc0,0x80,0x41,0x01,0xc0,0x80,0x41,0x00,0xc1,0x81,0x40,0x00,0xc1,0x81,0x40,0x01,0xc0,0x80,0x41,0x00,0xc1,0x81,0x40,0x01,0xc0,0x80,0x41,0x01,0xc0,0x80,0x41,0x00,0xc1,0x81,0x40,0x01,0xc0,0x80,0x41,0x00,0xc1,0x81,0x40,0x00,0xc1,0x81,0x40,0x01,0xc0,0x80,0x41,0x00,0xc1,0x81,0x40,0x01,0xc0,0x80,0x41,0x01,0xc0,0x80,0x41,0x00,0xc1,0x81,0x40,0x00,0xc1,0x81,0x40,0x01,0xc0,0x80,0x41,0x01,0xc0,0x80,0x41,0x00,0xc1,0x81,0x40,0x01,0xc0,0x80,0x41,0x00,0xc1,0x81,0x40,0x00,0xc1,0x81,0x40,0x01,0xc0,0x80,0x41,0x01,0xc0,0x80,0x41,0x00,0xc1,0x81,0x40,0x00,0xc1,0x81,0x40,0x01,0xc0,0x80,0x41,0x00,0xc1,0x81,0x40,0x01,0xc0,0x80,0x41,0x01,0xc0,0x80,0x41,0x00,0xc1,0x81,0x40,0x00,0xc1,0x81,0x40,0x01,0xc0,0x80,0x41,0x01,0xc0,0x80,0x41,0x00,0xc1,0x81,0x40,0x01,0xc0,0x80,0x41,0x00,0xc1,0x81,0x40,0x00,0xc1,0x81,0x40,0x01,0xc0,0x80,0x41,0x00,0xc1,0x81,0x40,0x01,0xc0,0x80,0x41,0x01,0xc0,0x80,0x41,0x00,0xc1,0x81,0x40,0x01,0xc0,0x80,0x41,0x00,0xc1,0x81,0x40,0x00,0xc1,0x81,0x40,0x01,0xc0,0x80,0x41,0x01,0xc0,0x80,0x41,0x00,0xc1,0x81,0x40,0x00,0xc1,0x81,0x40,0x01,0xc0,0x80,0x41,0x00,0xc1,0x81,0x40,0x01,0xc0,0x80,0x41,0x01,0xc0,0x80,0x41,0x00,0xc1,0x81,0x40};CRC码表高字节BYTEgabyCRCLo[]={0x00,0xc0,0xc1,0x01,0xc3,0x03,0x02,0xc2,0xc6,0x06,0x07,0xc7,0x05,0xc5,0xc4,0x04,0xcc,0x0c,0x0d,0xcd,0x0f,0xcf,0xce,0x0e,0x0a,0xca,0xcb,0x0b,0xc9,0x09,0x08,0xc8,0xd8,0x18,0x19,0xd9,0x1b,0xdb,0xda,0x1a,0x1e,0xde,0xdf,0x1f,0xdd,0x1d,0x1c,0xdc,0x14,0xd4,0xd5,0x15,0xd7,0x17,0x16,0xd6,0xd2,0x12,0x13,0xd3,0x11,0xd1,0xd0,0x10,0xf0,0x30,0x31,0xf1,0x33,0xf3,0xf2,0x32,0x36,0xf6,0xf7,0x37,0xf5,0x35,0x34,0xf4,0x3c,0xfc,0xfd,0x3d,0xff,0x3f,0x3e,0xfe,0xfa,0x3a,0x3b,0xfb,0x39,0xf9,0xf8,0x38,0x28,0xe8,0xe9,0x29,0xeb,0x2b,0x2a,0xea,0xee,0x2e,0x2f,0xef,0x2d,0xed,0xec,0x2c,0xe4,0x24,0x25,0xe5,0x27,0xe7,0xe6,0x26,0x22,0xe2,0xe3,0x23,0xe1,0x21,0x20,0xe0,0xa0,0x60,0x61,0xa1,0x63,0xa3,0xa2,0x62,0x66,0xa6,0xa7,0x67,0xa5,0x65,0x64,0xa4,0x6c,0xac,0xad,0x6d,0xaf,0x6f,0x6e,0xae,0xaa,0x6a,0x6b,0xab,0x69,0xa9,0xa8,0x68,0x78,0xb8,0xb9,0x79,0xbb,0x7b,0x7a,0xba,0xbe,0x7e,0x7f,0xbf,0x7d,0xbd,0xbc,0x7c,0xb4,0x74,0x75,0xb5,0x77,0xb7,0xb6,0x76,0x72,0xb2,0xb3,0x73,0xb1,0x71,0x70,0xb0,0x50,0x90,0x91,0x51,0x93,0x53,0x52,0x92,0x96,0x56,0x57,0x97,0x55,0x95,0x94,0x54,0x9c,0x5c,0x5d,0x9d,0x5f,0x9f,0x9e,0x5e,0x5a,0x9a,0x9b,0x5b,0x99,0x59,0x58,0x98,0x88,0x48,0x49,0x89,0x4b,0x8b,0x8a,0x4a,0x4e,0x8e,0x8f,0x4f,0x8d,0x4d,0x4c,0x8c,0x44,0x84,0x85,0x45,0x87,0x47,0x