1 public bool WriteWordsByte_B(out string msg, PlcMemory mr, short startIndex, byte[] inData)
2 {
3 msg = string.Empty;
4 try
5 {
6 if (inData == null || inData.Length < 1)
7 {
8 msg = "写入数据失败,写入数据为空!";
9 return false;
10 }
11 //奇数补零,写入数据必须为一个字
12 if ((inData.Length % 2) > 0)
13 {
14 inData = inData.Concat(new byte[1] { 0 }).ToArray();
15 }
16 //写入长度大于2000
17 System.Diagnostics.Stopwatch sp = new System.Diagnostics.Stopwatch();
18 sp.Start();
19 int i = 0;int len = inData.Length;
20 for (int index= startIndex; index < startIndex + len; index += OmronConsts.MAXRWRIDATE)
21 {
22 int _newLen = len + startIndex - index;
23 if (_newLen > OmronConsts.MAXRWRIDATE) _newLen = OmronConsts.MAXRWRIDATE;
24 i++;
25 byte[] nData = new byte[_newLen];
26
27 Array.Copy(inData, index- startIndex, nData,0, _newLen);
28
29 byte[] dataHead = FinsCmd(RorW.Write, mr, MemoryType.Word, (short)(index/2), 00, (short)(_newLen /2));
30
31 byte[] zData = new byte[_newLen+34];
32
33 dataHead.CopyTo(zData,0);
34
35 nData.CopyTo(zData, 34);
36
37 if (!SocketHelper.SendData(out msg, tcpClient, zData))
38 {
39 msg = $"写入,数据写入失败[{i}次]:{msg}!";
40 return false;
41 }
42 byte[] rBuffer= new byte[30];
43 if (!SocketHelper.ReceiveData(out msg, tcpClient, rBuffer))
44 {
45 msg = $"写入,数据接收失败[{i}次]:{msg}!";
46 return false;
47 }
48 if (rBuffer[11] == 3)
49 {
50 if (!ErrorCode.CheckHeadError(rBuffer[15], out msg))
51 {
52 msg = $"写入数据失败[{i}次]:{msg}!";
53 return false;
54 }
55 }
56 if (!ErrorCode.CheckEndCode(rBuffer[28], rBuffer[29], out msg))
57 {
58 msg = $"写入数据失败[{i}次]:{msg}!";
59 return false;
60 }
61
62 }
63 msg = $"写入({len})字节数据成功,耗时{sp.Elapsed.TotalMilliseconds.ToString()}ms,{i}次写入";
64 return true;
65 }
66 catch (Exception ex)
67 {
68 msg = ex.Message;
69 return false;
70 }
71 }