做erp总是免不了与各种硬件设备做对接,本人做畜牧行业erp。领导给分配了一个需求,就是开发托利多t800电子秤接口与erp对接!
生猪出笼称重,数据对接erp增加至数据库!
当然接口类工作都是非常简单的,无论是软件接口微信接口,支付宝接口等等还是硬件接口如温湿度接口,电子秤接口!
但是硬件设备公司都是很流氓的,不想软件公司,接口都会有api开发文档,参数详细。有的好友demo;当然此次开发电子秤接口也是如此,
公司只给了一份电子秤使用说明书!别说demo了,说起来都是泪呀!开始在网上寻找api开发文档,当然第一步肯定是现在,托利多官网寻找开发文档,不出所料
也是三无,没有开发文档,没有api接口说明书,没有demo。去网上找吧,最终找到了电子秤demo也就是讲解SerialPort类监控com串口!经过一番捣鼓,是能接口数据了可是数据类型,怎么确定哪,测试数据怎么传哪?还是网上找吧!不说废话了经过一番寻找好在都找到了!
开始正式讲解电子秤开发接口
1,安装 virtual-serial-port-kit用来带卡com串口(可以从文章地府附件内下载,也可以网上搜索)
2,下载ComDebug.exe用于发送测试数据 (可以从文章地府附件内下载,也可以网上搜索)
3,测试数据 (可以从文章地府附件内下载,也可以网上搜索)
4,开发软件监控com串口接收数据 (可以从文章地府附件内下载demo)
5,根据需求处理接收到的数据
6,可以与我交流吆QQ:809451989
demo代码如下
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;
using Microsoft.Win32;
namespace TEXTCOM
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private SerialPort Sp = new SerialPort();
public delegate void HandleInterfaceUpdataDelegate(string text); //委托,此为重点
private HandleInterfaceUpdataDelegate interfaceUpdataHandle;
private bool qiii = false;
public void Sp_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
string strTemp = "";
double iSecond = 0.5;
DateTime dtOld = System.DateTime.Now;
DateTime dtNow = System.DateTime.Now;
TimeSpan dtInter;
dtInter = dtNow - dtOld;
int i = Sp.BytesToRead;
if (i > 0)
{
try
{
strTemp = Sp.ReadExisting();
}
catch
{ }
if (strTemp.ToLower().IndexOf("\r") < 0)
{
i = 0;
}
else
{
this.Invoke(interfaceUpdataHandle, strTemp);
}
}
while (dtInter.TotalSeconds < iSecond && i <= 0)
{
dtNow = System.DateTime.Now;
dtInter = dtNow - dtOld;
i = Sp.BytesToRead;
if (i > 0)
{
try
{
strTemp += Sp.ReadExisting();
}
catch
{ }
if (strTemp.ToLower().IndexOf("\r") < 0)
{
i = 0;
}
else
{
this.Invoke(interfaceUpdataHandle, strTemp);
}
}
}
// do null
}
private void UpdateTextBox(string text)
{
textBox2.Text = text;
}
/// <summary>
/// 执行AT指令并返回 成功失败
/// </summary>
/// <param name="ATCmd">AT指令</param>
/// <param name="StCmd">AT指令标准结束标识</param>
/// <returns></returns>
private void ATCommand3(string ATCmd, string StCmd)
{
string response = "";
response = ATCommand(ATCmd, StCmd);
}
/// <summary>
/// 执行AT指令并返回结果字符
/// </summary>
/// <param name="ATCmd">AT指令</param>
/// <param name="StCmd">AT指令标准结束标识</param>
/// <returns></returns>
private string ATCommand(string ATCmd, string StCmd)
{
string response = "";
int i;
if (!ATCmd.EndsWith("\x01a"))
{
if (!(ATCmd.EndsWith("\r") || ATCmd.EndsWith("\r\n")))
{
ATCmd = ATCmd + "\r";
}
}
Sp.WriteLine(ATCmd);
//第一次读响应数据
if (Sp.BytesToRead > 0)
{
response = Sp.ReadExisting();
//去除前端多可能多读取的字符
if (response.IndexOf(ATCmd) > 0)
{
response = response.Substring(response.IndexOf(ATCmd));
}
else
{
}
if (response == "" || response.IndexOf(StCmd) < 0)
{
if (response != "")
{
if (response.Trim() == "ERROR")
{
//throw vError = new UnknowException("Unknown exception in sending command:" + ATCmd);
}
if (response.IndexOf("+CMS ERROR") >= 0)
{
string[] cols = new string[100];
cols = response.Split(\';\');
if (cols.Length > 1)
{
string errorCode = cols[1];
}
}
}
}
}
//读第一次没有读完的响应数据,直到读到特征数据或超时
for (i = 0; i < 3; i++)
{
Thread.Sleep(1000);
response = response + Sp.ReadExisting();
if (response.IndexOf(StCmd) >= 0)
{
break;
}
}
return response;
}
/// <summary>
/// 从注册表获取系统串口列表
/// </summary>
private void GetComList()
{
RegistryKey keyCom = Registry.LocalMachine.OpenSubKey("Hardware\\DeviceMap\\SerialComm");
if (keyCom != null)
{
string[] sSubKeys = keyCom.GetValueNames();
this.comboBox1.Items.Clear();
foreach (string sName in sSubKeys)
{
string sValue = (string)keyCom.GetValue(sName);
this.comboBox2.Items.Add(sValue);
}
}
}
private void cmID_DropDown(object sender, EventArgs e)
{
GetComList();
}
private void Form1_Load_1(object sender, EventArgs e)
{
GetComList();
btPause.Enabled = false;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Sp.Close();
}
private void btPause_Click_1(object sender, EventArgs e)
{
Sp.Close();
btENT.Enabled = true;
btPause.Enabled = false;
}
private void btEnt_Click_1(object sender, EventArgs e)
{
if ((this.comboBox2.Text.Trim() != "") && (this.comboBox1.Text != ""))
{
interfaceUpdataHandle = new HandleInterfaceUpdataDelegate(UpdateTextBox);//实例化委托对象
Sp.PortName = this.comboBox2.Text.Trim();
Sp.BaudRate = Convert.ToInt32(this.comboBox1.Text.Trim());
Sp.Parity = Parity.None;
Sp.StopBits = StopBits.One;
Sp.DataReceived += new SerialDataReceivedEventHandler(Sp_DataReceived);
Sp.ReceivedBytesThreshold = 1;
try
{
Sp.Open();
ATCommand3("AT+CLIP=1\r", "OK");
btPause.Enabled = true;
btENT.Enabled = false;
}
catch
{
MessageBox.Show("端口" + this.comboBox2.Text.Trim() + "打开失败!");
}
}
else
{
MessageBox.Show("请输入正确的端口号和波特率!");
this.comboBox2.Focus();
}
}
}
}
软件开发之后怎么使用哪?
1,打开Virtual Serial Port Kit
2,增加com串口
3,打开ComDebug.exe并设置刚才添加的串口com 填入发送的数据 点击打开端口
4,打开开发的软件
5,点击comdebug.exe 的发送
6,如果没错的话我们开发的demo就应该收到comdebug.exe发送的数据了
文章引用:http://www.qishunwang.net/knowledge_show_151.aspx