5
5
using System . Globalization ;
6
6
using System . IO ;
7
7
using System . IO . Ports ;
8
- using System . Net ;
9
8
using System . Text ;
10
9
11
10
namespace PanasonicPLCriver
@@ -43,7 +42,7 @@ public PanasonicSerialReader(IDataServer server, short id, string name, string i
43
42
_server = server ;
44
43
_id = id ;
45
44
//spare1 = {COM3,9600,Odd,8,One}
46
- _serialPort = new SerialPort ( "COM2" , 57600 , Parity . Odd , 8 , StopBits . One ) ;
45
+ _serialPort = new SerialPort ( "COM2" , 57600 , Parity . Odd , 8 , StopBits . One ) ;
47
46
_devId = byte . Parse ( spare2 ) ;
48
47
}
49
48
public bool IsClosed
@@ -299,7 +298,7 @@ private string CreateRDCmd(int start, ushort size, out string respBeginStr)
299
298
/// <param name="values">写入的值</param>
300
299
/// <param name="respBeginStr">响应的开始帧</param>
301
300
/// <returns></returns>
302
- public string CreateWDCmd ( int start , short [ ] values , out string respBeginStr )
301
+ public string CreateWDCmd ( int start , short [ ] values , out string respBeginStr )
303
302
{
304
303
string dataStr = string . Empty ;
305
304
int size = values . Length ;
@@ -330,17 +329,18 @@ public byte[] ReadBytes(DeviceAddress startAddress, ushort size)
330
329
{
331
330
if ( ! isBool ( startAddress . Area ) ) //读寄存器的情况
332
331
{
333
- cmd = CreateRDCmd ( startAddress . Start , size , out respBeginStr ) ;
332
+ cmd = CreateRDCmd ( startAddress . Start , size , out respBeginStr ) ;
334
333
}
335
334
else //读触点的情况
336
335
{
337
- cmd = CreateRCCCmd ( startAddress . Start , size , startAddress . Area , out respBeginStr ) ;
336
+ cmd = CreateRCCCmd ( startAddress . Start , size , startAddress . Area , out respBeginStr ) ;
338
337
}
339
338
return WriteSyncData ( respBeginStr , cmd ) ;
340
339
}
341
340
catch ( Exception e )
342
341
{
343
- OnClose ? . Invoke ( this , new ShutdownRequestEventArgs ( e . Message ) ) ;
342
+ if ( OnClose != null )
343
+ OnClose ( this , new ShutdownRequestEventArgs ( e . Message ) ) ;
344
344
return null ;
345
345
}
346
346
}
@@ -350,7 +350,7 @@ private byte[] WriteSyncData(string respBeginStr, string cmd)
350
350
byte [ ] writeData = Encoding . Default . GetBytes ( cmd ) ;
351
351
string recv = string . Empty ;
352
352
lock ( syncLock ) //这里加锁 防止一个刚写完还没全读来 另一就去写
353
- //但这就会造成锁死的情况 比如循环读的时候 触发去写 这时候必须要等读完才能去写
353
+ //但这就会造成锁死的情况 比如循环读的时候 触发去写 这时候必须要等读完才能去写
354
354
{
355
355
_serialPort . Write ( writeData , 0 , writeData . Length ) ;
356
356
try
@@ -359,29 +359,32 @@ private byte[] WriteSyncData(string respBeginStr, string cmd)
359
359
}
360
360
catch ( Exception )
361
361
{
362
- OnClose ? . Invoke ( this , new ShutdownRequestEventArgs ( "读取超时" ) ) ;
362
+ if ( OnClose != null )
363
+ OnClose ( this , new ShutdownRequestEventArgs ( "读取超时" ) ) ;
363
364
return null ;
364
- }
365
+ }
365
366
}
366
367
if ( recv . Substring ( 3 , 1 ) == "!" ) //返回为错误代码
367
368
{
368
369
string err = recv . Substring ( 4 , 2 ) ;
369
- OnClose ? . Invoke ( this , new ShutdownRequestEventArgs ( daveStrerror ( err ) ) ) ;
370
+ if ( OnClose != null )
371
+ OnClose ( this , new ShutdownRequestEventArgs ( daveStrerror ( err ) ) ) ;
370
372
return null ;
371
373
}
372
374
string needXorStr = recv . Substring ( 0 , recv . Length - 2 ) ; //需要进行xor校验的字符串
373
375
string recvCheck = recv . Substring ( recv . Length - 2 , 2 ) ; //接收的xor字符串
374
376
string checkStr = Utility . XorCheck ( needXorStr ) ;
375
377
if ( checkStr != recvCheck )
376
378
{
377
- OnClose ? . Invoke ( this , new ShutdownRequestEventArgs ( "校验失败" ) ) ;
379
+ if ( OnClose != null )
380
+ OnClose ( this , new ShutdownRequestEventArgs ( "校验失败" ) ) ;
378
381
return null ;
379
382
}
380
383
else
381
384
{
382
385
if ( recv . Substring ( 4 , 1 ) == "W" ) //如果是写入命令
383
386
{
384
- return new byte [ 0 ] ;
387
+ return new byte [ 0 ] ;
385
388
}
386
389
string dataStr = Utility . Pinchstring ( recv , respBeginStr , checkStr ) ;
387
390
return Utility . HexToBytes ( dataStr ) ;
@@ -391,7 +394,7 @@ string daveStrerror(string code)
391
394
{
392
395
switch ( code )
393
396
{
394
- case "20" : return "未定义" ;
397
+ case "20" : return "未定义" ;
395
398
case "21" : return "远程单元无法被正确识别,或者发生了数据错误." ;
396
399
case "22" : return "用于远程单元的接收缓冲区已满." ;
397
400
case "23" : return "远程单元编号(01 至16)设置与本地单元重复." ;
@@ -430,7 +433,7 @@ public ItemData<byte> ReadByte(DeviceAddress address)
430
433
}
431
434
public ItemData < float > ReadFloat ( DeviceAddress address )
432
435
{
433
- throw new NotImplementedException ( ) ;
436
+ throw new NotImplementedException ( ) ;
434
437
}
435
438
public ItemData < short > ReadInt16 ( DeviceAddress address )
436
439
{
@@ -475,10 +478,10 @@ public bool RemoveGroup(IGroup group)
475
478
476
479
public int WriteBit ( DeviceAddress address , bool bit )
477
480
{
478
- string respBeginStr = string . Empty ;
479
- string cmd = CreateWCSCmd ( address . Start , address . Bit , address . Area , bit , out respBeginStr ) ;
480
- WriteSyncData ( respBeginStr , cmd ) ;
481
- return 0 ;
481
+ string respBeginStr = string . Empty ;
482
+ string cmd = CreateWCSCmd ( address . Start , address . Bit , address . Area , bit , out respBeginStr ) ;
483
+ WriteSyncData ( respBeginStr , cmd ) ;
484
+ return 0 ;
482
485
}
483
486
484
487
public int WriteBits ( DeviceAddress address , byte bits )
@@ -499,7 +502,7 @@ public int WriteFloat(DeviceAddress address, float value)
499
502
public int WriteInt16 ( DeviceAddress address , short value )
500
503
{
501
504
string respBeginStr = string . Empty ;
502
- string cmd = CreateWDCmd ( address . Start , new short [ 1 ] { value } , out respBeginStr ) ;
505
+ string cmd = CreateWDCmd ( address . Start , new short [ 1 ] { value } , out respBeginStr ) ;
503
506
WriteSyncData ( respBeginStr , cmd ) ;
504
507
return 0 ;
505
508
}
@@ -609,12 +612,12 @@ public struct Panasonic
609
612
/// </summary>
610
613
public const string WDCmd = "WD" ;
611
614
612
- public const byte Xarea = 0 ; //外部输入
613
- public const byte Yarea = 1 ; //外部输出
614
- public const byte Rarea = 2 ; //内部继电器
615
- public const byte Tarea = 3 ; //定时器
616
- public const byte Carea = 4 ; //计数器 不支持
617
- public const byte Larea = 5 ; //链接继电器 不支持
615
+ public const byte Xarea = 0 ; //外部输入
616
+ public const byte Yarea = 1 ; //外部输出
617
+ public const byte Rarea = 2 ; //内部继电器
618
+ public const byte Tarea = 3 ; //定时器
619
+ public const byte Carea = 4 ; //计数器 不支持
620
+ public const byte Larea = 5 ; //链接继电器 不支持
618
621
619
622
public const byte DTarea = 6 ; //数据寄存器 DT
620
623
public const byte LDarea = 7 ; //链接寄存器 LD 不支持
0 commit comments