172 lines
6.1 KiB
C#
172 lines
6.1 KiB
C#
|
using DM_Weight.Models;
|
|||
|
using log4net;
|
|||
|
using SuperSimpleTcp;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.IO;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Channels;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using System.Windows.Markup;
|
|||
|
|
|||
|
namespace DM_Weight.Port
|
|||
|
{
|
|||
|
public class ScreenUtil
|
|||
|
{
|
|||
|
private readonly ILog logger = LogManager.GetLogger(typeof(ScreenUtil));
|
|||
|
SimpleTcpServer server = new SimpleTcpServer("0.0.0.0:8888");
|
|||
|
|
|||
|
string ipport { get; set; } = "";
|
|||
|
public ScreenUtil()
|
|||
|
{
|
|||
|
server.Events.ClientConnected += (object sender, ConnectionEventArgs e) =>
|
|||
|
{
|
|||
|
ipport = e.IpPort;
|
|||
|
logger.Info($"[{e.IpPort}] client connected");
|
|||
|
};
|
|||
|
server.Events.ClientDisconnected += (object sender, ConnectionEventArgs e) =>
|
|||
|
{
|
|||
|
logger.Info($"[{e.IpPort}] client disconnected: {e.Reason}");
|
|||
|
};
|
|||
|
server.Events.DataReceived += (object sender, DataReceivedEventArgs e) =>
|
|||
|
{
|
|||
|
logger.Info($"[{e.IpPort}]: {e.Data.Array}");
|
|||
|
};
|
|||
|
// let's go!
|
|||
|
server.Start();
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
private byte[] head = new byte[] { 0xee, 0xb1, 0x12 };
|
|||
|
|
|||
|
private byte[] end = new byte[] { 0xFF, 0xFC, 0xFF, 0xFF };
|
|||
|
public void SetStockInfo(ChannelStock channel, int screenId)
|
|||
|
{
|
|||
|
// 添加画面id
|
|||
|
byte[] a = Copy2NewArr(head, HighLow(screenId));
|
|||
|
|
|||
|
|
|||
|
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
|
|||
|
//if (channel.DrugInfo.DrugName != null)
|
|||
|
//{
|
|||
|
byte[] content = Encoding.GetEncoding("gb2312").GetBytes(channel.DrugInfo?.DrugName??"");
|
|||
|
a = WriteInfo(a, content, channel.ColNo * 10 + 1);
|
|||
|
//}
|
|||
|
//if (channel.DrugInfo.Manufactory != null)
|
|||
|
//{
|
|||
|
byte[] content1 = Encoding.GetEncoding("gb2312").GetBytes(channel.DrugInfo?.Manufactory?? "");
|
|||
|
a = WriteInfo(a, content1, channel.ColNo * 10 + 2);
|
|||
|
//}
|
|||
|
//if (channel.DrugInfo.DrugSpec != null)
|
|||
|
//{
|
|||
|
byte[] content2 = Encoding.GetEncoding("gb2312").GetBytes(channel.DrugInfo?.DrugSpec ?? "");
|
|||
|
a = WriteInfo(a, content2, channel.ColNo * 10 + 3);
|
|||
|
//}
|
|||
|
//if (channel.ManuNo != null)
|
|||
|
//{
|
|||
|
|
|||
|
byte[] content3 = Encoding.GetEncoding("gb2312").GetBytes(channel.ManuNo);
|
|||
|
a = WriteInfo(a, content3, channel.ColNo * 10 + 4);
|
|||
|
//}
|
|||
|
//if (channel.EffDate != null)
|
|||
|
//{
|
|||
|
byte[] content4 = Encoding.GetEncoding("gb2312").GetBytes(channel.EffDate);
|
|||
|
a = WriteInfo(a, content4, channel.ColNo * 10 + 5);
|
|||
|
//}
|
|||
|
//if (channel.Quantity != null)
|
|||
|
//{
|
|||
|
byte[] content5 = Encoding.GetEncoding("gb2312").GetBytes(channel.Quantity.ToString());
|
|||
|
a = WriteInfo(a, content5, channel.ColNo * 10 + 6);
|
|||
|
//}
|
|||
|
// 添加结束块
|
|||
|
byte[] b = Copy2NewArr(a, end);
|
|||
|
server.Send(ipport, b);
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
public void TellChange(int d)
|
|||
|
{
|
|||
|
byte[] buffer = new byte[] { 0xee, 0xb5, 0x01, 0x00, 0xFF, 0xFC, 0xFF, 0xFF };
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
public void SetStockInfo(List<ChannelStock> channels, int screenId)
|
|||
|
{
|
|||
|
// 添加画面id
|
|||
|
byte[] a = Copy2NewArr(head, HighLow(screenId));
|
|||
|
|
|||
|
|
|||
|
for (int i = 0; i < channels.Count; i++)
|
|||
|
{
|
|||
|
ChannelStock channel = channels[i];
|
|||
|
if (channel.DrugInfo.DrugName != null)
|
|||
|
{
|
|||
|
byte[] content = Encoding.ASCII.GetBytes(channel.DrugInfo.DrugName);
|
|||
|
a = WriteInfo(a, content, channels[0].DrawerNo * 10 + 1);
|
|||
|
}
|
|||
|
if (channel.DrugInfo.Manufactory != null)
|
|||
|
{
|
|||
|
byte[] content = Encoding.ASCII.GetBytes(channel.DrugInfo.Manufactory);
|
|||
|
a = WriteInfo(a, content, channels[0].DrawerNo * 10 + 2);
|
|||
|
}
|
|||
|
if (channel.DrugInfo.DrugSpec != null)
|
|||
|
{
|
|||
|
byte[] content = Encoding.ASCII.GetBytes(channel.DrugInfo.DrugSpec);
|
|||
|
a = WriteInfo(a, content, channels[0].DrawerNo * 10 + 3);
|
|||
|
}
|
|||
|
if (channel.ManuNo != null)
|
|||
|
{
|
|||
|
|
|||
|
byte[] content = Encoding.ASCII.GetBytes(channel.ManuNo);
|
|||
|
a = WriteInfo(a, content, channels[0].DrawerNo * 10 + 4);
|
|||
|
}
|
|||
|
if (channel.EffDate != null)
|
|||
|
{
|
|||
|
byte[] content = Encoding.ASCII.GetBytes(channel.EffDate);
|
|||
|
a = WriteInfo(a, content, channels[0].DrawerNo * 10 + 5);
|
|||
|
}
|
|||
|
if (channel.Quantity != null)
|
|||
|
{
|
|||
|
byte[] content = Encoding.ASCII.GetBytes(channel.Quantity.ToString());
|
|||
|
a = WriteInfo(a, content, channels[0].DrawerNo * 10 + 6);
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
}
|
|||
|
// 添加结束块
|
|||
|
byte[] b = Copy2NewArr(a, end);
|
|||
|
server.Send(ipport, b);
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
public byte[] WriteInfo(byte[] a, byte[] content, int id)
|
|||
|
{
|
|||
|
// 添加控件id
|
|||
|
byte[] b = Copy2NewArr(a, HighLow(id));
|
|||
|
// 添加需要向控件内写入的字节长度
|
|||
|
byte[] c = Copy2NewArr(b, HighLow(content.Length));
|
|||
|
// 写入内容
|
|||
|
return Copy2NewArr(c, content);
|
|||
|
}
|
|||
|
|
|||
|
public byte[] HighLow(int data)
|
|||
|
{
|
|||
|
byte high = Convert.ToByte((data >> 8) & 0x00ff); //位运算:右移8位
|
|||
|
byte low = Convert.ToByte(data & 0x00ff); //去掉高位
|
|||
|
return new byte[] {high, low};
|
|||
|
}
|
|||
|
|
|||
|
public byte[] Copy2NewArr(byte[] source1, byte[] source2)
|
|||
|
{
|
|||
|
byte[] target = new byte[source1.Length + source2.Length];
|
|||
|
Buffer.BlockCopy(source1, 0, target, 0, source1.Length);
|
|||
|
Buffer.BlockCopy(source2, 0, target, source1.Length, source2.Length);
|
|||
|
return target;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
}
|
|||
|
}
|