using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net; using System.Net.Sockets; namespace BarEasyAppCS { // 如有您有任何关于BarEasy或条码打印的相关问题,欢迎沟通 // 联系人:王新 // 手机/微信 13162267443 public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btPrint_Click(object sender, EventArgs e) { //打印 Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPAddress addr = IPAddress.Parse("127.0.0.1"); EndPoint point = new IPEndPoint(addr, 10086); client.Connect(point); String s1 = String.Format(""{0}":"{1}"", "FILE_NAME", "A"); //文件名 String s2 = String.Format(""{0}":"{1}"", "P_参数1", textPara1.Text);//参数1的值 String s3 = String.Format(""{0}":"{1}"", "PRINT_COUNT", txtCount.Value.ToString()); //打印笔数 String s4 = String.Format(""{0}":"{1}"", "PRINT_REPEAT", txtRepeat.Value.ToString());//打印份数 String sAll = "{" + s1 + "," + s2 + "," + s3 + "," + s4 + "}"; client.Send(Encoding.UTF8.GetBytes(sAll)); byte[] data = new byte[1024]; int length = client.Receive(data); string message = Encoding.UTF8.GetString(data, 0, length); textMsg.Text = message; client.Close(); } private void btGet_Click(object sender, EventArgs e) { //获取序号1的当前值 Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPAddress addr = IPAddress.Parse("127.0.0.1"); EndPoint point = new IPEndPoint(addr, 10086); client.Connect(point); String s1 = String.Format(""{0}":"{1}"", "FILE_NAME", "A"); //文件名 String s2 = String.Format(""{0}":"{1}"", "SN_GET_序号1", "");//参数1的值 String sAll = "{" + s1 + "," + s2 + "}"; client.Send(Encoding.UTF8.GetBytes(sAll)); byte[] data = new byte[1024]; int length = client.Receive(data); string message = Encoding.UTF8.GetString(data, 0, length); textMsg.Text = message; if (message.StartsWith("OK,")) textSN1.Text = message.Substring(3); client.Close(); } private void btSet_Click(object sender, EventArgs e) { //设置序号1的当前值 Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPAddress addr = IPAddress.Parse("127.0.0.1"); EndPoint point = new IPEndPoint(addr, 10086); client.Connect(point); String s1 = String.Format(""{0}":"{1}"", "FILE_NAME", "A"); //文件名 String s2 = String.Format(""{0}":"{1}"", "SN_SET_序号1", textSN1.Text);//参数1的值 String sAll = "{" + s1 + "," + s2 + "}"; client.Send(Encoding.UTF8.GetBytes(sAll)); byte[] data = new byte[1024]; int length = client.Receive(data); string message = Encoding.UTF8.GetString(data, 0, length); textMsg.Text = message; client.Close(); } } }
运行界面如下: