Skip to content
切换导航条
切换导航条
当前项目
正在载入...
登录
张东亮
/
DoubleLineClient_3D
转到一个项目
切换导航栏
切换导航栏固定状态
项目
群组
代码片段
帮助
项目
活动
版本库
流水线
图表
问题
0
合并请求
0
维基
网络
创建新的问题
作业
提交
问题看板
文件
提交
网络
比较
分支
标签
Commit acc64ed6
由
LN
编写于
2022-11-15 14:03:44 +0800
浏览文件
选项
浏览文件
标签
下载
电子邮件补丁
差异文件
aio代码更新
1 个父辈
ffa0a1c4
隐藏空白字符变更
内嵌
并排
正在显示
6 个修改的文件
包含
855 行增加
和
13 行删除
DoubleLineClient_3D.sln
source/DeviceLibrary/DeviceLibrary.csproj
source/DeviceLibrary/deviceLibrary/IO/AIOBOX/AIOBOX.cs
source/DeviceLibrary/deviceLibrary/IO/AIOBOX/AIOBOXManager.cs
source/DeviceLibrary/deviceLibrary/IO/IOManager.cs
source/DeviceLibrary/doubleLine/DoubleLineBean_lineShelf.cs
DoubleLineClient_3D.sln
查看文件 @
acc64ed
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 1
7
VisualStudioVersion = 1
7.3.32811.315
# Visual Studio Version 1
6
VisualStudioVersion = 1
6.0.31613.86
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Common", "source\Common\Common.csproj", "{43CDD09E-FCF3-4960-A01D-3BBFE9933122}"
EndProject
...
...
@@ -15,7 +15,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ABBRobotTest", "source\ABBR
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DoubleLineClient", "source\DoubleLineClient_3D\DoubleLineClient.csproj", "{0D2542F5-DD62-4352-82D0-383D9A045E74}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DL.IOModule", "
..\RC1250-FeederLineClient\
source\DL.IOModule\DL.IOModule.csproj", "{F85A7412-B5B3-4291-A448-A10564602E1A}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DL.IOModule", "source\DL.IOModule\DL.IOModule.csproj", "{F85A7412-B5B3-4291-A448-A10564602E1A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
...
...
source/DeviceLibrary/DeviceLibrary.csproj
查看文件 @
acc64ed
...
...
@@ -88,6 +88,7 @@
<Compile Include="agvClient\AgvClient_Dbline.cs" />
<Compile Include="bean\RobotBase.cs" />
<Compile Include="deviceLibrary\ABB\ABBControl.cs" />
<Compile Include="deviceLibrary\IO\AIOBOX\AIOBOX.cs" />
<Compile Include="doubleLine\DoubleLineBean.cs" />
<Compile Include="doubleLine\DoubleLineBean_bufShelf.cs" />
<Compile Include="doubleLine\DoubleLineBean_lineShelf.cs" />
...
...
@@ -120,10 +121,6 @@
</Compile>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\RC1250-FeederLineClient\source\DL.IOModule\DL.IOModule.csproj">
<Project>{f85a7412-b5b3-4291-a448-a10564602e1a}</Project>
<Name>DL.IOModule</Name>
</ProjectReference>
<ProjectReference Include="..\Common\Common.csproj">
<Project>{43cdd09e-fcf3-4960-a01d-3bbfe9933122}</Project>
<Name>Common</Name>
...
...
source/DeviceLibrary/deviceLibrary/IO/AIOBOX/AIOBOX.cs
0 → 100644
查看文件 @
acc64ed
using
OnlineStore.Common
;
using
OnlineStore.DeviceLibrary
;
using
System
;
using
System.Collections.Generic
;
using
System.Net
;
using
System.Net.Sockets
;
using
System.Threading
;
namespace
Asa.IOModule
{
/// <summary>
/// 零点IO模块操作类
/// </summary>
public
class
AIOBOX
{
private
string
LogName
{
get
{
return
"AIOBOX ["
+
IP
+
"]"
;
}
}
/// <summary>
/// 自动读取DI委托
/// </summary>
/// <param name="box">AIOBOX</param>
/// <param name="sta">所有DI状态</param>
public
delegate
void
DIO_Changed
(
AIOBOX
box
,
Box_Sta
[]
sta
);
/// <summary>
/// 自动读取DI事件触发
/// </summary>
public
event
DIO_Changed
DI_Changed_Event
;
/// <summary>
/// 自动读取DO事件触发
/// </summary>
public
event
DIO_Changed
DO_Changed_Event
;
private
Socket
_client
;
//客户端
private
bool
_loop
;
private
ushort
_uid
;
private
Box_Sta
[]
_stateDI
;
//DI状态
private
Box_Sta
[]
_stateDO
;
//DO状态
private
int
[]
_valueAI
;
//AI模拟量
private
int
[]
_valueAO
;
//AO模拟量
private
List
<
byte
>
_receive
;
private
System
.
Collections
.
Concurrent
.
ConcurrentQueue
<
byte
[
]>
_writeDO
;
private
log4net
.
ILog
log
;
private
readonly
Box_Type
_typeInput
;
//输入类型
private
readonly
Box_Type
_typeOutput
;
//输出类型
private
readonly
byte
[]
_addressInput
;
//输入地址
private
readonly
byte
[]
_addressOutput
;
//输出地址
private
const
int
SEND_SLEEP
=
60
;
//每条命令发送的间隔,不能小于15,会出现IO接收不到的情况,小于30时,会出现接收数据连包的情况
private
const
int
PORT
=
502
;
//端口
private
const
int
UPLOAD_TIME
=
1000
;
//3秒改为1秒
private
Thread
tRecon
;
//重连线程
private
Thread
tSend
;
//发送命令处理
private
Thread
tListen
;
//监听网络
private
UdpClient
broadcastClient
;
//广播客户端
private
IPEndPoint
broadcastEndPoint
;
//广播远程节点
private
byte
[]
broadcastBuffer
;
//远程返回数据
/// <summary>
/// 零点IO模块操作类
/// </summary>
/// <param name="input">输入类型</param>
/// <param name="inputCount">输入数量</param>
/// <param name="output">输出类型</param>
/// <param name="outputCount">输出数量</param>
public
AIOBOX
(
Box_Type
input
,
int
inputCount
,
Box_Type
output
,
int
outputCount
)
{
_typeInput
=
input
;
_typeOutput
=
output
;
log
=
log4net
.
LogManager
.
GetLogger
(
"AIOBOX"
);
byte
n
=
0
;
_addressInput
=
new
byte
[
inputCount
];
for
(
int
i
=
0
;
i
<
inputCount
;
i
++)
_addressInput
[
i
]
=
n
++;
_addressOutput
=
new
byte
[
outputCount
];
for
(
int
i
=
0
;
i
<
outputCount
;
i
++)
_addressOutput
[
i
]
=
n
++;
_stateDI
=
new
Box_Sta
[
inputCount
];
_stateDO
=
new
Box_Sta
[
outputCount
];
_valueAI
=
new
int
[
inputCount
];
_valueAO
=
new
int
[
outputCount
];
}
/// <summary>
/// IP地址
/// </summary>
public
string
IP
{
set
;
get
;
}
=
"192.168.1.100"
;
/// <summary>
/// 输入主动上传
/// </summary>
public
bool
Upload
{
set
;
get
;
}
=
false
;
/// <summary>
/// 是否连接
/// </summary>
public
bool
IsConn
{
get
;
private
set
;
}
=
false
;
/// <summary>
/// 错误信息
/// </summary>
public
string
ErrInfo
{
get
;
private
set
;
}
=
""
;
/// <summary>
/// 监控DI输入,索引数组
/// </summary>
public
int
[]
MonitorDI
{
get
;
set
;
}
/// <summary>
/// 监控DO输出,索引数组
/// </summary>
public
int
[]
MonitorDO
{
get
;
set
;
}
/// <summary>
/// 连接
/// </summary>
public
void
Connect
()
{
_loop
=
true
;
_uid
=
0
;
IsConn
=
false
;
_receive
=
new
List
<
byte
>();
_writeDO
=
new
System
.
Collections
.
Concurrent
.
ConcurrentQueue
<
byte
[
]>
();
tRecon
=
new
Thread
(
new
ThreadStart
(
Reconn
));
tSend
=
new
Thread
(
new
ThreadStart
(
Send
));
tListen
=
new
Thread
(
new
ThreadStart
(
Listen
));
tRecon
.
IsBackground
=
true
;
tSend
.
IsBackground
=
true
;
tListen
.
IsBackground
=
true
;
tRecon
.
Start
();
tSend
.
Start
();
tListen
.
Start
();
}
/// <summary>
/// 关闭连接
/// </summary>
public
void
Close
()
{
_loop
=
false
;
IsConn
=
false
;
try
{
if
(
_client
!=
null
)
{
_client
.
Shutdown
(
SocketShutdown
.
Both
);
_client
.
Close
();
}
log
.
Info
(
LogName
+
"Close"
);
}
catch
(
Exception
ex
)
{
log
.
Error
(
LogName
+
"Close"
,
ex
);
}
finally
{
_client
=
null
;
}
}
/// <summary>
/// 相反状态(ON/OFF)
/// </summary>
/// <param name="sta"></param>
/// <returns></returns>
public
Box_Sta
ReverseStatus
(
Box_Sta
sta
)
{
return
sta
==
Box_Sta
.
On
?
Box_Sta
.
Off
:
Box_Sta
.
On
;
}
/// <summary>
/// 相反状态(ON/OFF)
/// </summary>
/// <param name="sta"></param>
public
void
ReverseStatus
(
ref
Box_Sta
sta
)
{
if
(
sta
==
Box_Sta
.
On
)
sta
=
Box_Sta
.
Off
;
else
sta
=
Box_Sta
.
On
;
}
/// <summary>
/// 读取单个DI输入状态(ON/OFF)
/// </summary>
/// <param name="add">输入地址,从0开始</param>
/// <returns></returns>
public
Box_Sta
ReadDI
(
int
add
)
{
return
_stateDI
[
add
];
}
/// <summary>
/// 读取多个DI输入状态(ON/OFF)
/// </summary>
/// <param name="add">起始地址,从0开始</param>
/// <param name="count">数量</param>
/// <returns></returns>
public
Box_Sta
[]
ReadDI
(
int
add
,
int
count
)
{
Box_Sta
[]
sta
=
new
Box_Sta
[
count
];
Array
.
Copy
(
_stateDI
,
add
,
sta
,
0
,
count
);
return
sta
;
}
/// <summary>
/// 读取单个AI模拟量的值
/// </summary>
/// <param name="add">模拟量地址,从0开始</param>
/// <returns></returns>
public
int
ReadAI
(
int
add
)
{
return
_valueAI
[
add
];
}
/// <summary>
/// 读取多个AI模拟量的值
/// </summary>
/// <param name="add">起始地址,从0开始</param>
/// <param name="count">数量</param>
/// <returns></returns>
public
int
[]
ReadAI
(
int
add
,
int
count
)
{
int
[]
sta
=
new
int
[
count
];
Array
.
Copy
(
_valueAI
,
add
,
sta
,
0
,
count
);
return
sta
;
}
/// <summary>
/// 读取单个DO输出状态(ON/OFF)
/// </summary>
/// <param name="add">输出地址,从0开始</param>
/// <returns></returns>
public
Box_Sta
ReadDO
(
int
add
)
{
return
_stateDO
[
add
];
}
/// <summary>
/// 读取多个DO输出状态(ON/OFF)
/// </summary>
/// <param name="add">起始地址,从0开始</param>
/// <param name="count">数量</param>
/// <returns></returns>
public
Box_Sta
[]
ReadDO
(
int
add
,
int
count
)
{
Box_Sta
[]
sta
=
new
Box_Sta
[
count
];
Array
.
Copy
(
_stateDO
,
add
,
sta
,
0
,
count
);
return
sta
;
}
/// <summary>
/// 读取单个AO模拟量的值
/// </summary>
/// <param name="add">输出地址,从0开始</param>
/// <returns></returns>
public
int
ReadAO
(
int
add
)
{
return
_valueAO
[
add
];
}
/// <summary>
/// 读取多个AO模拟量的值
/// </summary>
/// <param name="add">起始地址,从0开始</param>
/// <param name="count">数量</param>
/// <returns></returns>
public
int
[]
ReadAO
(
int
add
,
int
count
)
{
int
[]
sta
=
new
int
[
count
];
Array
.
Copy
(
_valueAO
,
add
,
sta
,
0
,
count
);
return
sta
;
}
/// <summary>
/// 写入单个DO输出状态(ON/OFF)
/// </summary>
/// <param name="add">输出地址,从0开始</param>
/// <param name="sta"></param>
/// <returns></returns>
public
bool
WriteDO
(
int
add
,
Box_Sta
sta
)
{
try
{
if
(!
IsConn
)
{
LogUtil
.
error
(
LogName
+
"WriteDO Fail : IsConn= "
+
IsConn
);
log
.
Error
(
LogName
+
"WriteDO Fail : IsConn= "
+
IsConn
);
return
false
;
}
log
.
Debug
(
LogName
+
"WriteDO【"
+
$
"{add}={sta}"
+
"】"
);
byte
[]
data
=
Command
();
byte
[]
buff
=
new
byte
[
12
];
Array
.
Copy
(
data
,
0
,
buff
,
0
,
data
.
Length
);
buff
[
5
]
=
6
;
//后面字节数
buff
[
7
]
=
5
;
//功能码
buff
[
9
]
=
_addressOutput
[
add
];
//地址
buff
[
10
]
=
(
byte
)
sta
;
//写入值
//_writeDO.Enqueue(buff);
return
SendToDevice
(
buff
);
}
catch
(
Exception
ex
)
{
LogUtil
.
error
(
LogName
+
"WriteDO【"
+
$
"{add}={sta}"
+
"】"
,
ex
);
log
.
Error
(
LogName
+
"WriteDO【"
+
$
"{add}={sta}"
+
"】"
,
ex
);
return
false
;
}
}
/// <summary>
/// 发送命令线程
/// </summary>
private
void
Send
()
{
bool
shift
=
true
;
//切换
int
time
=
0
;
//上传时间
int
writeTimer
=
0
;
//写入DO次数
bool
writeStop
=
false
;
//写入命令停止
while
(
_loop
)
{
Thread
.
Sleep
(
SEND_SLEEP
);
if
(!
IsConn
)
continue
;
byte
[]
buff
=
null
;
if
(
Upload
)
//主动上传
{
if
(
time
>=
UPLOAD_TIME
)
{
buff
=
GetReadDI_Command
();
time
=
0
;
}
//else
//{
// if (writeTimer >= 3) //连续发送WriteDO3次,强制发送DO
// {
// buff = GetReadDO_Command();
// writeTimer = 0;
// }
// else
// {
// if (_writeDO.TryDequeue(out buff))
// {
// writeTimer++;
// }
// else
// {
// buff = GetReadDO_Command();
// writeTimer = 0;
// }
// }
//}
time
+=
SEND_SLEEP
;
}
else
{
if
(
writeTimer
>=
3
)
//连续发送WriteDO3次,强制发送DI和DO
{
if
(
shift
)
buff
=
GetReadDI_Command
();
else
buff
=
GetReadDO_Command
();
shift
=
!
shift
;
if
(
writeStop
)
writeTimer
=
0
;
else
writeStop
=
true
;
}
else
{
if
(
_writeDO
.
TryDequeue
(
out
buff
))
{
writeTimer
++;
writeStop
=
false
;
}
else
{
//if (shift)
// buff = GetReadDI_Command();
//else
// buff = GetReadDO_Command();
shift
=
!
shift
;
writeTimer
=
0
;
}
}
}
if
(
buff
!=
null
)
{
SendToDevice
(
buff
);
}
}
}
private
bool
SendToDevice
(
byte
[]
buff
)
{
try
{
if
(
IsConn
)
{
_client
.
Send
(
buff
);
log
.
Debug
(
LogName
+
"Send: "
+
HexBuff
(
buff
));
return
true
;
}
}
catch
(
Exception
ex
)
{
LogUtil
.
error
(
LogName
+
"Send Error Socket Close"
,
ex
);
log
.
Error
(
LogName
+
"Send Error,Socket Close "
,
ex
);
IsConn
=
false
;
}
return
false
;
}
/// <summary>
/// 获取ReadDI的命令
/// </summary>
/// <returns></returns>
private
byte
[]
GetReadDI_Command
()
{
byte
[]
data
=
Command
();
byte
[]
buff
=
new
byte
[
12
];
Array
.
Copy
(
data
,
0
,
buff
,
0
,
data
.
Length
);
buff
[
5
]
=
6
;
//后面字节数
//功能码
if
(
_typeInput
==
Box_Type
.
DI
)
buff
[
7
]
=
2
;
else
if
(
_typeInput
==
Box_Type
.
AI
)
buff
[
7
]
=
4
;
buff
[
9
]
=
_addressInput
[
0
];
//地址
buff
[
11
]
=
(
byte
)
_addressInput
.
Length
;
//个数
return
buff
;
}
/// <summary>
/// 获取ReadDO的命令
/// </summary>
/// <returns></returns>
private
byte
[]
GetReadDO_Command
()
{
byte
[]
data
=
Command
();
byte
[]
buff
=
new
byte
[
12
];
Array
.
Copy
(
data
,
0
,
buff
,
0
,
data
.
Length
);
buff
[
5
]
=
6
;
//后面字节数
buff
[
7
]
=
1
;
//功能码
buff
[
9
]
=
_addressOutput
[
0
];
//地址
buff
[
11
]
=
(
byte
)
_addressOutput
.
Length
;
//个数
return
buff
;
}
/// <summary>
/// 监听网络线程
/// </summary>
private
void
Listen
()
{
int
index
=
0
;
while
(
_loop
)
{
Thread
.
Sleep
(
10
);
if
(!
IsConn
)
continue
;
if
(
_client
==
null
)
continue
;
try
{
if
(
_client
.
Available
>
0
)
{
byte
[]
buff
=
new
byte
[
_client
.
ReceiveBufferSize
];
int
count
=
_client
.
Receive
(
buff
);
byte
[]
temp
=
new
byte
[
count
];
Array
.
Copy
(
buff
,
0
,
temp
,
0
,
count
);
// lock (_receive)
{
_receive
.
AddRange
(
temp
);
while
(
_receive
.
Count
>
6
)
//分解连包
{
int
len
=
_receive
[
5
]
+
6
;
if
(
len
>
_receive
.
Count
)
break
;
byte
[]
cmd
=
new
byte
[
len
];
_receive
.
CopyTo
(
0
,
cmd
,
0
,
len
);
_receive
.
RemoveRange
(
0
,
len
);
index
++;
log
.
Debug
(
LogName
+
"Receive: "
+
HexBuff
(
cmd
));
if
(
IP
.
Equals
(
"192.168.101.22"
)
&&
cmd
.
Length
>
7
&&
(
cmd
[
7
]
==
2
))
{
LogUtil
.
LOGGER
.
Debug
(
LogName
+
" Listen 收到数据 ["
+
index
+
"] "
+
HexBuff
(
cmd
));
}
CommandProcess
(
cmd
,
index
);
// System.Threading.Tasks.Task.Run(() => CommandProcess(cmd, index));
}
}
}
}
catch
(
Exception
ex
)
{
LogUtil
.
error
(
LogName
+
"Listen "
,
ex
);
log
.
Error
(
LogName
+
"Listen "
,
ex
);
IsConn
=
false
;
}
}
}
/// <summary>
/// 接收到的命令处理方法,(task多线程)
/// </summary>
/// <param name="cmd"></param>
private
void
CommandProcess
(
byte
[]
cmd
,
int
index
)
{
string
str
;
int
idx
=
0
;
int
count
=
cmd
[
8
];
try
{
if
(
cmd
[
7
]
==
1
)
//ReadDO
{
Box_Sta
[]
staDO
=
new
Box_Sta
[
_stateDO
.
Length
];
// lock (_stateDO)
{
for
(
int
i
=
1
;
i
<=
count
;
i
++)
{
int
move
=
0
;
byte
val
=
cmd
[
8
+
i
];
for
(
int
j
=
0
;
j
<
8
;
j
++)
//字节的0-7位
{
int
n
=
(
val
&
Convert
.
ToInt32
(
Math
.
Pow
(
2
,
move
)))
>>
move
;
staDO
[
idx
++]
=
n
==
1
?
Box_Sta
.
On
:
Box_Sta
.
Off
;
move
++;
}
}
Array
.
Copy
(
staDO
,
0
,
_stateDO
,
0
,
staDO
.
Length
);
if
(
MonitorDO
!=
null
)
{
str
=
"MonitorDO "
;
for
(
int
i
=
0
;
i
<
MonitorDO
.
Length
;
i
++)
str
+=
string
.
Format
(
"[{0}]{1} "
,
MonitorDO
[
i
],
_stateDO
[
MonitorDO
[
i
]].
ToString
());
log
.
Info
(
LogName
+
str
);
}
}
DO_Changed_Event
?.
Invoke
(
this
,
staDO
);
}
else
if
(
cmd
[
7
]
==
2
)
//ReadDI
{
Box_Sta
[]
staDI
=
new
Box_Sta
[
_stateDI
.
Length
];
// lock (_stateDI)
{
for
(
int
i
=
1
;
i
<=
count
;
i
++)
{
int
move
=
0
;
byte
val
=
cmd
[
8
+
i
];
for
(
int
j
=
0
;
j
<
8
;
j
++)
//字节的0-7位
{
int
n
=
(
val
&
Convert
.
ToInt32
(
Math
.
Pow
(
2
,
move
)))
>>
move
;
staDI
[
idx
++]
=
n
==
1
?
Box_Sta
.
On
:
Box_Sta
.
Off
;
move
++;
}
}
Array
.
Copy
(
staDI
,
0
,
_stateDI
,
0
,
staDI
.
Length
);
if
(
MonitorDI
!=
null
)
{
str
=
"MonitorDI "
;
for
(
int
i
=
0
;
i
<
MonitorDI
.
Length
;
i
++)
str
+=
string
.
Format
(
"[{0}]{1} "
,
MonitorDI
[
i
],
_stateDI
[
MonitorDI
[
i
]].
ToString
());
log
.
Info
(
LogName
+
str
);
}
}
if
(
IP
.
Equals
(
"192.168.101.22"
))
{
string
msg
=
""
;
foreach
(
Box_Sta
s
in
staDI
)
{
msg
+=
" "
+
(
int
)
s
;
}
LogUtil
.
LOGGER
.
Debug
(
LogName
+
" DI_Changed_Event ["
+
index
+
"] "
+
msg
);
}
DI_Changed_Event
?.
Invoke
(
this
,
staDI
);
}
else
if
(
cmd
[
7
]
==
5
)
{
idx
=
Array
.
FindIndex
(
_addressOutput
,
s
=>
s
==
cmd
[
9
]);
if
(
idx
!=
-
1
)
{
Box_Sta
[]
staDO
=
new
Box_Sta
[
_stateDO
.
Length
];
_stateDO
[
idx
]
=
cmd
[
10
]
==
0xFF
?
Box_Sta
.
On
:
Box_Sta
.
Off
;
Array
.
Copy
(
_stateDO
,
staDO
,
staDO
.
Length
);
DO_Changed_Event
?.
Invoke
(
this
,
staDO
);
//System.Threading.Tasks.Task.Run(() => DO_Changed_Event?.Invoke(this, staDO));
}
}
}
catch
(
Exception
ex
)
{
LogUtil
.
error
(
LogName
+
"CommandProcess "
,
ex
);
ErrInfo
=
ex
.
Message
;
}
}
/// <summary>
/// 重连线程
/// </summary>
private
void
Reconn
()
{
while
(
_loop
)
{
if
(
IsConn
)
{
Thread
.
Sleep
(
1000
);
}
else
{
//if (AIOAutoConSetting.NeedAutoConnect())
//{
Thread
.
Sleep
(
200
);
if
(
_loop
)
{
Open
();
}
Thread
.
Sleep
(
200
);
//}
//else
//{
// Thread.Sleep(1000);
//}
}
}
}
/// <summary>
/// 打开socket建立连接
/// </summary>
private
void
Open
()
{
try
{
IsConn
=
false
;
bool
rtn
=
CheckIP
(
IP
);
if
(!
rtn
)
return
;
//建立连接
_client
=
new
Socket
(
AddressFamily
.
InterNetwork
,
SocketType
.
Stream
,
ProtocolType
.
Tcp
);
_client
.
SetSocketOption
(
SocketOptionLevel
.
Socket
,
SocketOptionName
.
SendTimeout
,
500
);
_client
.
SetSocketOption
(
SocketOptionLevel
.
Socket
,
SocketOptionName
.
ReceiveTimeout
,
500
);
_client
.
SetSocketOption
(
SocketOptionLevel
.
Socket
,
SocketOptionName
.
NoDelay
,
1
);
_client
.
Connect
(
IPAddress
.
Parse
(
IP
),
PORT
);
Thread
.
Sleep
(
100
);
//需要等待一会才能获取连接状态
IsConn
=
true
;
byte
[]
buff
=
GetReadDI_Command
();
SendToDevice
(
buff
);
buff
=
GetReadDO_Command
();
SendToDevice
(
buff
);
LogUtil
.
info
(
LogName
+
"Socket Connect OK"
);
log
.
Info
(
LogName
+
"Socket Connect OK"
);
}
catch
(
Exception
ex
)
{
IsConn
=
false
;
log
.
Error
(
LogName
+
"Open "
,
ex
);
LogUtil
.
error
(
LogName
+
"Open "
,
ex
);
}
}
/// <summary>
/// 检查IP地址
/// </summary>
/// <param name="ip"></param>
/// <returns></returns>
private
bool
CheckIP
(
string
ip
)
{
//IP合法
string
pattern
=
@"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$"
;
bool
rtn
=
System
.
Text
.
RegularExpressions
.
Regex
.
IsMatch
(
ip
,
pattern
);
if
(!
rtn
)
{
ErrInfo
=
"非法的IP地址"
;
return
false
;
}
//Ping服务端
System
.
Net
.
NetworkInformation
.
Ping
ping
=
new
System
.
Net
.
NetworkInformation
.
Ping
();
System
.
Net
.
NetworkInformation
.
PingReply
result
=
ping
.
Send
(
ip
,
1000
);
ping
.
Dispose
();
if
(
result
.
Status
!=
System
.
Net
.
NetworkInformation
.
IPStatus
.
Success
)
{
ErrInfo
=
"Ping "
+
ip
+
" 请求没有响应"
;
return
false
;
}
return
true
;
}
/// <summary>
/// 自动获取IP地址,未连接前使用,必须在同一网段
/// </summary>
/// <param name="localIP">本地IP地址</param>
/// <returns></returns>
private
bool
AutoIP
(
string
localIP
)
{
try
{
IPEndPoint
local
=
new
IPEndPoint
(
IPAddress
.
Parse
(
localIP
),
55654
);
broadcastClient
=
new
UdpClient
(
local
);
broadcastBuffer
=
null
;
Thread
tTemp
=
new
Thread
(
new
ThreadStart
(
GetIP
));
tTemp
.
Start
();
broadcastEndPoint
=
new
IPEndPoint
(
IPAddress
.
Parse
(
"255.255.255.255"
),
1024
);
byte
[]
dgram
=
new
byte
[]
{
0x05
,
0x00
,
0x07
,
0x00
,
0x00
,
0xC1
,
0x59
};
broadcastClient
.
Send
(
dgram
,
dgram
.
Length
,
broadcastEndPoint
);
Thread
.
Sleep
(
1000
);
tTemp
.
Abort
();
broadcastClient
.
Close
();
ErrInfo
=
"无法访问"
;
if
(
broadcastBuffer
==
null
)
return
false
;
if
(
broadcastBuffer
[
0
]
!=
0x3B
)
return
false
;
byte
[]
buff
=
new
byte
[
broadcastBuffer
[
7
]];
Array
.
Copy
(
broadcastBuffer
,
9
,
buff
,
0
,
buff
.
Length
);
IP
=
buff
[
buff
.
Length
-
12
]
+
"."
+
buff
[
buff
.
Length
-
11
]
+
"."
+
buff
[
buff
.
Length
-
10
]
+
"."
+
buff
[
buff
.
Length
-
9
];
ErrInfo
=
"OK"
;
return
true
;
}
catch
(
Exception
ex
)
{
ErrInfo
=
ex
.
Message
;
LogUtil
.
error
(
LogName
+
"AutoIP "
,
ex
);
return
false
;
}
}
/// <summary>
/// 获取IO模块IP地址
/// </summary>
private
void
GetIP
()
{
broadcastBuffer
=
broadcastClient
.
Receive
(
ref
broadcastEndPoint
);
}
/// <summary>
/// 命令,前7个字节
/// </summary>
/// <returns></returns>
[
System
.
Runtime
.
CompilerServices
.
MethodImpl
(
System
.
Runtime
.
CompilerServices
.
MethodImplOptions
.
Synchronized
)]
private
byte
[]
Command
()
{
byte
[]
flag
=
BitConverter
.
GetBytes
(++
_uid
);
if
(
_uid
==
ushort
.
MaxValue
)
_uid
=
0
;
byte
[]
data
=
new
byte
[
7
];
data
[
0
]
=
flag
[
1
];
data
[
1
]
=
flag
[
0
];
data
[
2
]
=
0
;
data
[
3
]
=
0
;
data
[
4
]
=
0
;
data
[
5
]
=
0
;
data
[
6
]
=
255
;
return
data
;
}
private
string
HexBuff
(
byte
[]
buff
)
{
string
s
=
""
;
if
(
buff
==
null
)
return
s
;
for
(
int
i
=
0
;
i
<
buff
.
Length
;
i
++)
s
+=
buff
[
i
].
ToString
(
"X2"
)
+
" "
;
return
s
;
}
}
/// <summary>
/// IO模块类型
/// </summary>
public
enum
Box_Type
:
int
{
/// <summary>
/// 数字信号输入
/// </summary>
DI
,
/// <summary>
/// 数字信号输出
/// </summary>
DO
,
/// <summary>
/// 模拟量输入
/// </summary>
AI
,
/// <summary>
/// 模拟量输出
/// </summary>
AO
}
/// <summary>
/// IO模块寄存器状态
/// </summary>
public
enum
Box_Sta
:
int
{
/// <summary>
/// 断开,关闭,低电平
/// </summary>
Off
=
0
,
/// <summary>
/// 闭合,打开,高电平
/// </summary>
On
=
255
}
}
source/DeviceLibrary/deviceLibrary/IO/AIOBOX/AIOBOXManager.cs
查看文件 @
acc64ed
...
...
@@ -8,9 +8,9 @@ using System.Threading;
using
OnlineStore.Common
;
using
OnlineStore.LoadCSVLibrary
;
using
System.Threading.Tasks
;
//using Asa.IOModule;
using
DL.IOModule
;
//using Asa.IOModule;
using
System.Windows.Forms
;
using
Asa.IOModule
;
namespace
OnlineStore.DeviceLibrary
{
...
...
@@ -82,9 +82,9 @@ namespace OnlineStore.DeviceLibrary
string
logName
=
"IO模块["
+
ioIp
+
"] DI["
+
DILength
+
"] DO["
+
DOLength
+
"],["
+
DIMS
+
"] ["
+
DOMS
+
"]"
;
try
{
aioBox
=
new
AIOBOX
();
aioBox
=
new
AIOBOX
(
Box_Type
.
DI
,
DILength
,
Box_Type
.
DO
,
DOLength
);
aioBox
.
IP
=
ioIp
;
aioBox
.
SetType
(
IOType
.
DI
,
DILength
,
IOType
.
DO
,
DOLength
);
//
aioBox.SetType(IOType.DI, DILength, IOType.DO, DOLength);
//DI主动上传
aioBox
.
Upload
=
true
;
...
...
source/DeviceLibrary/deviceLibrary/IO/IOManager.cs
查看文件 @
acc64ed
...
...
@@ -23,9 +23,18 @@ namespace OnlineStore.DeviceLibrary
ConfigIO
configIo
=
LineManager
.
Config
.
DOList
[
ioType
];
instance
.
WriteSingleDO
(
configIo
.
IO_IP
,
configIo
.
SlaveID
,
configIo
.
GetIOAddr
(),
ioValue
);
Thread
.
Sleep
(
60
);
while
(!
IOValue
(
ioType
).
Equals
(
ioValue
))
int
tryTime
=
0
;
while
(
tryTime
<
10
)
{
IO_VALUE
currValue
=
instance
.
GetDOValue
(
configIo
.
IO_IP
,
configIo
.
SlaveID
,
configIo
.
GetIOAddr
());
if
(
currValue
.
Equals
(
ioValue
))
{
break
;
}
instance
.
WriteSingleDO
(
configIo
.
IO_IP
,
configIo
.
SlaveID
,
configIo
.
GetIOAddr
(),
ioValue
);
tryTime
++;
LogUtil
.
error
(
"IOMove: tryTime="
+
tryTime
+
", ioType ="
+
ioType
+
",ioValue="
+
ioValue
+
",currValue="
+
currValue
);
Thread
.
Sleep
(
60
);
}
}
...
...
source/DeviceLibrary/doubleLine/DoubleLineBean_lineShelf.cs
查看文件 @
acc64ed
...
...
@@ -175,7 +175,13 @@ namespace OnlineStore.DeviceLibrary
else
{
inLineWatch
.
Stop
();
CheckAndMove
(
IO_Type
.
Line_StopCylinder
,
IO_VALUE
.
LOW
);
if
(
MoveInfo
.
MoveType
.
Equals
(
LineMoveType
.
LeftShelf
)&&
MoveInfo
.
MoveStep
.
Equals
(
MoveStep
.
SI04_InStopDown
))
{
}
else
{
CheckAndMove
(
IO_Type
.
Line_StopCylinder
,
IO_VALUE
.
LOW
);
}
}
//右侧出口
...
...
编写
预览
支持
Markdown
格式
附加文件
你添加了
0
人
到此讨论。请谨慎行事。
Finish editing this message first!
Cancel
请
注册
或
登录
后发表评论