📄 modbus.lst
字号:
198
199 //强制单个线圈
200 void forceSingleCoil(void)
201 {
202 1 uint8 addr;
203 1 uint8 tempAddr;
204 1 uint16 tempData;
205 1 uint8 onOff;
206 1 uint8 i;
207 1
208 1 //addr = (receBuf[2]<<8) + receBuf[3];
209 1 //tempAddr = addr & 0xfff;
210 1 addr = receBuf[3];
211 1 tempAddr = addr;
212 1
213 1 //onOff = (receBuf[4]<<8) + receBuf[5];
214 1 onOff = receBuf[4];
215 1
216 1 //if(onOff == 0xff00)
217 1 if(onOff == 0xff)
218 1 { //设为ON
219 2 tempData = 1;
220 2 }
221 1 //else if(onOff == 0x0000)
222 1 else if(onOff == 0x00)
223 1 { //设为OFF
224 2 tempData = 0;
225 2 }
226 1
227 1 setCoilVal(tempAddr,tempData);
228 1
229 1 for(i=0;i<receCount;i++)
230 1 {
231 2 sendBuf[i] = receBuf[i];
232 2 }
233 1 sendCount = receCount;
234 1 beginSend();
235 1 }//void forceSingleCoil(void)
236
237
238 //设置多个寄存器
239 void presetMultipleRegisters(void)
240 {
241 1 uint8 addr;
C51 COMPILER V8.05a MODBUS 08/19/2008 15:20:43 PAGE 5
242 1 uint8 tempAddr;
243 1 uint8 byteCount;
244 1 uint8 setCount;
245 1 uint16 crcData;
246 1 uint16 tempData;
247 1 uint8 i;
248 1
249 1 //addr = (receBuf[2]<<8) + receBuf[3];
250 1 //tempAddr = addr & 0xfff;
251 1 addr = receBuf[3];
252 1 tempAddr = addr & 0xff;
253 1
254 1 //setCount = (receBuf[4]<<8) + receBuf[5];
255 1 setCount = receBuf[5];
256 1 byteCount = receBuf[6];
257 1
258 1 for(i=0;i<setCount;i++,tempAddr++)
259 1 {
260 2 tempData = (receBuf[i*2+7]<<8) + receBuf[i*2+8];
261 2
262 2 setRegisterVal(tempAddr,tempData);
263 2 }
264 1
265 1 sendBuf[0] = localAddr;
266 1 sendBuf[1] = receBuf[1];
267 1 sendBuf[2] = addr >> 8;
268 1 sendBuf[3] = addr & 0xff;
269 1 sendBuf[4] = setCount >> 8;
270 1 sendBuf[5] = setCount & 0xff;
271 1 crcData = crc16(sendBuf,6);
272 1 sendBuf[6] = crcData >> 8;
273 1 sendBuf[7] = crcData & 0xff;
274 1 sendCount = 8;
275 1 beginSend();
276 1 }//void presetMultipleRegisters(void)
277
278
279
280 //检查uart0数据
281 void checkComm0Modbus(void)
282 {
283 1 uint16 crcData;
284 1 uint16 tempData;
285 1
286 1 if(receCount > 4)
287 1 {
288 2 switch(receBuf[1])
289 2 {
290 3 case 1://读取线圈状态(读取点 16位以内)
291 3 case 3://读取保持寄存器(一个或多个)
292 3 case 5://强制单个线圈
293 3 case 6://设置单个寄存器
294 3 if(receCount >= 8)
295 3 {//接收完成一组数据
296 4 //应该关闭接收中断
297 4
298 4 if(receBuf[0]==localAddr && checkoutError==0)
299 4 {
300 5 crcData = crc16(receBuf,6);
301 5 if(crcData == receBuf[7]+(receBuf[6]<<8))//校验正确
302 5 {
303 6 if(receBuf[1] == 1)
C51 COMPILER V8.05a MODBUS 08/19/2008 15:20:43 PAGE 6
304 6 {
305 7 readCoil();//读取线圈状态(读取点 16位以内)
306 7 }
307 6 else if(receBuf[1] == 3)
308 6 {
309 7 readRegisters();//读取保持寄存器(一个或多个)
310 7 }
311 6 else if(receBuf[1] == 5)
312 6 {
313 7 forceSingleCoil();//强制单个线圈
314 7 }
315 6 else if(receBuf[1] == 6)
316 6 {
317 7 presetMultipleRegisters();
318 7 }
319 6
320 6 }
321 5 }
322 4 receCount = 0;
323 4 checkoutError = 0;
324 4 }
325 3 break;
326 3
327 3 /*case 15://设置多个线圈
328 3 tempData = receBuf[6];
329 3 tempData += 9; //数据个数
330 3 if(receCount >= tempData)
331 3 {
332 3 if(receBuf[0]==localAddr && checkoutError==0)
333 3 {
334 3 crcData = crc16(receBuf,tempData-2);
335 3 if(crcData == (receBuf[tempData-2]<<8)+ receBuf[tempData-1])
336 3 {
337 3 //forceMultipleCoils();
338 3 }
339 3 }
340 3 receCount = 0;
341 3 checkoutError = 0;
342 3 }
343 3 break; */
344 3
345 3 case 16://设置多个寄存器
346 3 tempData = (receBuf[4]<<8) + receBuf[5];
347 3 tempData = tempData * 2; //数据个数
348 3 tempData += 9;
349 3 if(receCount >= tempData)
350 3 {
351 4 if(receBuf[0]==localAddr && checkoutError==0)
352 4 {
353 5 crcData = crc16(receBuf,tempData-2);
354 5 if(crcData == (receBuf[tempData-2]<<8)+ receBuf[tempData-1])
355 5 {
356 6 presetMultipleRegisters();
357 6 }
358 5 }
359 4 receCount = 0;
360 4 checkoutError = 0;
361 4 }
362 3 break;
363 3
364 3 default:
365 3 break;
C51 COMPILER V8.05a MODBUS 08/19/2008 15:20:43 PAGE 7
366 3 }
367 2 }
368 1 }//void checkComm0(void)
369
370 //取线圈状态 返回0表示成功
371 uint16 getCoilVal(uint16 addr,uint16 *tempData)
372 {
373 1 uint16 result = 0;
374 1 uint16 tempAddr;
375 1
376 1 tempAddr = addr & 0xfff;
377 1 //只取低8位地址
378 1 switch(tempAddr & 0xff)
379 1 {
380 2 case 0:
381 2 *tempData = led0;
382 2 break;
383 2 case 1:
384 2 *tempData = led1;
385 2 break;
386 2 case 2:
387 2 *tempData = led2;
388 2 break;
389 2 case 3:
390 2 *tempData = led3;
391 2 break;
392 2 case 4:
393 2 *tempData = led4;
394 2 break;
395 2 case 5:
396 2 *tempData = led5;
397 2 break;
398 2 case 6:
399 2 *tempData = led6;
400 2 break;
401 2 case 7:
402 2 *tempData = led7;
403 2 break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -