using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.IO.Ports;
namespace BluetoothTest
{
public class Printer
{
SerialPort server = new SerialPort();
private string _portName = "COM6";//蓝牙一般默认为com6
/// <summary>
/// 获取或设置端口名称
/// </summary>
public string PortName
{
get
{
_portName = server.PortName;
return _portName;
}
set
{
_portName = value;
server.PortName = _portName;
}
}
/// <summary>
/// 端口是否已经打开
/// </summary>
public bool IsOpen
{
get
{
return server.IsOpen;
}
}
/// <summary>
/// 构造方法初始化串口参数
/// </summary>
public Printer()
{//初始化各个参数
server.BaudRate = 9600;//波特率
server.Parity = 0;//校检位
server.DataBits = 8;//数据位
server.StopBits = StopBits.One;//停止位
server.PortName = _portName;//端口名称
server.WriteTimeout = -1;//超时时间
server.ReadTimeout = -1;//超时时间
}
/// <summary>
/// 打开端口
/// </summary>
/// <returns></returns>
public bool OpenPort()
{
try
{
if (!server.IsOpen)
{//关闭的
server.Open();
}
else
{//打开的
//server.Close();
//server.Open();
}
return true;
}
catch(Exception ex)
{ return false; }
}
/// <summary>
/// 发送数据
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public bool SendDataToPort(string str,out string mes)
{
try
{
byte[] OutBuffer;//数据
int BufferSize;
Encoding targetEncoding;
//将[UNICODE编码]转换为[GB码],仅使用于简体中文版mobile
targetEncoding = Encoding.GetEncoding(0); //得到简体中文字码页的编码方式,因为是简体中文操作系统,参数用0就可以,用936也行。
BufferSize = targetEncoding.GetByteCount(str); //计算对指定字符数组中的所有字符进行编码所产生的字节数
OutBuffer = new byte[BufferSize];
OutBuffer = targetEncoding.GetBytes(str); //将指定字符数组中的所有字符编码为一个字节序列,完成后outbufer里面即为简体中文编码
byte[] cmdData = new byte[BufferSize+100];
//初始化打印机
cmdData[0] = 0x1B;
cmdData[1] = 0x40;
//设置字符顺时旋转180度
cmdData[2] = 0x1B;
cmdData[3] = 0x56;
cmdData[4] = 0;
for (int i = 0; i < BufferSize; i++)
{
cmdData[5 + i] = OutBuffer[i];
}
//向打印机发送[GB码]数据
server.Write(cmdData, 0, BufferSize + 5);
//server.WriteLine(str);
//初始化指令1B 40
cmdData[0] = 0x1B;
cmdData[1] = 0x40;
//打印并走纸指令
cmdData[2] = 0x1B;
cmdData[3] = 0x64;
cmdData[4] = 0x02;
server.Write(cmdData, 0, 5);
mes = "";
return true;
}
catch(Exception ex)
{
mes = ex.Message;
return false;
}
}
/// <summary>
/// 十六进制转换字节数组
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
private byte[] HexStringToByteArray(string s)
{
s = s.Replace(" ", "");
byte[] buffer = new byte[s.Length / 2];
for (int i = 0; i < s.Length; i += 2)
buffer[i / 2] = (byte)Convert.ToByte(s.Substring(i, 2), 16);
return buffer;
}
/// <summary>
/// 字节数组转换十六进制
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
private string ByteArrayToHexString(byte[] data)
{
StringBuilder sb = new StringBuilder(data.Length * 3);
foreach (byte b in data)
sb.Append(Convert.ToString(b, 16).PadLeft(2, '0').PadRight(3, ' '));
return sb.ToString().ToUpper();
}
public void ClosePort()
{
server.Close();
}
}
}
3.接下在项目窗体上的使用,看看下面的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace BluetoothTest
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
Printer printer = new Printer();
private void menuItem4_Click(object sender, EventArgs e)
{
//退出
printer.ClosePort();
Application.Exit();
}
private void menuItem2_Click(object sender, EventArgs e)
{
//打印
if (tbxBody.Text.Length == 0)
return;
setEnabled(false);
if (!printer.IsOpen)//只有在端口关闭的时候才能设置
printer.PortName = drpPort.GetItemText(drpPort.SelectedItem);
if (!printer.OpenPort())
{
MessageBox.Show("无法打开端口" + drpPort.GetItemText(drpPort.SelectedItem));
}
string mes = "";
if (!printer.SendDataToPort(tbxBody.Text,out mes))
{
MessageBox.Show("发送数据失败" + drpPort.GetItemText(drpPort.SelectedItem) + " " + mes);
}
setEnabled(true);
}
private void setEnabled(bool b)
{
menuItem1.Enabled = b;
menuItem3.Enabled = b;
}
private void frmMain_Load(object sender, EventArgs e)
{
//初始化
drpPort.SelectedIndex = 1;
}
}
}
---------------------------------------------------------
专注移动开发
Android, Windows Mobile, iPhone, J2ME, BlackBerry, Symbian
posted on 2010-03-23 21:47
TiGERTiAN 阅读(838)
评论(0) 编辑 收藏 所属分类:
Windows Mobile