嘀嘀刷卡器的屏幕显示文字的参考代码:
/// <summary>
///
/// </summary>
/// <param name="oneRow">显示第一条</param>
/// <param name="twoRow">显示第二条</param>
/// <param name="threeRow">显示第三条</param>
/// <param name="fourRow">显示第四条</param>
/// <param name="socketServer"></param>
public void ShowInfo(string oneRow, string twoRow, string threeRow, string fourRow, Socket socketServer)
{
if (oneRow.Length > 0)
{
oneRow = GetGuoBiaoMaStr("01",oneRow);
}
else
{
oneRow = "0100";
}
if (twoRow.Length > 0)
{
twoRow = GetGuoBiaoMaStr("02",twoRow);
}
else
{
twoRow = "0200";
}
if (threeRow.Length > 0)
{
threeRow = GetGuoBiaoMaStr("03",threeRow);
}
else
{
threeRow = "0300";
}
if (fourRow.Length > 0)
{
fourRow = GetGuoBiaoMaStr("04",fourRow);
}
else
{
fourRow = "0400";
}
int zongshu = ((oneRow.Length + twoRow.Length + threeRow.Length + fourRow.Length) / 2) + 2;
string zongshu1 = zongshu.ToString("X2");
string sendmsg = "4449" + zongshu1 + "00" + oneRow + twoRow + threeRow + fourRow;
byte[] arrSendMsg1 = new byte[sendmsg.Length / 2];
for (int i = 0; i < arrSendMsg1.Length; i++)
{
arrSendMsg1[i] = Convert.ToByte(sendmsg.Substring(i * 2, 2), 16);
}
socketServer.Send(GetXORFull(arrSendMsg1));
}
public string GetGuoBiaoMaStr(string title,string SourceCode)
{
int totallength = 0;
string totalstr = "";
for (int i = 0; i < SourceCode.Length; i++)
{
string code = SourceCode[i].ToString();
string guobiaoma = retmsg(code);
totallength += guobiaoma.Length / 2;
totalstr += guobiaoma;
}
string nozs = totallength.ToString("X2");//.PadLeft(2, '0');
return title + nozs + "01" + totalstr;
}
/// <summary>
/// 返回string国标字符串
/// </summary>
/// <returns></returns>
public static string retmsg(string hanzi)
{
string jinei = null;
string guobiao = null;
byte[] array = System.Text.Encoding.Default.GetBytes(hanzi);
if (array.Length % 2 == 0)
{
for (int i = 0; i < array.Length; i = i + 2)
{
if ((int)array[i] > 127)
{
jinei = Convert.ToString(array[i], 16) + Convert.ToString(array[i + 1], 16);
}
else
{
if (i == 0)
{
jinei = array[i].ToString("X2");
}
else
{
int isa = i - 1;
if ((int)array[isa] > 127)
{
jinei = array[i].ToString("X2");
}
else
{
jinei = array[isa].ToString("X2") + array[i].ToString("X2");
}
}
}
guobiao += jinei;
}
if ((int)array[array.Length - 1] < 128)
{
jinei = array[array.Length - 1].ToString("X2");
guobiao += jinei;
}
}
else
{
int count = array.Length + 1;
for (int i = 0; i < count; i = i + 2)
{
if ((int)array[i] > 127)
{
jinei = Convert.ToString(array[i], 16) + Convert.ToString(array[i + 1], 16);
}
else
{
if (i == 0)
{
jinei = array[i].ToString("X2");
}
else
{
int isa = i - 1;
if ((int)array[isa] > 127)
{
jinei = array[i].ToString("X2");
}
else
{
jinei = array[isa].ToString("X2") + array[i].ToString("X2");
}
}
}
guobiao += jinei;
}
}
string guobiaoda = guobiao.ToUpper();
return guobiaoda;
}
/// <summary>
/// strEncode 需要转化的原始字符串
/// 转换的过程是直接把字符转换成Unicode字符,比如数字"3"-->0033,汉字"我"-->U+6211
/// 函数decode的过程是encode的逆过程.
/// </summary>
/// <param name="strEncode"></param>
/// <returns></returns>
public static string Encode(string strEncode)
{
string strReturn = "";// 存储转换后的编码
foreach (short shortx in strEncode.ToCharArray())
{
strReturn += shortx.ToString("X2");
}
return strReturn;
}
/// <summary>
/// 将一条十六进制字符串转换为ASCII
/// </summary>
/// <param name="hexstring">一条十六进制字符串</param>
/// <returns>返回一条ASCII码</returns>
public static string HexStringToASCII(string hexstring)
{
List<byte> buffer = new List<byte>();
for (int i = 0; i < hexstring.Length; i += 2)
{
string temp = hexstring.Substring(i, 2);
if (temp != "00")
{
byte value = Convert.ToByte(temp, 16);
buffer.Add(value);
}
}
string sValue = System.Text.Encoding.ASCII.GetString(buffer.ToArray());
return sValue;
}
/// <summary>
/// 时间戳转为格式时间
/// </summary>
/// <returns></returns>
public DateTime StampToDateTime(string timeStamp)
{
DateTime dateTimeStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
long lTime = long.Parse(timeStamp + "0000000");
TimeSpan toNow = new TimeSpan(lTime);
return dateTimeStart.Add(toNow);
}
/// <summary>
/// 发送信息到客户端的方法
/// </summary>
/// <param name="sendMsg">发送的字符串信息</param>
public void ServerSendMsgInf(string sendMsg)
{
//将输入的字符串转换成 机器可以识别的字节数组
// byte[] arrSendMsg = Encoding.UTF8.GetBytes(sendMsg);
byte[] arrSendMsg1 = new byte[sendMsg.Length / 2];
for (int i = 0; i < arrSendMsg1.Length; i++)
{
arrSendMsg1[i] = Convert.ToByte(sendMsg.Substring(i * 2, 2), 16);
}
//向客户端发送字节数组信息
socConnection.Send(GetXORFull(arrSendMsg1));
}
/// <summary>
/// 发送信息到客户端的方法
/// </summary>
/// <param name="sendMsg">发送的字符串信息</param>
public void ServerSendMsg(string sendMsg)
{
//将输入的字符串转换成 机器可以识别的字节数组
// byte[] arrSendMsg = Encoding.UTF8.GetBytes(sendMsg);
byte[] arrSendMsg1 = new byte[sendMsg.Length / 2];
for (int i = 0; i < arrSendMsg1.Length; i++)
{
arrSendMsg1[i] = Convert.ToByte(sendMsg.Substring(i * 2, 2), 16);
}
//向客户端发送字节数组信息
socConnection.Send(arrSendMsg1);
}
/// <summary>
/// 异或校验码
/// </summary>
/// <param name="Cmd"></param>
/// <returns></returns>
public static byte[] GetXORFull(byte[] Cmd)
{
byte check = GetXOR(Cmd);
List<byte> newCmd = new List<byte>();
newCmd.AddRange(Cmd);
newCmd.Add(check);
return newCmd.ToArray();
}
/// <summary>
/// 计算按位异或校验和(返回校验和值)
/// </summary>
/// <param name="Cmd">命令数组</param>
/// <returns>校验和值</returns>
public static byte GetXOR(byte[] Cmd)
{
byte check = (byte)(Cmd[0] ^ Cmd[1]);
for (int i = 2; i < Cmd.Length; i++)
{
check = (byte)(check ^ Cmd[i]);
}
return check;
}
官方只有 C#版本的代码,如果需要java版本的代码,可以联系我。