1.硬件:条码电子秤
2.需要厂家提供ddl工具包完成下发和回收数据
我这边使用的是DhScalePluNet.dll包
需求描述:
自动定时完成数据拉取,拉取数据后调用服务器接口写入数据库;服务器同时可以设置热键商品,然后通过小程序下发到条码秤(本c#小程序工具依靠第三方提供的ddl,第三方ddl依赖于txt文件做中转下发和回收,我这边也是对txt文件读取和写入数据)
首先上图看下小程序工具:
本工具可以完成自动和手动拉取下发数据:
1.第三方函数导入:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
namespace BarCodeScale
{
class OtherCar
{
[DllImport("DhScalePluNet.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
public static extern int dhSendPluDefault();
[DllImport("DhScalePluNet.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
public static extern int dhRcvRdDefault();
}
}
二.页面回收数据按钮操作:
private void button2_Click(object sender, EventArgs e)
{
try
{
OtherCar.dhRcvRdDefault();
string defaultPath = System.IO.Directory.GetCurrentDirectory().ToString();
Console.WriteLine(defaultPath + "\\record.txt");
byte[] byData = new byte[100*100];
char[] charData = new char[1024*1024];
FileStream file = new FileStream(defaultPath + "\\record.txt", FileMode.Open);
file.Seek(0, SeekOrigin.Begin);
file.Read(byData, 0, 100*100);
//byData传进来的字节数组,用以接受FileStream对象中的数据,第2个参数是字节数组中开始写入数据的位置,它通常是0,表示从数组的开端文件中向数组写数据,最后一个参数规定从文件读多少字符.
Decoder d = Encoding.Default.GetDecoder();
d.GetChars(byData, 0, byData.Length, charData, 0);
string line = new string(charData);
file.Close();
string result= line.Replace("\n", ",");
string[] sArray = Regex.Split(result, ",", RegexOptions.IgnoreCase);
foreach (string str in sArray) {
string res=Regex.Replace(str, @"\s", ";");
string nowStr=res.Replace("\0", "");
if (!string.IsNullOrEmpty(nowStr))
{
//发送数据到服务器
nowStr = nowStr.TrimEnd(';');
nowStr = nowStr.TrimEnd(';');
string[] array =Regex.Split(nowStr, ";", RegexOptions.IgnoreCase);
string ip = array[0];
string date = array[1];
string org = array[2];
string orgDet = array[3];
string plu = array[4];
string weight = array[5];
string money = array[6];
string zheQ = array[7];
string zheH = array[8];
Console.WriteLine("ip:"+ ip+ "日期:" + date + "plu编号:" + plu+"重量:"+weight+"单价:"+money+"总价:"+ zheH);
Dictionary<string, string> param = new Dictionary<string, string>();
param.Add("ip", ip);
param.Add("date", date);
param.Add("org", org);
param.Add("orgDet", orgDet);
param.Add("plu", plu);
param.Add("weight", weight);
param.Add("money", money);
param.Add("zheQ", zheQ);
param.Add("zheH", zheH);
HttpUtils.PostJson("http://localhost:66/mi/barCodeScale/add", param);
}
}
}
catch (Exception ex)
{
Console.WriteLine("回收失败");
}
}
3.下发数据按钮:
private void button1_Click(object sender, EventArgs e)
{
try {
String appDir = System.AppDomain.CurrentDomain.BaseDirectory + @"PLU\" + "\\tma07.txt";
FileStream stream = File.Open(appDir, FileMode.OpenOrCreate, FileAccess.Write);
stream.Seek(0, SeekOrigin.Begin);
stream.SetLength(0);
stream.Close();
string str1 = "1P0001A2281080001100000000000800000000000000000000000000000000000000000000000000##美国苹果";
string str2 = "1P0002A2280081001050000000008020000000000000000000000000000000000000000000000000##美国香蕉";
string str3 = "1P0003A2290082001800001020310030000000000000000000000000000000000000000000000000##美国猕猴桃";
string str = str1 + "\n" + str2 + "\n" + str3;
StreamWriter sw = new StreamWriter(appDir, true,Encoding.GetEncoding("gb2312"));//true是追加false是覆盖
sw.WriteLine(str1);
sw.WriteLine(str2);
sw.WriteLine(str3);
sw.Close();
OtherCar.dhSendPluDefault();
MessageBox.Show("下发成功");
}
catch (Exception ex) {
MessageBox.Show("下发失败");
}
}
4.定时自动回收条码秤数据:
private void Form1_Load(object sender, EventArgs e)
{
System.Timers.Timer pTimer = new System.Timers.Timer(10000);//每隔10秒执行一次,没用winfrom自带的
pTimer.Elapsed += pTimer_Elapsed;//委托,要执行的方法
pTimer.AutoReset = true;//获取该定时器自动执行
pTimer.Enabled = true;//这个一定要写,要不然定时器不会执行的
Control.CheckForIllegalCrossThreadCalls = false;
}
private void pTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
button2_Click(null, null);
}
5.发送http请求到服务器(这里是发送的json字符串)
public static string PostJson(string _url, Dictionary<string, string> dic) {
string jsonParam = JsonConvert.SerializeObject(dic);
var request = (HttpWebRequest)WebRequest.Create(_url);
request.Method = "POST";
request.ContentType = "application/json;charset=UTF-8";
byte[] byteData = Encoding.UTF8.GetBytes(jsonParam);
int length = byteData.Length;
request.ContentLength = length;
Stream writer = request.GetRequestStream();
writer.Write(byteData, 0, length);
writer.Close();
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")).ReadToEnd();
return responseString.ToString();
}
6.bebug包下的文档信息: