搜索了很多资源,发现网上的对于海康机器人ID5000 系列智能读码器系列的文章实在是少,幸好找到一篇海康自己的用户手册。在此写下自己的操作步骤。
其他相關文章
海康机器人客户端
需要下载海康自带的IDMVS,海康没有整合工业相机和读码器一体的,所以工业相机的是MVS,扫码器是IDMVS,就很不方便。附上链接:
海康机器人-机器视觉-下载中心 (hikrobotics.com)https://www.hikrobotics.com/cn/machinevision/service/download?module=0
下载完之后安装,没啥注意的,选个安装位置就行
然后看下它的文档
找到下载位置打开doc文件夹
前两个是中英文的海康威视网站,没啥用,中间两个是一些术语,看不懂。
最后两个是 工业读码器通信指令 可以配置一些通信
具体要修改可以在这里看。
扫码器的连接和使用
首先附上找到的用户手册:
链接:https://pan.baidu.com/s/1CERcIU_3wZZUsWmABLpDXg
提取码:a91g
接下来是接线,
它是一根电源线带12根管脚,以及一根网线接口
电源接白棕两根,我用的二孔插头,只要分清楚VCC和GND就行
进入IDMVS操作界面
刚连接到摄像头会让你分配地址,我们只用设置最后一位就行(由于我已经设置完了,复现不了了),就相当于下图25可以改成任意0-255之间的数字
前三位是由你的局域网设置决定的,如下
前三位相同就叫做在同一个网段,就是在同一个局域网下
连接到了之后
打开test模式
作说明:
normal模式下是相机正常使用,断开连接之后扫码器正常使用
test模式下只能连接使用,但是接受帧率可以达到20以上,非常流程适合调试
raw模式暂时未知,但帧率很低
图像设置
依据实际情况来设置参数
算法配置
这一部分只用设置上面的二维码种类即可
不得不说海康做的很成功,种类真的是很多
通信配置
test模式只有SDK通信,就是客户端通信,把获取到的信息提供在客户端上,
真正使用还是在normal下设置TCP连接
重点!!!!!配置管理
千万要记得保存配置
至此,扫码器算是调配好了。
TCP数据获取
很奇怪的是,TCP传入的参数在java上打印不出来,我写了半天的java程序能建立TCP连接但是接受到的数据产生了错误。望有没有人做出来告知我,万分感谢
附上C#代码
可用
项目结构
Form1.cs代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
//读取APP.config下的内容
public partial class Form1 : Form
{
#region config設定
static string SER_IP
{
get
{
return Convert.ToString(ConfigurationManager.AppSettings["SER_IP"]);
}
}
static int SER_Port
{
get
{
return Convert.ToInt32(ConfigurationManager.AppSettings["SER_Port"]);
}
}
static string Org
{
get
{
return Convert.ToString(ConfigurationManager.AppSettings["Org"]);
}
}
static string pnl_log_path
{
get
{
if (Convert.ToString(ConfigurationManager.AppSettings["pnl_log_path"]) != "")
return Convert.ToString(ConfigurationManager.AppSettings["pnl_log_path"]);
else
return @"D:\pnl_log_hist";
}
}
#endregion
public delegate void MyInvoke(string str);
static Socket serverSocket;
private static int myProt = SER_Port; //端口
private static string v_ip_port = "";
private static byte[] result = new byte[1024];
static IPHostEntry ipHost = Dns.GetHostEntry(Dns.GetHostName());
//static string localIP = ipHost.AddressList.Where(x => x.ToString().StartsWith("10.9")).FirstOrDefault().ToString();
private string m_storedFilePath = pnl_log_path;
public Form1()
{
//string xx = "ERROR".Substring(0, 17);
InitializeComponent();
//隐藏掉窗口上部的关闭按钮
this.ControlBox = true;
CollectData();
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
//serverSocket.Close();
//serverSocket.Dispose();
System.Environment.Exit(0);
}
#region Form關閉事件
/// <summary>
/// Form關閉事件
/// </summary>
/// <param name="e"></param>
protected override void OnFormClosing(FormClosingEventArgs e)
{
e.Cancel = true;
base.OnFormClosing(e);
}
#endregion
public void CollectData()
{
try
{
if (!Directory.Exists(ConfigurationManager.AppSettings["exe_log"].ToString()))
{
Directory.CreateDirectory(ConfigurationManager.AppSettings["exe_log"].ToString());
}
}
catch (Exception ex)
{
Console.WriteLine(DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss") + ",<CollectData>," + ex.Message);
}
try
{
//服務器IP地址
IPAddress ip = IPAddress.Parse(SER_IP);
serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
serverSocket.Bind(new IPEndPoint(ip, myProt)); //绑定IP地址:端口
serverSocket.Listen(10); //设定最多10个排队连接请求
Console.WriteLine("啟動監聽{0}成功", serverSocket.LocalEndPoint.ToString());
//通过Clientsoket发送数据
Thread myThread = new Thread(ListenClientConnect);
myThread.Start();
}
catch (Exception ex)
{
// Console.WriteLine(ex.Message + "<ListenClientConnect>" + " " + DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss"));
using (var fs = new FileStream(ConfigurationManager.AppSettings["exe_log"] + "\\" + DateTime.Now.ToString("yyyyMMdd") + ".csv", FileMode.Append, FileAccess.Write, FileShare.Read))
{
using (System.IO.StreamWriter textFile = new System.IO.StreamWriter(fs))
{
textFile.WriteLine(DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss") + ",<CollectData>," + ex.Message);
textFile.Close();
}
}
}
}
/// <summary>
/// 监听客户端连接
/// </summary>
private void ListenClientConnect()
{
try
{
while (true)
{
Socket clientSocket = serverSocket.Accept();
//clientSocket.Send(Encoding.ASCII.GetBytes("Server Say Hello"));
v_ip_port = clientSocket.RemoteEndPoint.ToString();
string[] ipRows = v_ip_port.Split(':');
//v_ip = ipRows[0].ToString();
Console.WriteLine("客戶端{0}連線成功", clientSocket.RemoteEndPoint.ToString());
Thread receiveThread = new Thread(ReceiveMessage);
receiveThread.Start(clientSocket);
}
}
catch (Exception ex)
{
// Console.WriteLine(ex.Message + "<ListenClientConnect>" + " " + DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss"));
using (var fs = new FileStream(ConfigurationManager.AppSettings["exe_log"] + "\\" + DateTime.Now.ToString("yyyyMMdd") + ".csv", FileMode.Append, FileAccess.Write, FileShare.Read))
{
using (System.IO.StreamWriter textFile = new System.IO.StreamWriter(fs))
{
textFile.WriteLine(ex.Message + "<ListenClientConnect>" + " " + DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss"));
textFile.Close();
}
}
}
}
/// <summary>
/// 接收消息
/// </summary>
/// <param name="clientSocket"></param>
private void ReceiveMessage(object clientSocket)
{
string p_ip = "";
string p_ip_port = "";
Socket myClientSocket = (Socket)clientSocket;
string str = "", sql = "", sign = "";
HashSet<string> uniqueValues = new HashSet<string>();
while (true)
{
p_ip = "";
p_ip_port = "";
try
{
p_ip_port = myClientSocket.RemoteEndPoint.ToString();
string strRow = p_ip_port;
string[] strRows = strRow.Split(':');
string temp = "";
p_ip = strRows[0].ToString(); //Reader IP
// Console.WriteLine(p_ip);
//通过clientSocket接收数据
int receiveNumber = myClientSocket.Receive(result);
//showMsg("receiveNumber=" + receiveNumber + "\n");
/*for (int xx = 0; xx < receiveNumber;xx++ )
{
showMsg(result[xx]+",");
}*/
temp = Encoding.ASCII.GetString(result, 0, receiveNumber);
string pattern = @"<p>(.*?)<\/p>";
String msg = ExtractData(temp, pattern);
if (uniqueValues.Contains(msg))
{
showMsg(msg + " " + DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss") + "\n");
}
else
{
uniqueValues.Add(msg);
showMsg("扫描到二维码" + msg + " " + DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss") + "\n");
}
//msg = msg.TrimEnd();
// Console.WriteLine("MSG=" + msg + ", " + DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss"));
//Available
//
//socket :由于Socket 一次传输数据有限,因此需要多次接受数据传输
// int receiveNumber = 0;
int receiveLength = 0;
int index = 0;
//ThreadPool.QueueUserWorkItem(TestMethod, p_ip + "-" + msg);
//Thread.Sleep(TimeSpan.FromSeconds(10));
}
catch (Exception ex)
{
myClientSocket.Shutdown(SocketShutdown.Both);
myClientSocket.Close();
// Console.WriteLine(ex.Message + "<ReceiveMessage>" + " " + DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss"));
using (var fs = new FileStream(ConfigurationManager.AppSettings["exe_log"] + "\\" + DateTime.Now.ToString("yyyyMMdd") + ".csv", FileMode.Append, FileAccess.Write, FileShare.Read))
{
using (System.IO.StreamWriter textFile = new System.IO.StreamWriter(fs))
{
textFile.WriteLine(DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss") + ",<ReceiveMessage>," + ex.Message + "," + p_ip_port);
textFile.Close();
}
}
if (!myClientSocket.Connected)
{
break;
}
}
}
}
public void showMsg(string msg)
{
if (richTextBox1.InvokeRequired)
{
MyInvoke _myinvoke = new MyInvoke(showMsg);
richTextBox1.Invoke(_myinvoke, new object[] { msg });
}
else
{
richTextBox1.AppendText(msg);
}
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
if (richTextBox1.Lines.Length > 50)//大于100行時清楚前面的
{
int n = 20;
int start = richTextBox1.GetFirstCharIndexFromLine(0);//第一行第一個字符索引
int end = richTextBox1.GetFirstCharIndexFromLine(n);//第n行第一個字符索引
richTextBox1.Select(start, end);//選中n行
richTextBox1.SelectedText = "";//設置前n行為空
}
richTextBox1.Focus();
richTextBox1.Select(richTextBox1.TextLength, 0);
}
//正则表达式处理获得到的内容
static string ExtractData(string input, string pattern)
{
Match match = Regex.Match(input, pattern);
if (match.Success)
{
return match.Groups[1].Value;
}
return null;
}
private void label1_Click(object sender, EventArgs e)
{
}
}
}
APP.con代码
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="Org" value="CQ1" />
<add key="SER_IP" value="192.168.10.21" />
<add key="SER_Port" value="5000" />
<add key="pnl_log_path" value="D:\pnl_log_hist" />
<add key="exe_log" value="D:\exelog" />
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
</configuration>
界面
最终获取数据
第一次扫描到会说扫描到二维码,
之后还是这个二维码的话就正常打印内容加时间