Skip to content

Commit 6c228c5

Browse files
committed
update panasonic driver
1 parent e718f35 commit 6c228c5

File tree

2 files changed

+28
-25
lines changed

2 files changed

+28
-25
lines changed

SCADA/Program/PanasonicDriver/PanasonicSerialReader.cs

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.Globalization;
66
using System.IO;
77
using System.IO.Ports;
8-
using System.Net;
98
using System.Text;
109

1110
namespace PanasonicPLCriver
@@ -43,7 +42,7 @@ public PanasonicSerialReader(IDataServer server, short id, string name, string i
4342
_server = server;
4443
_id = id;
4544
//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);
4746
_devId = byte.Parse(spare2);
4847
}
4948
public bool IsClosed
@@ -299,7 +298,7 @@ private string CreateRDCmd(int start, ushort size, out string respBeginStr)
299298
/// <param name="values">写入的值</param>
300299
/// <param name="respBeginStr">响应的开始帧</param>
301300
/// <returns></returns>
302-
public string CreateWDCmd(int start, short[] values, out string respBeginStr)
301+
public string CreateWDCmd(int start, short[] values, out string respBeginStr)
303302
{
304303
string dataStr = string.Empty;
305304
int size = values.Length;
@@ -330,17 +329,18 @@ public byte[] ReadBytes(DeviceAddress startAddress, ushort size)
330329
{
331330
if (!isBool(startAddress.Area))//读寄存器的情况
332331
{
333-
cmd = CreateRDCmd(startAddress.Start,size,out respBeginStr);
332+
cmd = CreateRDCmd(startAddress.Start, size, out respBeginStr);
334333
}
335334
else //读触点的情况
336335
{
337-
cmd = CreateRCCCmd(startAddress.Start, size, startAddress.Area, out respBeginStr);
336+
cmd = CreateRCCCmd(startAddress.Start, size, startAddress.Area, out respBeginStr);
338337
}
339338
return WriteSyncData(respBeginStr, cmd);
340339
}
341340
catch (Exception e)
342341
{
343-
OnClose?.Invoke(this, new ShutdownRequestEventArgs(e.Message));
342+
if (OnClose != null)
343+
OnClose(this, new ShutdownRequestEventArgs(e.Message));
344344
return null;
345345
}
346346
}
@@ -350,7 +350,7 @@ private byte[] WriteSyncData(string respBeginStr, string cmd)
350350
byte[] writeData = Encoding.Default.GetBytes(cmd);
351351
string recv = string.Empty;
352352
lock (syncLock)//这里加锁 防止一个刚写完还没全读来 另一就去写
353-
//但这就会造成锁死的情况 比如循环读的时候 触发去写 这时候必须要等读完才能去写
353+
//但这就会造成锁死的情况 比如循环读的时候 触发去写 这时候必须要等读完才能去写
354354
{
355355
_serialPort.Write(writeData, 0, writeData.Length);
356356
try
@@ -359,29 +359,32 @@ private byte[] WriteSyncData(string respBeginStr, string cmd)
359359
}
360360
catch (Exception)
361361
{
362-
OnClose?.Invoke(this, new ShutdownRequestEventArgs("读取超时"));
362+
if (OnClose != null)
363+
OnClose(this, new ShutdownRequestEventArgs("读取超时"));
363364
return null;
364-
}
365+
}
365366
}
366367
if (recv.Substring(3, 1) == "!")//返回为错误代码
367368
{
368369
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)));
370372
return null;
371373
}
372374
string needXorStr = recv.Substring(0, recv.Length - 2);//需要进行xor校验的字符串
373375
string recvCheck = recv.Substring(recv.Length - 2, 2);//接收的xor字符串
374376
string checkStr = Utility.XorCheck(needXorStr);
375377
if (checkStr != recvCheck)
376378
{
377-
OnClose?.Invoke(this, new ShutdownRequestEventArgs("校验失败"));
379+
if (OnClose != null)
380+
OnClose(this, new ShutdownRequestEventArgs("校验失败"));
378381
return null;
379382
}
380383
else
381384
{
382385
if (recv.Substring(4, 1) == "W")//如果是写入命令
383386
{
384-
return new byte[0];
387+
return new byte[0];
385388
}
386389
string dataStr = Utility.Pinchstring(recv, respBeginStr, checkStr);
387390
return Utility.HexToBytes(dataStr);
@@ -391,7 +394,7 @@ string daveStrerror(string code)
391394
{
392395
switch (code)
393396
{
394-
case "20":return "未定义";
397+
case "20": return "未定义";
395398
case "21": return "远程单元无法被正确识别,或者发生了数据错误.";
396399
case "22": return "用于远程单元的接收缓冲区已满.";
397400
case "23": return "远程单元编号(01 至16)设置与本地单元重复.";
@@ -430,7 +433,7 @@ public ItemData<byte> ReadByte(DeviceAddress address)
430433
}
431434
public ItemData<float> ReadFloat(DeviceAddress address)
432435
{
433-
throw new NotImplementedException();
436+
throw new NotImplementedException();
434437
}
435438
public ItemData<short> ReadInt16(DeviceAddress address)
436439
{
@@ -475,10 +478,10 @@ public bool RemoveGroup(IGroup group)
475478

476479
public int WriteBit(DeviceAddress address, bool bit)
477480
{
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;
482485
}
483486

484487
public int WriteBits(DeviceAddress address, byte bits)
@@ -499,7 +502,7 @@ public int WriteFloat(DeviceAddress address, float value)
499502
public int WriteInt16(DeviceAddress address, short value)
500503
{
501504
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);
503506
WriteSyncData(respBeginStr, cmd);
504507
return 0;
505508
}
@@ -609,12 +612,12 @@ public struct Panasonic
609612
/// </summary>
610613
public const string WDCmd = "WD";
611614

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;//链接继电器 不支持
618621

619622
public const byte DTarea = 6;//数据寄存器 DT
620623
public const byte LDarea = 7;//链接寄存器 LD 不支持

SCADA/dll/PanasonicDriver.dll

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)