C# POS客户显示器通信代码
★源代码
using System;
using System.Collections.Generic;
using System.Text;
using System.IO.Ports;
namespace CommFunc
{
/*
*拍档客户显示器CD7110 rs-232C 接口
*波特率9600bps
*数据位8位 奇偶校验:无 停止位:握手方式:RTS/CTS
*到网址:http://www.doc88.com/p-307515526153.html 查看说明书指令
*/
/// <summary>
/// POS客显:客显屏幕一般来说只能显示数字和小数点 拍档客户显示器CD7110
/// </summary>
public class CustomerDisplay
{
#region 成员变量
private string spPortName;
private int spBaudRate;
private StopBits spStopBits;
private int spDataBits;
private CustomerDispiayType dispiayType;
#endregion --成员变量
#region 属性
/// <summary>
/// 客显发送类型
/// </summary>
public CustomerDispiayType DispiayType
{
get { return dispiayType; }
set { dispiayType = value; }
}
#endregion --属性
#region 构造函数
/// <summary>
/// 构造函数
/// </summary>
/// <param name="_spPortName">端口名称(COM1,COM2,COM3...)</param>
/// <param name="_spBaudRate">通信波特率(2400,9600....)</param>
/// <param name="_spStopBits">停止位</param>
/// <param name="_spDataBits">数据位</param>
public CustomerDisplay(string _spPortName, int _spBaudRate, StopBits _spStopBits, int _spDataBits)
{
this.spBaudRate = _spBaudRate;
this.spDataBits = _spDataBits;
this.spPortName = _spPortName;
this.spStopBits = _spStopBits;// (StopBits)(Enum.Parse(typeof(StopBits), _spStopBits));
}
#endregion --构造函数
#region Method
#region 公共方法
/// <summary>
/// 数据信息展现
/// </summary>
/// <param name="data">发送的数据(清屏可以为null或者空)</param>
public void DisplayData(string data)
{
SerialPort serialPort = new SerialPort();
serialPort.PortName = spPortName;
serialPort.BaudRate = spBaudRate;
serialPort.StopBits = spStopBits;
serialPort.DataBits = spDataBits;
if (!serialPort.IsOpen)
{
serialPort.Open();
}
//先清屏
serialPort.WriteLine(((char)12).ToString());
//指示灯
string str = ((char)27).ToString() + ((char)115).ToString() + ((char)(int)this.DispiayType).ToString() + (char)1;
serialPort.WriteLine(str);
//发送数据
if (!string.IsNullOrEmpty(data))
{
serialPort.WriteLine(((char)27).ToString() + ((char)81).ToString() + ((char)65).ToString() + data + ((char)13).ToString());
}
serialPort.Close();
}
/// <summary>
/// 数据信息展现的另一种方法
/// </summary>
/// <param name="data">要发送的数据:decimal类型转换的字符串</param>
/// <param name="_displayType">客户显示屏显示的类型</param>
public void DisplayData(string data, CustomerDispiayType _displayType)
{
SerialPort serialPort = new SerialPort();
serialPort.PortName = spPortName;
serialPort.BaudRate = spBaudRate;
serialPort.StopBits = spStopBits;
serialPort.DataBits = spDataBits;
//if (!serialPort.IsOpen)
//{
serialPort.Open();
//}
//先清屏
serialPort.WriteLine(((char)12).ToString());
//指示灯:27,115,对应的显示类型,1
string str = ((char)27).ToString() + ((char)115).ToString() + ((char)(int)_displayType).ToString() + (char)1;
serialPort.WriteLine(str);
//设置横线的亮
//serialPort.WriteLine(((char)27).ToString() + ((char)116).ToString() + (char)1);
//发送数据
if (!string.IsNullOrEmpty(data))
{
serialPort.WriteLine(data);
//serialPort.WriteLine(((char)27).ToString() + ((char)81).ToString() + ((char)65).ToString() + data + ((char)13).ToString());
}
serialPort.Close();
}
#endregion --公共方法
#endregion --Method
}
/*
*当n=0 , 四种字符全暗。
*当n=1, “多谢惠顾”字符亮,其它三种暗。
*当n=2, “单价”字符亮,其它三种暗。
*当n=3, “合计”字符亮,其它三种暗。
*当n=4, “收款”字符亮,其它三种暗
*当n=5, “找零”字符亮,其它三种暗
*/
/// <summary>
/// 客显类型
/// </summary>
public enum CustomerDispiayType
{
/// <summary>
/// 清屏,多谢惠顾
/// </summary>
Clear = 1,
/// <summary>
/// 单价
/// </summary>
Price = 2,
/// <summary>
/// 合计
/// </summary>
Total = 3,
/// <summary>
/// 收款
/// </summary>
Receive = 4,
/// <summary>
/// 找零
/// </summary>
Change = 5
}
}
★测试代码及说明
CustomerDisplay display = new CustomerDisplay("COM1", 9600, System.IO.Ports.StopBits.One, 8);
调用方法1:
decimal price = 32.8m;
display.DisplayData(price.ToString("N2"), CustomerDispiayType.Price);
调用方法2:
decimal total = 123.40m;
display.DispiayType = CustomerDispiayType.Total;
display.DisplayData(total.ToString("N2"));