單片機(jī) Modbus 多機(jī)通信程序設(shè)計(jì)
給從機(jī)下發(fā)不同的指令,從機(jī)去執(zhí)行不同的操作,這個(gè)就是判斷一下功能碼即可,和我們前邊學(xué)的實(shí)用串口例程是類似的。多機(jī)通信,無(wú)非就是添加了一個(gè)設(shè)備地址判斷而已,難度也不大。我們找了一個(gè) Modbus 調(diào)試精靈,通過(guò)設(shè)置設(shè)備地址,讀寫寄存器的地址以及數(shù)值數(shù)量等參數(shù),可以直接替代串口調(diào)試助手,比較方便的下發(fā)多個(gè)字節(jié)的數(shù)據(jù),如圖18-7所示。我們先來(lái)就圖中的設(shè)置和數(shù)據(jù)來(lái)對(duì) Modbus 做進(jìn)一步的分析,圖中的數(shù)據(jù)來(lái)自于調(diào)試精靈與我們接下來(lái)要講的例程之間的交互。
圖18-7 Modbus 調(diào)試精靈
如圖,我們的 USB 轉(zhuǎn) RS485 模塊虛擬出的是 COM5,波特率9600,無(wú)校驗(yàn)位,數(shù)據(jù)位是8位,1位停止位,設(shè)備地址假設(shè)為1。
寫寄存器的時(shí)候,如果我們要把01寫到一個(gè)地址是0000的寄存器地址里,點(diǎn)一下“寫入”,就會(huì)出現(xiàn)發(fā)送指令:01 06 00 00 00 01 48 0A。我們來(lái)分析一下這幀數(shù)據(jù),其中01是設(shè)備地址,06是功能碼,代表寫寄存器這個(gè)功能,后邊跟00 00表示的是要寫入的寄存器的地址,00 01就是要寫入的數(shù)據(jù),48 0A就是 CRC 校驗(yàn)碼,這是軟件自動(dòng)算出來(lái)的。而根據(jù) Modbus 協(xié)議,當(dāng)寫寄存器的時(shí)候,從機(jī)成功完成該指令的操作后,會(huì)把主機(jī)發(fā)送的指令直接返回,我們的調(diào)試精靈會(huì)接收到這樣一幀數(shù)據(jù):01 06 00 00 00 01 48 0A。
假如我們現(xiàn)在要從寄存器地址0002開(kāi)始讀取寄存器,并且讀取的數(shù)量是2個(gè)。點(diǎn)一下“讀出”,就會(huì)出現(xiàn)發(fā)送指令:01 03 00 02 00 02 65 CB。其中01是設(shè)備地址,03是功能碼,代表讀寄存器這個(gè)功能,00 02就是讀寄存器的起始地址,后一個(gè)00 02就是要讀取2個(gè)寄存器的數(shù)值,65 CB就是 CRC 校驗(yàn)。而接收到的數(shù)據(jù)是:01 03 04 00 00 00 00 FA 33。其中01是設(shè)備地址,03是功能碼,04代表的是后邊讀到的數(shù)據(jù)字節(jié)數(shù)是4個(gè),00 00 00 00分別是地址00 02和00 03的寄存器內(nèi)部的數(shù)據(jù),而 FA 33 就是 CRC 校驗(yàn)了。
似乎越來(lái)越明朗了,所謂的 Modbus 通信協(xié)議,無(wú)非就是主機(jī)下發(fā)了不同的指令,從機(jī)根據(jù)指令的判斷來(lái)執(zhí)行不同的操作而已。由于我們的開(kāi)發(fā)板沒(méi)有 Modbus 功能碼那么多相應(yīng)的功能,我們?cè)诔绦蛑卸x了一個(gè)數(shù)組 regGroup[5],相當(dāng)于5個(gè)寄存器,此外又定義了第6個(gè)寄存器,控制蜂鳴器,通過(guò)下發(fā)不同的指令我們改變寄存器組的數(shù)據(jù)或者改變蜂鳴器的開(kāi)關(guān)狀態(tài)。在 Modbus 協(xié)議里寄存器的地址和數(shù)值都是16位的,即2個(gè)字節(jié),我們默認(rèn)高字節(jié)是 0x00,低字節(jié)就是數(shù)組 regGroup 對(duì)應(yīng)的值。其中地址 0x0000 到 0x0004 對(duì)應(yīng)的就是 regGroup數(shù)組中的元素,我們寫入的同時(shí)把數(shù)字又顯示到 1602 液晶上,而 0x0005 這個(gè)地址,寫入 0x00,蜂鳴器就不響,寫入任何其它數(shù)值,蜂鳴器就報(bào)警。我們單片機(jī)的主要工作也就是解析串口接收的數(shù)據(jù)執(zhí)行不同操作。 /*Lcd1602.c 文件程序源代碼***/ (此處省略,可參考之前章節(jié)的代碼) /****RS485.c 文件程序源代碼*****/ (此處省略,可參考之前章節(jié)的代碼) /****CRC16.c 文件程序源代碼****/
/*CRC16計(jì)算函數(shù),ptr-數(shù)據(jù)指針,len-數(shù)據(jù)長(zhǎng)度,返回值-計(jì)算出的CRC16數(shù)值*/unsignedintGetCRC16(unsignedchar*ptr,unsignedcharlen){unsignedintindex;unsignedcharcrch=0xFF;//高CRC字節(jié)unsignedcharcrcl=0xFF;//低CRC字節(jié)unsignedcharcodeTabH[]={//CRC高位字節(jié)值表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};unsignedcharcodeTabL[]={//CRC低位字節(jié)值表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,0x46,0x86,0x82,0x42,0x43,0x83,0x41,0x81,0x80,0x40};while(len--){//計(jì)算指定長(zhǎng)度的CRCindex=crch^*ptr++;crch=crcl^TabH[index];crcl=TabL[index];}return((crch<<8)"crcl);}