@@ -365,175 +365,216 @@ public void Init()
365
365
private void ReciveData ( )
366
366
{
367
367
if ( ! _active || _plcReader . tcpRecive == null ) return ;
368
- List < HistoryData > historys = null ;
368
+ List < HistoryData > historys = new List < HistoryData > ( ) ; ;
369
369
byte [ ] bytes = new byte [ ushort . MaxValue ] ;
370
- byte [ ] temp = new byte [ _tcpRecive . ReceiveBufferSize ] ;
370
+ byte [ ] temp = new byte [ ushort . MaxValue ] ;
371
371
Storage value = Storage . Empty ;
372
- int result = 0 ;
373
372
int start = 0 ;
374
373
SocketError error ;
374
+ int result = 0 ;
375
375
do
376
376
{
377
- if ( ! _tcpRecive . Connected ) return ;
378
- result = _tcpRecive . Receive ( bytes , 0 , bytes . Length , SocketFlags . None , out error ) ;
379
- if ( error == SocketError . Success )
377
+ if ( ! _tcpRecive . Connected || ! _active ) return ;
378
+ try
380
379
{
381
- if ( DataChange != null )
382
- historys = new List < HistoryData > ( ) ;
383
- //DateTime time = DateTime.Now;//当前时间戳
384
- if ( start != 0 && temp [ 0 ] == FCTCOMMAND . fctHead )
380
+ result = _tcpRecive . Receive ( bytes , 0 , bytes . Length , SocketFlags . None , out error ) ;
381
+ if ( error == SocketError . Success )
385
382
{
386
- int j = 3 ;
387
- if ( start < 0 )
383
+ if ( start != 0 && temp [ 0 ] == FCTCOMMAND . fctHead )
388
384
{
389
- Array . Copy ( bytes , 0 , temp , - start , 5 + start ) ;
390
- }
391
- short tc = BitConverter . ToInt16 ( temp , j ) ; //总字节数
392
- if ( start < 0 )
393
- start += tc ;
394
- Array . Copy ( bytes , 0 , temp , tc - start , start ) ;
395
- j += 2 ;
396
- while ( j < tc )
397
- {
398
- short id = BitConverter . ToInt16 ( temp , j ) ; //标签ID、数据长度、数据值(T,L,V)
385
+ int j = 3 ;
386
+ if ( start < 0 )
387
+ {
388
+ Array . Copy ( bytes , 0 , temp , - start , 5 + start ) ;
389
+ }
390
+ short tc = BitConverter . ToInt16 ( temp , j ) ; //总字节数
391
+ if ( start < 0 )
392
+ start += tc ;
393
+ Array . Copy ( bytes , 0 , temp , tc - start , start ) ;
399
394
j += 2 ;
400
- byte length = temp [ j ++ ] ;
401
- ITag tag ;
402
- if ( _items . TryGetValue ( id , out tag ) )
395
+ while ( j < tc )
403
396
{
404
- //数据类型
405
- switch ( tag . Address . VarType )
397
+ short id = BitConverter . ToInt16 ( temp , j ) ; //标签ID、数据长度、数据值(T,L,V)
398
+ j += 2 ;
399
+ byte length = temp [ j ++ ] ;
400
+ ITag tag ;
401
+ if ( _items . TryGetValue ( id , out tag ) )
406
402
{
407
- case DataType . BOOL :
408
- value . Boolean = BitConverter . ToBoolean ( temp , j ) ;
409
- break ;
410
- case DataType . BYTE :
411
- value . Byte = temp [ j ] ;
412
- break ;
413
- case DataType . WORD :
414
- value . Word = BitConverter . ToUInt16 ( temp , j ) ; //需测试
415
- break ;
416
- case DataType . SHORT :
417
- value . Int16 = BitConverter . ToInt16 ( temp , j ) ; //需测试
418
- break ;
419
- case DataType . DWORD :
420
- value . DWord = BitConverter . ToUInt32 ( temp , j ) ; //需测试
421
- break ;
422
- case DataType . INT :
423
- value . Int32 = BitConverter . ToInt32 ( temp , j ) ; //需测试
424
- break ;
425
- case DataType . FLOAT :
426
- value . Single = BitConverter . ToSingle ( temp , j ) ;
427
- break ;
428
- case DataType . STR :
429
- StringTag strTag = tag as StringTag ;
430
- if ( strTag != null )
431
- {
432
- strTag . String = Encoding . ASCII . GetString ( temp , j , length ) . Trim ( ( char ) 0 ) ;
433
- }
434
- break ;
435
- default :
436
- break ;
403
+ //数据类型
404
+ switch ( tag . Address . VarType )
405
+ {
406
+ case DataType . BOOL :
407
+ value . Boolean = BitConverter . ToBoolean ( temp , j ) ;
408
+ break ;
409
+ case DataType . BYTE :
410
+ value . Byte = temp [ j ] ;
411
+ break ;
412
+ case DataType . WORD :
413
+ value . Word = BitConverter . ToUInt16 ( temp , j ) ; //需测试
414
+ break ;
415
+ case DataType . SHORT :
416
+ value . Int16 = BitConverter . ToInt16 ( temp , j ) ; //需测试
417
+ break ;
418
+ case DataType . DWORD :
419
+ value . DWord = BitConverter . ToUInt32 ( temp , j ) ; //需测试
420
+ break ;
421
+ case DataType . INT :
422
+ value . Int32 = BitConverter . ToInt32 ( temp , j ) ; //需测试
423
+ break ;
424
+ case DataType . FLOAT :
425
+ value . Single = BitConverter . ToSingle ( temp , j ) ;
426
+ break ;
427
+ case DataType . STR :
428
+ StringTag strTag = tag as StringTag ;
429
+ if ( strTag != null )
430
+ {
431
+ strTag . String = Encoding . ASCII . GetString ( temp , j , length ) . Trim ( ( char ) 0 ) ;
432
+ }
433
+ break ;
434
+ default :
435
+ break ;
436
+ }
437
+ j += length ;
438
+ try
439
+ {
440
+ DateTime time = DateTime . FromFileTime ( BitConverter . ToInt64 ( temp , j ) ) ;
441
+ //tag.Update(value, time, QUALITIES.QUALITY_GOOD);
442
+ //if (historys != null)
443
+ historys . Add ( new HistoryData ( id , QUALITIES . QUALITY_GOOD , value , time ) ) ;
444
+ }
445
+ catch ( Exception exp )
446
+ {
447
+ }
448
+ j += 8 ;
437
449
}
438
- j += length ;
439
- DateTime time = DateTime . FromFileTime ( BitConverter . ToInt64 ( temp , j ) ) ;
440
- j += 8 ;
441
- tag . Update ( value , time , QUALITIES . QUALITY_GOOD ) ;
442
- if ( historys != null )
443
- historys . Add ( new HistoryData ( id , QUALITIES . QUALITY_GOOD , value , time ) ) ;
450
+ else
451
+ j += length + 8 ;
444
452
}
445
- else
446
- j += length + 8 ;
447
453
}
448
- }
449
- byte head = bytes [ start ] ;
450
- int count = start ;
451
- while ( head == FCTCOMMAND . fctHead && result > count )
452
- {
453
- if ( count + 5 > bytes . Length )
454
+ byte head = bytes [ start ] ;
455
+ int count = start ;
456
+ while ( head == FCTCOMMAND . fctHead && result > count )
454
457
{
455
- start = count - bytes . Length ;
456
- Array . Copy ( bytes , count , temp , 0 , - start ) ;
457
- break ;
458
- }
459
- int j = count + 3 ;
460
- short tc = BitConverter . ToInt16 ( bytes , j ) ; //总标签数
461
- count += tc ;
462
- if ( count >= bytes . Length )
463
- {
464
- start = count - bytes . Length ;
465
- Array . Copy ( bytes , count - tc , temp , 0 , tc - start ) ;
466
- break ;
467
- }
468
- else start = 0 ;
469
- j += 2 ;
470
- while ( j < count )
471
- {
472
- short id = BitConverter . ToInt16 ( bytes , j ) ; //标签ID、数据长度、数据值(T,L,V)
458
+ if ( count + 5 > bytes . Length )
459
+ {
460
+ start = count - bytes . Length ;
461
+ Array . Copy ( bytes , count , temp , 0 , - start ) ;
462
+ break ;
463
+ }
464
+ int j = count + 3 ;
465
+ short tc = BitConverter . ToInt16 ( bytes , j ) ; //总标签数
466
+ count += tc ;
467
+ if ( count >= bytes . Length )
468
+ {
469
+ start = count - bytes . Length ;
470
+ Array . Copy ( bytes , count - tc , temp , 0 , tc - start ) ;
471
+ break ;
472
+ }
473
+ else start = 0 ;
473
474
j += 2 ;
474
- byte length = bytes [ j ++ ] ;
475
- ITag tag ;
476
- if ( _items . TryGetValue ( id , out tag ) )
475
+ while ( j < count )
477
476
{
478
- //数据类型
479
- switch ( tag . Address . VarType )
477
+ short id = BitConverter . ToInt16 ( bytes , j ) ; //标签ID、数据长度、数据值(T,L,V)
478
+ j += 2 ;
479
+ byte length = bytes [ j ++ ] ;
480
+ ITag tag ;
481
+ if ( _items . TryGetValue ( id , out tag ) )
480
482
{
481
- case DataType . BOOL :
482
- value . Boolean = BitConverter . ToBoolean ( bytes , j ) ;
483
- break ;
484
- case DataType . BYTE :
485
- value . Byte = bytes [ j ] ;
486
- break ;
487
- case DataType . WORD :
488
- value . Word = BitConverter . ToUInt16 ( bytes , j ) ; //需测试
489
- break ;
490
- case DataType . SHORT :
491
- value . Int16 = BitConverter . ToInt16 ( bytes , j ) ; //需测试
492
- break ;
493
- case DataType . DWORD :
494
- value . DWord = BitConverter . ToUInt32 ( bytes , j ) ; //需测试
495
- break ;
496
- case DataType . INT :
497
- value . Int32 = BitConverter . ToInt32 ( bytes , j ) ; //需测试
498
- break ;
499
- case DataType . FLOAT :
500
- value . Single = BitConverter . ToSingle ( bytes , j ) ;
501
- break ;
502
- case DataType . STR :
503
- StringTag strTag = tag as StringTag ;
504
- if ( strTag != null )
505
- {
506
- strTag . String = Encoding . ASCII . GetString ( bytes , j , length ) . Trim ( ( char ) 0 ) ;
507
- }
508
- break ;
509
- default :
510
- break ;
483
+ //数据类型
484
+ switch ( tag . Address . VarType )
485
+ {
486
+ case DataType . BOOL :
487
+ value . Boolean = BitConverter . ToBoolean ( bytes , j ) ;
488
+ break ;
489
+ case DataType . BYTE :
490
+ value . Byte = bytes [ j ] ;
491
+ break ;
492
+ case DataType . WORD :
493
+ value . Word = BitConverter . ToUInt16 ( bytes , j ) ; //需测试
494
+ break ;
495
+ case DataType . SHORT :
496
+ value . Int16 = BitConverter . ToInt16 ( bytes , j ) ; //需测试
497
+ break ;
498
+ case DataType . DWORD :
499
+ value . DWord = BitConverter . ToUInt32 ( bytes , j ) ; //需测试
500
+ break ;
501
+ case DataType . INT :
502
+ value . Int32 = BitConverter . ToInt32 ( bytes , j ) ; //需测试
503
+ break ;
504
+ case DataType . FLOAT :
505
+ value . Single = BitConverter . ToSingle ( bytes , j ) ;
506
+ break ;
507
+ case DataType . STR :
508
+ StringTag strTag = tag as StringTag ;
509
+ if ( strTag != null )
510
+ {
511
+ strTag . String = Encoding . ASCII . GetString ( bytes , j , length ) . Trim ( ( char ) 0 ) ;
512
+ }
513
+ break ;
514
+ default :
515
+ break ;
516
+ }
517
+ j += length ;
518
+ try
519
+ {
520
+ DateTime time = DateTime . FromFileTime ( BitConverter . ToInt64 ( bytes , j ) ) ;
521
+ //tag.Update(value, time, QUALITIES.QUALITY_GOOD);
522
+ //if (historys != null)
523
+ historys . Add ( new HistoryData ( id , QUALITIES . QUALITY_GOOD , value , time ) ) ;
524
+ }
525
+ catch ( Exception exp )
526
+ {
527
+ }
528
+ j += 8 ;
511
529
}
512
- j += length ;
513
- DateTime time = DateTime . FromFileTime ( BitConverter . ToInt64 ( bytes , j ) ) ;
514
- j += 8 ;
515
- tag . Update ( value , time , QUALITIES . QUALITY_GOOD ) ;
516
- if ( historys != null )
517
- historys . Add ( new HistoryData ( id , QUALITIES . QUALITY_GOOD , value , time ) ) ;
530
+ else
531
+ j += length + 8 ;
518
532
}
519
- else
520
- j += length + 8 ;
533
+ head = bytes [ count ] ;
521
534
}
522
- head = bytes [ count ] ;
523
- }
524
- if ( DataChange != null && historys . Count > 0 )
525
- DataChange ( this , new DataChangeEventArgs ( 1 , historys ) ) ;
535
+ ThreadPool . UnsafeQueueUserWorkItem ( new WaitCallback ( this . OnRecieve ) , historys ) ;
536
+ //if (DataChange != null && historys.Count > 0)
537
+ // DataChange(this, new DataChangeEventArgs(1, historys));
526
538
539
+ }
540
+ else if ( error == SocketError . ConnectionReset || error == SocketError . Interrupted
541
+ || error == SocketError . HostDown || error == SocketError . NetworkDown || error == SocketError . Shutdown )
542
+ {
543
+ _tcpRecive . Dispose ( ) ;
544
+ return ;
545
+ }
527
546
}
528
- else if ( error == SocketError . ConnectionReset || error == SocketError . Interrupted
529
- || error == SocketError . HostDown || error == SocketError . NetworkDown || error == SocketError . Shutdown )
547
+ catch ( Exception )
530
548
{
531
549
_tcpRecive . Dispose ( ) ;
532
- _active = false ;
533
550
return ;
534
551
}
535
552
}
536
553
while ( result > 0 ) ;
554
+ try
555
+ {
556
+ _tcpRecive . Dispose ( ) ;
557
+ }
558
+ catch ( Exception )
559
+ {
560
+ }
561
+ }
562
+
563
+ private void OnRecieve ( object stateInfo )
564
+ {
565
+ var historys = stateInfo as List < HistoryData > ;
566
+ if ( historys == null ) return ;
567
+ if ( DataChange != null && historys . Count > 0 )
568
+ DataChange ( this , new DataChangeEventArgs ( 1 , historys ) ) ;
569
+ for ( int i = 0 ; i < historys . Count ; i ++ )
570
+ {
571
+ var data = historys [ i ] ;
572
+ ITag tag ;
573
+ if ( _items . TryGetValue ( data . ID , out tag ) )
574
+ {
575
+ tag . Update ( data . Value , data . TimeStamp , data . Quality ) ;
576
+ }
577
+ }
537
578
}
538
579
539
580
public void OnUpdate ( object stateInfo )
0 commit comments