MODBUS RTU MASTER的C語言程式碼
阿新 • • 發佈:2021-11-12
轉自:https://blog.csdn.net/chuanshaoke/article/details/7352602
[csharp]view plaincopy
- 這是我幾年前寫的一個關於MODBUSRTUMASTER的C語言程式碼,用於嵌入式控制器內。另一個觸控式螢幕MCGS_E作為MODBUSRTUSLAVE。
- 下面是相關程式碼,以前測試過,可以執行。由於目前的程式碼尚有很多地方值得改進,所以近期想把它改進重寫。高手們有什麼建議,請多多賜教。
- 若您找到網路上有共享的MODBUS的原始碼,請與我分享。非常感謝。
- 下面沒有main()函式,大家可以自己寫個,把ListenToTouch函式放進去就可以了。
- /*********************************************************************************/
- /*函式名稱:strmcpy()
- *輸入引數:共個引數;
- *輸出引數:共個引數;
- *返回值:
- *需儲存的引數:共個引數;
- *功能介紹:
- (1)字元陣列拷貝;
- *修改日誌:
- *[2006-3-617:07]Ver.1.00
- 開始編寫;
- 完成;
- /**/
- /*********************************************************************************/
- voidstrmcpy(unsignedchardest[],unsignedcharsrc[],intcount)
- {
- inti;
- for(i=0;i<count;i++)
- {
- dest[i]=src[i];
- }
- dest[i]='/0';
- }
- /*****************************************************************************/
- /*********************************************************************************/
- /*函式名稱:bitmcpy()
- *輸入引數:共個引數;
- *輸出引數:共個引數;
- *返回值:
- *需儲存的引數:共個引數;
- *功能介紹:
- (1)開關量陣列拷貝;
- *修改日誌:
- *[2006-3-714:59]Ver.1.00
- 開始編寫;
- 完成;
- /**/
- /*********************************************************************************/
- voidbitmcpy(intdest[],intsrc[],intcount)
- {
- inti;
- for(i=0;i<count;i++)
- {
- dest[i]=src[i];
- }
- }
- /*****************************************************************************/
- /*********************************************************************************/
- /*函式名稱:strmcmp()
- *輸入引數:共個引數;
- *輸出引數:共個引數;
- *返回值:
- *需儲存的引數:共個引數;
- *功能介紹:
- (1)字元陣列比較;相同則返回0,不相同則返回1;
- *修改日誌:
- *[2006-3-617:41]Ver.1.00
- 開始編寫;
- 完成;
- /**/
- /*********************************************************************************/
- intstrmcmp(unsignedcharstr1[],unsignedcharstr2[],intcount)
- {
- inti;
- for(i=0;i<count;i++)
- {
- if(str1[i]!=str2[i])
- {
- return1;
- }
- }
- return0;
- }
- /*****************************************************************************/
- /*********************************************************************************/
- /*函式名稱:Datamcmp()
- *輸入引數:共個引數;
- *輸出引數:共個引數;
- *返回值:
- *需儲存的引數:共個引數;
- *功能介紹:
- (1)浮點陣列比較;相同則返回0,不相同則返回1;
- *修改日誌:
- *[2006-3-618:05]Ver.1.00
- 開始編寫;
- 完成;
- *[2006-3-913:52]
- 加了一層括號,以前是錯誤的
- if(!((data1[i]-data2[i]<0.0001)&&(data1[i]-data2[i]>-0.0001)))
- /**/
- /*********************************************************************************/
- intDatamcmp(floatdata1[],floatdata2[],intcount)
- {
- inti;
- for(i=0;i<count;i++)
- {
- if(!((data1[i]-data2[i]<0.0001)&&(data1[i]-data2[i]>-0.0001)))
- {
- return1;
- }
- }
- return0;
- }
- /*****************************************************************************/
- /*********************************************************************************/
- /*函式名稱:Bitmcmp()
- *輸入引數:共個引數;
- *輸出引數:共個引數;
- *返回值:
- *需儲存的引數:共個引數;
- *功能介紹:
- (1)開關量陣列比較;相同則返回0,不相同則返回1;
- *修改日誌:
- *[2006-3-618:10]Ver.1.00
- 開始編寫;
- 完成;
- /**/
- /*********************************************************************************/
- intBitmcmp(intdata1[],intdata2[],intcount)
- {
- inti;
- for(i=0;i<count;i++)
- {
- if(data1[i]!=data2[i])
- {
- return1;
- }
- }
- return0;
- }
- /*****************************************************************************/
- /*********************************************************************************/
- /*函式名稱:GetCRC16()
- *輸入引數:共個引數;
- *輸出引數:共個引數;
- *返回值:
- *需儲存的引數:共個引數;
- *功能介紹:
- (1)CRC16校驗;返回校驗碼;
- *修改日誌:
- *[2005-11-2816:40]Ver.1.00
- 開始編寫;
- 完成;
- /**/
- /*********************************************************************************/
- unsignedshortGetCRC16(unsignedchar*puchMsg,unsignedshortusDataLen)
- {
- /*CRC高位位元組值表*/
- unsignedcharauchCRCHi[256]={
- 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
- };
- unsignedcharauchCRCLo[256]={
- 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
- };
- unsignedcharuchCRCHi=0xFF;/*高CRC位元組初始化*/
- unsignedcharuchCRCLo=0xFF;/*低CRC位元組初始化*/
- unsigneduIndex=0;/*CRC迴圈中的索引*/
- while(usDataLen--)/*傳輸訊息緩衝區*/
- {
- uIndex=uchCRCHi^*puchMsg++;/*計算CRC*/
- uchCRCHi=uchCRCLo^auchCRCHi[uIndex];
- uchCRCLo=auchCRCLo[uIndex];
- }
- return(unsignedshort)((unsignedshort)uchCRCHi<<8|uchCRCLo);
- }
- /*****************************************************************************/
- /*********************************************************************************/
- /*函式名稱:SendRtuCmdToModBus()
- *輸入引數:共個引數;
- *輸出引數:共個引數;
- *返回值:無
- *需儲存的引數:共個引數;
- *功能介紹:
- (1)傳送ModBusRTU指令到ModbusSlave,加上CRC16校驗碼;
- *修改日誌:
- *[2005-11-2816:40]Ver.1.00
- 開始編寫;
- 完成;
- *[2006-3-19:02]
- 返回型別改為void;
- /**/
- /*********************************************************************************/
- voidSendRtuCmdToModBus(intModPort,unsignedcharcmd[],unsignedshortlen)
- {
- unsignedshortCRC16;
- CRC16=GetCRC16(cmd,len);
- ToComBufn(ModPort,cmd,len);
- ToCom(ModPort,(unsignedchar)(CRC16>>8&0x00FF));/*sendCRC16high*/
- ToCom(ModPort,(unsignedchar)(CRC16&0x00FF));/*sendCRC16low*/
- }
- /*****************************************************************************/
- /*********************************************************************************/
- /*函式名稱:ReadModBusRtuSlave()
- *輸入引數:共8個引數;
- *輸出引數:共5個引數;
- *返回值:成功與否1:成功,2:失敗;
- *需儲存的引數:共0個引數;
- *功能介紹:
- (1)讀取ModBusRtuSlave,並解碼輸出反饋字串相關內容;
- ReadModBusRtuSlave(COMPORT1,ModAddr,ModFunction,ModByteNum,
- ModData,&ModDataLen,ModTimeout=10,waitTime=30)
- *修改日誌:
- *[2005-11-2816:40]Ver.1.00
- 開始編寫;
- 完成;
- *[2006-3-19:23]
- 增加了intcounter=0;
- unsignedcharModBusMessage[MOD_STR_MAX_LEN];
- *[2006-3-113:46]
- 增加了strmcpy(ModData,ModBusMessage+3,ModBusIdx-5);
- /**/
- /*********************************************************************************/
- intReadModBusRtuSlave(intModPort,unsignedchar*ModAddr,unsignedchar*ModFunction,unsignedchar*ModByteNum,
- unsignedcharModData[],unsignedchar*ModDataLen,unsignedModTimeout,unsignedwaitTime)
- {
- unsignedchardata;
- unsignedlongt;
- inti;
- unsignedshortCRC16;
- intModBusIdx=0;
- intcounter=0;
- unsignedcharModBusMessage[MOD_STR_MAX_LEN]={'/0'};
- while((!(IsCom(ModPort))&&(counter<waitTime)))
- {
- DelayMs(1);
- counter++;
- }
- StopWatchStart(MODBUS_STOP_WATCH);
- do
- {
- while(IsCom(ModPort)&&(ModBusIdx<MOD_STR_MAX_LEN-1))
- {
- data=ReadCom(ModPort);
- ModBusMessage[ModBusIdx++]=data;
- StopWatchStart(MODBUS_STOP_WATCH);
- /*Print("[%02X]",data);*/
- }
- StopWatchReadValue(MODBUS_STOP_WATCH,&t);
- }
- while(t<=ModTimeout);
- if(ModBusIdx==0)return0;
- ModBusMessage[ModBusIdx]='/0';
- StopWatchStop(MODBUS_STOP_WATCH);
- CRC16=GetCRC16(ModBusMessage,ModBusIdx-2);
- /*for(i=0;i>8&0x00FF)==ModBusMessage[ModBusIdx-2]&&(unsignedchar)(CRC16&0x00FF)==ModBusMessage[ModBusIdx-1])
- {
- *ModAddr=ModBusMessage[0];
- *ModFunction=ModBusMessage[1];
- *ModByteNum=ModBusMessage[2];
- strmcpy(ModData,ModBusMessage+3,ModBusIdx-5);
- /*Print("idx%d",ModBusIdx-5);/*test*/
- /*for(i=3;i<ModBusIdx-2;i++)
- {
- ModData[i-3]=ModBusMessage[i];
- }
- ModData[i]='/0';
- */
- *ModDataLen=ModBusIdx-5;
- /*Print("{DataLen=%d}/n/r",*ModDataLen);*/
- return1;
- }
- else
- {
- *ModDataLen=0;
- return0;
- /*CRC16error*/
- /*Print("{CRC16Error}/n/r");*/
- }
- }
- /*-------------------------------------------------------------------------------*/
- /*********************************************************************************/
- /*函式名稱:ResponseFromModBusRtuSlave()
- *輸入引數:共8個引數;
- *輸出引數:共5個引數;
- *返回值:成功與否1:成功,2:失敗;
- *需儲存的引數:共0個引數;
- *功能介紹:
- (1)寫引數到ModBusRtuSlave後,ModbusRtuSlave反饋字串;
- *修改日誌:
- *[2006-3-112:34]Ver.1.00
- 開始編寫;
- *[2006-3-112:48]
- 完成;
- *[2006-3-616:32]
- strcpy(totalStr,ModBusMessage);
- 改為strmcpy(totalStr,ModBusMessage,ModBusIdx);
- /**/
- /*********************************************************************************/
- intResponseFromModBusRtuSlave(intModPort,unsignedchartotalStr[],
- unsignedchar*ModAddr,unsignedchar*ModFunction,unsignedshort*ModDataAddr,
- unsignedshort*ModDataNum,unsignedModTimeout,unsignedwaitTime)
- {
- unsignedchardata;
- unsignedlongt;
- unsignedshortCRC16;
- intModBusIdx=0;
- intcounter=0;
- unsignedcharModBusMessage[MOD_STR_MAX_LEN]={'/0'};
- while((!(IsCom(ModPort))&&(counter<waitTime)))
- {
- DelayMs(1);
- counter++;
- }
- StopWatchStart(MODBUS_STOP_WATCH);
- do
- {
- while(IsCom(ModPort)&&(ModBusIdx<MOD_STR_MAX_LEN-1))
- {
- data=ReadCom(ModPort);
- ModBusMessage[ModBusIdx++]=data;
- StopWatchStart(MODBUS_STOP_WATCH);
- /*Print("[%02X]",data);*/
- }
- StopWatchReadValue(MODBUS_STOP_WATCH,&t);
- }
- while(t<=ModTimeout);
- if(ModBusIdx==0)return0;
- ModBusMessage[ModBusIdx]='/0';
- strmcpy(totalStr,ModBusMessage,ModBusIdx);
- StopWatchStop(MODBUS_STOP_WATCH);
- CRC16=GetCRC16(ModBusMessage,ModBusIdx-2);
- if((unsignedchar)(CRC16>>8&0x00FF)==ModBusMessage[ModBusIdx-2]&&(unsignedchar)(CRC16&0x00FF)==ModBusMessage[ModBusIdx-1])
- {
- *ModAddr=ModBusMessage[0];
- *ModFunction=ModBusMessage[1];
- *ModDataAddr=(unsignedshort)((unsignedshort)ModBusMessage[2]<<8|ModBusMessage[3]);
- *ModDataNum=(unsignedshort)((unsignedshort)ModBusMessage[4]<<8|ModBusMessage[5]);
- return1;
- }
- else
- {
- return0;
- /*CRC16error*/
- /*Print("{CRC16Error}/n/r");*/
- }
- }
- /*-------------------------------------------------------------------------------*/
- /*********************************************************************************/
- /*函式名稱:ResponseSingleBitFromModBus()
- *輸入引數:共8個引數;
- *輸出引數:共5個引數;
- *返回值:成功與否1:成功,2:失敗;
- *需儲存的引數:共0個引數;
- *功能介紹:
- (1)寫引數到ModBusRtuSlave後,ModbusRtuSlave反饋字串;
- *修改日誌:
- *[2006-3-714:40]Ver.1.00
- 開始編寫;
- *[2006-3-714:40]
- 完成;
- /**/
- /*********************************************************************************/
- intResponseSingleBitFromModBus(intModPort,unsignedchartotalStr[],
- unsignedchar*ModAddr,unsignedchar*ModFunction,unsignedshort*ModDataAddr,
- unsignedshort*ModDataState,unsignedModTimeout,unsignedwaitTime)
- {
- unsignedchardata;
- unsignedlongt;
- unsignedshortCRC16;
- intModBusIdx=0;
- intcounter=0;
- unsignedcharModBusMessage[MOD_STR_MAX_LEN]={'/0'};
- while((!(IsCom(ModPort))&&(counter<waitTime)))
- {
- DelayMs(1);
- counter++;
- }
- StopWatchStart(MODBUS_STOP_WATCH);
- do
- {
- while(IsCom(ModPort)&&(ModBusIdx<MOD_STR_MAX_LEN-1))
- {
- data=ReadCom(ModPort);
- ModBusMessage[ModBusIdx++]=data;
- StopWatchStart(MODBUS_STOP_WATCH);
- /*Print("[%02X]",data);*/
- }
- StopWatchReadValue(MODBUS_STOP_WATCH,&t);
- }
- while(t<=ModTimeout);
- if(ModBusIdx==0)return0;
- ModBusMessage[ModBusIdx]='/0';
- strmcpy(totalStr,ModBusMessage,ModBusIdx);
- StopWatchStop(MODBUS_STOP_WATCH);
- CRC16=GetCRC16(ModBusMessage,ModBusIdx-2);
- if((unsignedchar)(CRC16>>8&0x00FF)==ModBusMessage[ModBusIdx-2]&&(unsignedchar)(CRC16&0x00FF)==ModBusMessage[ModBusIdx-1])
- {
- *ModAddr=ModBusMessage[0];
- *ModFunction=ModBusMessage[1];
- *ModDataAddr=(unsignedshort)((unsignedshort)ModBusMessage[2]<<8|ModBusMessage[3]);
- if(ModBusMessage[4])*ModDataState=1;
- else*ModDataState=0;
- return1;
- }
- else
- {
- return0;
- /*CRC16error*/
- /*Print("{CRC16Error}/n/r");*/
- }
- }
- /*-------------------------------------------------------------------------------*/
- /*********************************************************************************/
- /*函式名稱:ChangeAllParameterDataByModBus()
- *輸入引數:共個引數;
- *輸出引數:共個引數;
- *返回值:無
- *需儲存的引數:共個引數;
- *功能介紹:
- (1)將MODBUSSLAVE返回的資料字元轉化為引數值;
- (2)void*memcpy(void*dest,constvoid*src,size_tn)
- (3)四個字元的順序應倒一下。
- *修改日誌:
- *[2006-3-114:10]Ver.1.00
- 開始編寫;
- *[2006-3-114:35]
- 完成;
- *[2006-3-615:57]
- 四個字元的順序應倒一下。
- /**/
- /*********************************************************************************/
- voidChangeAllParameterDataByModBus(floatparameterData[],unsignedcharModDataBack[],unsignedshortnum)
- {
- inti;
- charsingleStr[5]={'/0','/0','/0','/0','/0'};
- float*pf;
- floatdataf=0;
- intlen=4;
- pf=&dataf;
- for(i=0;i<num;i++)
- {
- singleStr[0]=ModDataBack[i*4+3];
- singleStr[1]=ModDataBack[i*4+2];
- singleStr[2]=ModDataBack[i*4+1];
- singleStr[3]=ModDataBack[i*4];
- singleStr[4]='/0';
- memcpy(pf,singleStr,len);
- parameterData[i]=*pf;
- /*Print("%-6.2f/t",parameterData[i]);/*test*/
- }
- }
- /*-------------------------------------------------------------------------------*/
- /*********************************************************************************/
- /*函式名稱:ChangeAllButtonsDataByModBus()
- *輸入引數:共個引數;
- *輸出引數:共個引數;
- *返回值:無
- *需儲存的引數:共個引數;
- *功能介紹:
- (1)將MODBUSSLAVE返回的資料字元轉化為按鈕值;
- *修改日誌:
- *[2006-3-114:03]Ver.1.00
- 開始編寫;
- *[2006-3-114:10]
- 完成;
- /**/
- /*********************************************************************************/
- voidChangeAllButtonsDataByModBus(intbuttonData[],unsignedcharModDataBack[],unsignedshortnum)
- {
- unsignedcharbutton8Data=0;
- inti,j;
- for(j=0;j<(int)(num/8);j++)
- {
- button8Data=ModDataBack[j];
- for(i=0;i<8;i++)
- {
- buttonData[i+j*8]=(int)(button8Data&(int)pow(2,i))/(int)pow(2,i);
- }
- }
- button8Data=ModDataBack[j];
- for(i=0;i<num%8;i++)
- {
- buttonData[i+j*8]=(int)(button8Data&(int)pow(2,i))/(int)pow(2,i);
- }
- }
- /*-------------------------------------------------------------------------------*/
- /*********************************************************************************/
- /*函式名稱:WordDataToModbusStr()
- *輸入引數:共個引數;
- *輸出引數:共個引數;
- *返回值:無
- *需儲存的引數:共個引數;
- *功能介紹:
- (1)parameterData或AIData轉化為MODBUSSLAVE的WORD值的STR;
- (2)void*memcpy(void*dest,constvoid*src,size_tn)
- (3)四個字元的順序應倒一下。
- *修改日誌:
- *[2006-3-114:37]Ver.1.00
- 開始編寫;
- *[2006-3-114:49]
- 完成;
- *[2006-3-615:57]
- 四個字元的順序應倒一下。
- /**/
- /*********************************************************************************/
- voidWordDataToModbusStr(unsignedcharwriteCmdStr[],floatdata[],unsignedshortnum,unsignedshortstartAddr)
- {
- inti;
- charsingleStr[5]={'/0','/0','/0','/0','/0'};
- float*pf;
- unsignedchar*pstr;
- floatdataf=0;
- intlen=4;
- pf=&dataf;
- pstr=singleStr;
- for(i=0;i<num;i++)
- {
- *pf=data[i];
- memcpy(pstr,pf,len);
- writeCmdStr[startAddr+i*4+3]=singleStr[0];
- writeCmdStr[startAddr+i*4+2]=singleStr[1];
- writeCmdStr[startAddr+i*4+1]=singleStr[2];
- writeCmdStr[startAddr+i*4]=singleStr[3];
- }
- }
- /*-------------------------------------------------------------------------------*/
- /*********************************************************************************/
- /*函式名稱:BitDataToModbusStr()
- *輸入引數:共個引數;
- *輸出引數:共個引數;
- *返回值:無
- *需儲存的引數:共個引數;
- *功能介紹:
- (1)data轉化為MODBUSSLAVE的WORD值的STR;
- *修改日誌:
- *[2006-3-114:51]Ver.1.00
- 開始編寫;
- *[2006-3-115:02]
- 完成;
- /**/
- /*********************************************************************************/
- voidBitDataToModbusStr(unsignedcharwriteCmdStr[],intdata[],unsignedshortnum,unsignedshortstartAddr)
- {
- inti,j;
- unsignedcharbutton8Data=0;
- for(j=0;j<(int)(num/8);j++)
- {
- button8Data=0;
- for(i=0;i<8;i++)
- {
- button8Data+=data[i+j*8]*(int)pow(2,i);
- }
- writeCmdStr[startAddr+j]=button8Data;
- }
- if(num%8)
- {
- button8Data=0;
- for(i=0;i<num%8;i++)
- {
- button8Data+=data[i+j*8]*(int)pow(2,i);
- }
- writeCmdStr[startAddr+j]=button8Data;
- }
- }
- /*-------------------------------------------------------------------------------*/
- /*********************************************************************************/
- /*函式名稱:ReadWordFromModBusSlave()
- *輸入引數:共2個引數;
- *輸出引數:共1個引數;
- *返回值:成功與否;
- *需儲存的引數:共1個引數;
- *功能介紹:
- (1)從觸控式螢幕中讀取設定引數值;
- (2)讀取指令為0103HALAHNLNHCLC;其中01為MODBUSSLAVE的地址,
- 03為讀連續多個WORD暫存器的功能碼,HALA為第一個暫存器的高低位地址,
- HNLN為暫存器數量的高低位,HCLC為CRC校驗碼;
- (3)觸控式螢幕reset後各個引數歸零,7188檢測到此狀態後向除摸屏傳送設定引數;
- *修改日誌:
- *[2005-11-2816:51]Ver.1.00
- 開始編寫;
- *[2005-11-2818:10]
- 完成初稿,未測試;
- *[2006-3-39:38]
- staticunsignedcharoldModDataBack[400];改為形參;
- *[2006-3-317:58]
- 增加intkind形參;
- *[2006-3-616:34]
- strmcpy(oldModDataBack,ModDataBack,ModDataLenBack);
- *[2006-3-912:33]
- 去掉
- (strmcmp(oldModDataBack,ModDataBack,ModDataLenBack))
- /**/
- /*********************************************************************************/
- intReadWordFromModBusSlave(intkind,intcomPort,floatparameterData[],unsignedshortnum,
- unsignedshortModDataAddr,intparameterEEPROMAddr,unsignedcharoldModDataBack[])
- {
- floatparameterJudge[50];
- unsignedcharreadCmdStr[15];
- unsignedcharModAddr=1;
- unsignedcharModFunction=3;
- unsignedshortModDataNum;
- unsignedshortreadCmdLen=6;
- intisReadOK=0;
- unsignedcharModAddrBack=0;
- unsignedcharModFunctionBack=0;
- unsignedcharModByteNumBack=0;
- unsignedcharModDataBack[MOD_STR_MAX_LEN]={'/0'};
- unsignedcharModDataLenBack=0;
- /*unsignedshortModWord[100];*/
- intparameterDecimalEEPROM[PARAMETER_DATA_NUM]=PARAMETER_DECIMAL_EEPROM;/*adjust*/
- intparameterStartAddr=0;
- chari7188Addr[5];
- strcpy(i7188Addr,I7188_ADDRESS);/*adjust*/
- if(kind==1)
- {
- strcpy(i7188Addr,I7188_ADDRESS);
- }
- elseif(kind==2)
- {
- strcpy(i7188Addr,I7188_ADDRESS_2);
- }
- elseif(kind==3)
- {
- strcpy(i7188Addr,I7188_ADDRESS_3);
- }
- ModDataNum=(unsignedshort)num*2;
- readCmdStr[0]=ModAddr;
- readCmdStr[1]=ModFunction;
- readCmdStr[2]=(unsignedchar)(ModDataAddr>>8&0x00FF);
- readCmdStr[3]=(unsignedchar)(ModDataAddr&0x00FF);
- readCmdStr[4]=(unsignedchar)(ModDataNum>>8&0x00FF);
- readCmdStr[5]=(unsignedchar)(ModDataNum&0x00FF);
- readCmdStr[6]='/0';
- ClearCom(comPort);
- SendRtuCmdToModBus(comPort,readCmdStr,readCmdLen);
- isReadOK=ReadModBusRtuSlave(comPort,&ModAddrBack,&ModFunctionBack,&ModByteNumBack,
- ModDataBack,&ModDataLenBack,MOD_TIMEOUT,MOD_WAITTIME);
- if(1==isReadOK)
- {
- if((ModAddr==ModAddrBack)&&(ModFunction==ModFunctionBack)&&(ModByteNumBack==ModDataLenBack))
- {
- if(ModDataNum*2==ModDataLenBack)/*!!!*/
- {
- ChangeAllParameterDataByModBus(parameterJudge,ModDataBack,num);
- /*Print("%f%f%f/t",parameterData[0],parameterData[1],parameterData[2]);
- Print("/n%f%f%f/t",parameterJudge[0],parameterJudge[1],parameterJudge[2]);
- */if(parameterJudge[0]>=0.1)/*以溫度設定值為判斷點判斷正確性*/
- {
- /*Print("cp1.5/n");*/
- if(Datamcmp(parameterData,parameterJudge,num))
- {
- Print("cp1.6/n");
- ChangeAllParameterDataByModBus(parameterData,ModDataBack,num);
- if(IS_RS485)Set485DirToTransmit(COMPORT1);
- SendParameterDataToPCForwardly(COMPORT1,parameterData,i7188Addr);
- if(IS_RS485)WaitTransmitOver(COMPORT1);
- if(IS_RS485){DelayMs(2);Set485DirToReceive(COMPORT1);}
- WriteDataToEEPROM(parameterData,parameterDecimalEEPROM,num,parameterEEPROMAddr,parameterStartAddr);
- }
- strmcpy(oldModDataBack,ModDataBack,ModDataLenBack);
- }
- else
- {
- /*未初始化,傳送資料以初始*/
- WriteMultipleWordToModBusSlave(comPort,parameterData,num,ModDataAddr);
- }
- /*Print("/n%f%f%f/t",parameterData[0],parameterData[1],parameterData[2]);*/
- }
- return1;
- }
- else
- {
- /*收到的字串不匹配*/
- return0;
- }
- }
- else
- {
- /*未收到字串或CRC校驗出錯*/
- return0;
- }
- }
- /*-------------------------------------------------------------------------------*/
- /*********************************************************************************/
- /*函式名稱:ReadBitFromModBusSlave()
- *輸入引數:共2個引數;
- *輸出引數:共1個引數;
- *返回值:成功與否;
- *需儲存的引數:共1個引數;
- *功能介紹:
- (1)從觸控式螢幕中讀取按鈕值;
- (2)讀取指令為0101HALAHNLNHCLC;其中01為MODBUSSLAVE的地址,
- 01為讀連續多個BIT線圈的功能碼,HALA為第一個BIT的高低位地址,
- HNLN為BIT數量的高低位,HCLC為CRC校驗碼;
- *修改日誌:
- *[2005-11-2818:10]Ver.1.00
- 開始編寫;
- *[2005-11-2818:42]
- 完成初稿,未測試;
- *[2006-3-39:38]
- staticunsignedcharoldModDataBack[400];改為形參;
- *[2006-3-317:58]
- 增加intkind形參;
- *[2006-3-616:35]
- strmcpy(oldModDataBack,ModDataBack,ModDataLenBack);
- *[2006-3-912:35]
- 去掉(strmcmp(oldModDataBack,ModDataBack,ModDataLenBack))
- /**/
- /*********************************************************************************/
- intReadBitFromModBusSlave(intkind,intcomPort,intbuttonData[],unsignedshortnum,
- unsignedshortModDataAddr,intbuttonNVRAMAddr,unsignedcharoldModDataBack[])
- {
- intbuttonJudge[30];
- unsignedcharreadCmdStr[15];
- unsignedcharModAddr=1;
- unsignedcharModFunction=1;
- unsignedshortModDataNum;
- unsignedshortreadCmdLen=6;
- intisReadOK=0;
- unsignedcharModAddrBack=0;
- unsignedcharModFunctionBack=0;
- unsignedcharModByteNumBack=0;
- unsignedcharModDataBack[300]={'/0'};
- unsignedcharModDataLenBack=0;
- /*unsignedshortModBit[100];*/
- chari7188Addr[5];
- strcpy(i7188Addr,I7188_ADDRESS);/*adjust*/
- if(kind==1)
- {
- strcpy(i7188Addr,I7188_ADDRESS);
- }
- elseif(kind==2)
- {
- strcpy(i7188Addr,I7188_ADDRESS_2);
- }
- elseif(kind==3)
- {
- strcpy(i7188Addr,I7188_ADDRESS_3);
- }
- ModDataNum=(unsignedshort)num;
- readCmdStr[0]=ModAddr;
- readCmdStr[1]=ModFunction;
- readCmdStr[2]=(unsignedchar)(ModDataAddr>>8&0x00FF);
- readCmdStr[3]=(unsignedchar)(ModDataAddr&0x00FF);
- readCmdStr[4]=(unsignedchar)(ModDataNum>>8&0x00FF);
- readCmdStr[5]=(unsignedchar)(ModDataNum&0x00FF);
- readCmdStr[6]='/0';
- ClearCom(comPort);
- SendRtuCmdToModBus(comPort,readCmdStr,readCmdLen);
- isReadOK=ReadModBusRtuSlave(comPort,&ModAddrBack,&ModFunctionBack,&ModByteNumBack,
- ModDataBack,&ModDataLenBack,MOD_TIMEOUT,MOD_WAITTIME);
- if(1==isReadOK)
- {
- if((ModAddr==ModAddrBack)&&(ModFunction==ModFunctionBack)&&(ModByteNumBack==ModDataLenBack))
- {
- ChangeAllButtonsDataByModBus(buttonJudge,ModDataBack,num);
- if(Bitmcmp(buttonData,buttonJudge,num))
- {
- ChangeAllButtonsDataByModBus(buttonData,ModDataBack,num);
- if(IS_RS485)Set485DirToTransmit(COMPORT1);
- SendButtonDataToPC(COMPORT1,buttonData,i7188Addr);
- if(IS_RS485)WaitTransmitOver(COMPORT1);
- if(IS_RS485){DelayMs(2);Set485DirToReceive(COMPORT1);}
- WriteButtonToNVRAM(buttonData,num,buttonNVRAMAddr);
- }
- strmcpy(oldModDataBack,ModDataBack,ModDataLenBack);
- return1;
- }
- else
- {
- /*收到的字串不匹配*/
- return1;
- }
- }
- else
- {
- /*未收到字串或CRC校驗出錯*/
- return0;
- }
- }
- /*-------------------------------------------------------------------------------*/
- /*********************************************************************************/
- /*函式名稱:WriteMultipleWordToModBusSlave()
- *輸入引數:共2個引數;
- *輸出引數:共1個引數;
- *返回值:成功與否;
- *需儲存的引數:共1個引數;
- *功能介紹:
- (1)從觸控式螢幕中寫入設定引數值;
- (2)指令為0116HALAHNLNHDLD......HCLC;其中01為MODBUSSLAVE的地址,
- 16為寫連續多個WORD暫存器的功能碼,HALA為第一個暫存器的高低位地址,
- HNLN為暫存器數量的高低位,HCLC為CRC校驗碼;
- HDLD為資料高低位;
- *修改日誌:
- *[2005-11-2818:43]Ver.1.00
- 開始編寫;
- *[2006-3-112:20]
- 中間隔了很多時間;
- 完成初稿,未測試;
- /**/
- /*********************************************************************************/
- intWriteMultipleWordToModBusSlave(intcomPort,floatparameterData[],unsignedshortnum,unsignedshortModDataAddr)
- {
- unsignedcharwriteCmdStr[MOD_STR_MAX_LEN];/*adjust*/
- unsignedcharModAddr=1;
- unsignedcharModFunction=16;
- unsignedshortModDataNum;
- unsignedshortreadCmdLen;
- intisReadOK=0;
- unsignedshortbyteCount;
- unsignedchartotalStrBack[300]={'/0'};
- unsignedcharModAddrBack=0;
- unsignedcharModFunctionBack=0;
- unsignedshortModDataAddrBack;
- unsignedshortModDataNumBack;
- ModDataNum=(unsignedshort)num*2;
- byteCount=(unsignedshort)num*4;
- readCmdLen=7+byteCount;
- writeCmdStr[0]=ModAddr;
- writeCmdStr[1]=ModFunction;
- writeCmdStr[2]=(unsignedchar)(ModDataAddr>>8&0x00FF);
- writeCmdStr[3]=(unsignedchar)(ModDataAddr&0x00FF);
- writeCmdStr[4]=(unsignedchar)(ModDataNum>>8&0x00FF);
- writeCmdStr[5]=(unsignedchar)(ModDataNum&0x00FF);
- writeCmdStr[6]=(unsignedchar)(byteCount&0x00FF);
- WordDataToModbusStr(writeCmdStr,parameterData,num,7);
- writeCmdStr[7+byteCount]='/0';
- ClearCom(comPort);
- SendRtuCmdToModBus(comPort,writeCmdStr,readCmdLen);
- isReadOK=ResponseFromModBusRtuSlave(comPort,totalStrBack,
- &ModAddrBack,&ModFunctionBack,&ModDataAddrBack,
- &ModDataNumBack,MOD_TIMEOUT,MOD_WAITTIME);
- if(1==isReadOK)
- {
- if((ModAddr==ModAddrBack)&&(ModFunction==ModFunctionBack)&&(ModDataAddr==ModDataAddrBack)
- &&(ModDataNum==ModDataNumBack))
- {
- return1;
- }
- else
- {
- return0;
- }
- }
- else
- {
- /*未收到字串或CRC校驗出錯*/
- return0;
- }
- }
- /*-------------------------------------------------------------------------------*/
- /*********************************************************************************/
- /*函式名稱:WriteMultipleBitToModBusSlave()
- *輸入引數:共2個引數;
- *輸出引數:共1個引數;
- *返回值:成功與否;
- *需儲存的引數:共1個引數;
- *功能介紹:
- (1)從觸控式螢幕中寫入BIT;
- (2)指令為0115HALAHNLNHDLD......HCLC;其中01為MODBUSSLAVE的地址,
- 16為讀連續多個WORD暫存器的功能碼,HALA為第一個BIT的高低位地址,
- HNLN為BIT數量的高低位,HCLC為CRC校驗碼;
- HDLD為資料高低位;
- *修改日誌:
- *[2006-3-112:58]Ver.1.00
- 開始編寫;
- *[2006-3-113:29]
- 完成;
- /**/
- /*********************************************************************************/
- intWriteMultipleBitToModBusSlave(intcomPort,intDOData[],unsignedshortnum,unsignedshortModDataAddr)
- {
- unsignedcharwriteCmdStr[100];
- unsignedcharModAddr=1;
- unsignedcharModFunction=15;
- unsignedshortModDataNum;
- unsignedshortreadCmdLen;
- intisReadOK=0;
- unsignedshortbyteCount;
- unsignedchartotalStrBack[300]={'/0'};
- unsignedcharModAddrBack=0;
- unsignedcharModFunctionBack=0;
- unsignedshortModDataAddrBack;
- unsignedshortModDataNumBack;
- ModDataNum=(unsignedshort)num;
- if(num%8)
- byteCount=(unsignedshort)(num/8+1);
- elsebyteCount=(unsignedshort)(num/8);
- readCmdLen=7+byteCount;
- writeCmdStr[0]=ModAddr;
- writeCmdStr[1]=ModFunction;
- writeCmdStr[2]=(unsignedchar)(ModDataAddr>>8&0x00FF);
- writeCmdStr[3]=(unsignedchar)(ModDataAddr&0x00FF);
- writeCmdStr[4]=(unsignedchar)(ModDataNum>>8&0x00FF);
- writeCmdStr[5]=(unsignedchar)(ModDataNum&0x00FF);
- writeCmdStr[6]=(unsignedchar)(byteCount&0x00FF);
- BitDataToModbusStr(writeCmdStr,DOData,num,7);
- writeCmdStr[7+byteCount]='/0';
- ClearCom(comPort);
- SendRtuCmdToModBus(comPort,writeCmdStr,readCmdLen);
- isReadOK=ResponseFromModBusRtuSlave(comPort,totalStrBack,
- &ModAddrBack,&ModFunctionBack,&ModDataAddrBack,
- &ModDataNumBack,MOD_TIMEOUT,MOD_WAITTIME);
- if(1==isReadOK)
- {
- if((ModAddr==ModAddrBack)&&(ModFunction==ModFunctionBack)&&(ModDataAddr==ModDataAddrBack)
- &&(ModDataNum==ModDataNumBack))
- {
- return1;
- }
- else
- {
- return0;
- }
- }
- else
- {
- /*未收到字串或CRC校驗出錯*/
- return0;
- }
- }
- /*-------------------------------------------------------------------------------*/
- /*********************************************************************************/
- /*函式名稱:WriteSingleBitToModBusSlave()
- *輸入引數:共2個引數;
- *輸出引數:共1個引數;
- *返回值:成功與否;
- *需儲存的引數:共1個引數;
- *功能介紹:
- (1)從觸控式螢幕中寫入BIT;
- (2)指令為0105HALAHDLD......HCLC;其中01為MODBUSSLAVE的地址,
- 05為寫單個BIT功能碼,HALA為第一個BIT的高低位地址,
- HNLN為BIT數量的高低位,HCLC為CRC校驗碼;
- HDLD為資料高低位;
- *修改日誌:
- *[2006-3-714:06]Ver.1.00
- 開始編寫;
- *[2006-3-714:40]
- 完成;
- *[2006-3-912:44]
- 去掉了等待接收反饋的功能;
- /**/
- /*********************************************************************************/
- intWriteSingleBitToModBusSlave(intcomPort,intDOData[],unsignedshortnum,unsignedshortModDataAddr)
- {
- unsignedcharwriteCmdStr[15];
- unsignedcharModAddr=1;
- unsignedcharModFunction=5;
- unsignedshortModDataState;
- unsignedshortreadCmdLen=6;
- /*intisReadOK=0;
- unsignedshortbyteCount;
- unsignedchartotalStrBack[100];
- unsignedcharModAddrBack=0;
- unsignedcharModFunctionBack=0;
- unsignedshortModDataAddrBack;
- unsignedshortModDataStateBack;
- */
- ModDataState=DOData[num];
- writeCmdStr[0]=ModAddr;
- writeCmdStr[1]=ModFunction;
- writeCmdStr[2]=(unsignedchar)(ModDataAddr>>8&0x00FF);
- writeCmdStr[3]=(unsignedchar)(ModDataAddr&0x00FF);
- if(ModDataState)writeCmdStr[4]=(unsignedchar)(0x00FF);
- elsewriteCmdStr[4]=(unsignedchar)(0x0000);
- writeCmdStr[5]=(unsignedchar)(0x0000);
- writeCmdStr[6]='/0';
- /*ClearCom(comPort);*/
- SendRtuCmdToModBus(comPort,writeCmdStr,readCmdLen);
- return1;
- /*
- isReadOK=ResponseSingleBitFromModBus(comPort,totalStrBack,
- &ModAddrBack,&ModFunctionBack,&ModDataAddrBack,
- &ModDataStateBack,MOD_TIMEOUT,MOD_WAITTIME);
- if(1==isReadOK)
- {
- Print("/tB%u%u%u%u%u%u%u%u",ModAddr,ModAddrBack,ModFunction,ModFunctionBack,ModDataAddr,ModDataAddrBack,ModDataState,ModDataStateBack);
- if((ModAddr==ModAddrBack)&&(ModFunction==ModFunctionBack)&&(ModDataAddr==ModDataAddrBack)
- &&(ModDataState==ModDataStateBack))
- {
- Print("/tretursB1/t");
- return1;
- }
- else
- {
- Print("/tretursB0/t");
- return0;
- }
- }
- else
- {
- /*未收到字串或CRC校驗出錯*/
- return0;
- }
- */
- }
- /*-------------------------------------------------------------------------------*/
- /*********************************************************************************/
- /*函式名稱:WriteBitByBitToModBusSlave()
- *輸入引數:共個引數;
- *輸出引數:共個引數;
- *返回值:成功與否;
- *需儲存的引數:共個引數;
- *功能介紹:
- (1)一個一個地將BIT寫如觸控式螢幕中;
- (2)可能很耗時;
- *修改日誌:
- *[2006-3-714:40]Ver.1.00
- 開始編寫;
- *[2006-3-714:51]
- 完成;
- /**/
- /*********************************************************************************/
- intWriteBitByBitToModBusSlave(intcomPort,intDOData[],intdataOld[],unsignedshortnum,unsignedshortModDataAddr)
- {
- inti;
- for(i=0;i<num;i++)
- {
- if(DOData[i]!=dataOld[i])
- {
- DelayMs(1);
- WriteSingleBitToModBusSlave(comPort,DOData,i,ModDataAddr+i);
- }
- }
- return1;
- }
- /*-------------------------------------------------------------------------------*/
- /*********************************************************************************/
- /*函式名稱:WriteAllBitByBitToModBusSlave()
- *輸入引數:共個引數;
- *輸出引數:共個引數;
- *返回值:成功與否;
- *需儲存的引數:共個引數;
- *功能介紹:
- (1)一個一個地將BIT寫如觸控式螢幕中;
- (2)可能很耗時;
- *修改日誌:
- *[2006-3-912:52]Ver.1.00
- 開始編寫;
- *[2006-3-912:52]
- 完成;
- /**/
- /*********************************************************************************/
- intWriteAllBitByBitToModBusSlave(intcomPort,intDOData[],unsignedshortnum,unsignedshortModDataAddr)
- {
- inti;
- for(i=0;i<num;i++)
- {
- DelayMs(1);
- WriteSingleBitToModBusSlave(comPort,DOData,i,ModDataAddr+i);
- }
- return1;
- }
- /*-------------------------------------------------------------------------------*/
- /*********************************************************************************/
- /*函式名稱:ListenToTouch()
- *輸入引數:共個引數;
- *輸出引數:共個引數;
- *返回值:無;
- *需儲存的引數:共個引數;
- *功能介紹:
- (1)監控Touch觸控式螢幕;
- (2)init的工作給WriteMultipleWordToModBusSlave是否成功。
- (3)必須要DelayMs(50)才能使一下個response正確。
- *修改日誌:
- *[2006-3-117:19]Ver.1.00
- 開始編寫;
- *[2006-3-117:33]
- 完成;
- *[2006-3-914:10]
- 測試通過;
- /**/
- /*********************************************************************************/
- voidListenToTouch(intcomPort,intbuttonData[],floatparameterData[],intDOData[],floatAIData[])
- {
- intkind=1;/*adjust*/
- unsignedshortparameterNum=PARAMETER_DATA_NUM;
- unsignedshortparameterAddr=PARAMETER_MODBUS_ADDR_1;/*adjust*/
- unsignedshortbuttonNum=BUTTON_DATA_NUM;
- unsignedshortbuttonAddr=BUTTON_MODBUS_ADDR_1;/*adjust*/
- unsignedshortDONum=DIGITAL_OUT_DATA_NUM;
- unsignedshortDOAddr=DO_MODBUS_ADDR_1;/*adjust*/
- unsignedshortAINum=ANALOG_IN_DATA_NUM;
- unsignedshortAIAddr=AI_MODBUS_ADDR_1;/*adjust*/
- intbuttonNVRAMAddr=BUTTON_ADDR_1;/*adjust*/
- intparameterEEPROMAddr=EEPROM_BLOCK;/*adjust*/
- staticunsignedcharoldModWordBack[MOD_STR_MAX_LEN]={'/0'};/*以前的str*/
- staticunsignedcharoldModBitBack[MOD_STR_MAX_LEN]={'/0'};
- staticintresponseFromTouch_init=0;/*首次初始化*/
- /*staticintresponseFromTouch_P=0;
- staticintresponseFromTouch_Btn=0;
- staticintoldButtonData[30]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
- staticintoldDOData[30]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
- */
- intresponse;/*test*/
- /*Print("/n/n");*/
- if(0==responseFromTouch_init)/*第一次寫入Touch_Btn失敗重新寫*/
- {
- /*responseFromTouch_Btn=WriteMultipleBitToModBusSlave(comPort,buttonData,buttonNum,buttonAddr);
- */response=WriteAllBitByBitToModBusSlave(comPort,buttonData,buttonNum,buttonAddr);
- /*bitmcpy(oldButtonData,buttonData,buttonNum);*/
- }
- else
- {
- /*讀取按鈕值*/
- response=ReadBitFromModBusSlave(kind,comPort,buttonData,buttonNum,buttonAddr,buttonNVRAMAddr,oldModBitBack);
- }
- DelayMs(50);
- /*Print("rb%d",response);*/
- if(0==responseFromTouch_init)/*第一次寫入Touch_P失敗重新寫*/
- {
- responseFromTouch_init=WriteMultipleWordToModBusSlave(comPort,parameterData,parameterNum,parameterAddr);
- response=responseFromTouch_init;
- /*responseFromTouch_P=1;/*test*/
- }
- else
- {
- /*讀取設定引數*/
- response=ReadWordFromModBusSlave(kind,comPort,parameterData,parameterNum,parameterAddr,parameterEEPROMAddr,oldModWordBack);
- }
- /*Print("p%d",responseFromTouch_P);
- Print("rp%d",response);*/
- DelayMs(2);
- /*寫入開關量*/
- /*response=WriteMultipleBitToModBusSlave(comPort,DOData,DONum,DOAddr);
- */
- response=WriteAllBitByBitToModBusSlave(comPort,DOData,DONum,DOAddr);
- /*bitmcpy(oldDOData,DOData,DONum);*/
- /*Print("d%d",response);*/
- DelayMs(40);
- /*寫入取樣值*/
- response=WriteMultipleWordToModBusSlave(comPort,AIData,AINum,AIAddr);
- /*Print("a%d",response);*/
- if(response);
- }
- /*-------------------------------------------------------------------------------*/