各位大神们,看见小弟写的博客可不要笑话,小弟也是第一次写博客,初来乍到多多指教,本人也是还没毕业的学生,在一家公司进行实习,实习5个月了,公司主要是做管理系统,跟硬件也有一些接触,一个星期以前老板安排让我做一个WinCE的系统,扫描枪用蓝牙连接打印机打印一维码和Vin码,当时一听这个任务就给我吓蒙圈了,还是实习生刚毕业的我来说还是很有难度的,从开始的demo做起,用扫描枪扫描商品码,通过蓝牙连接打印机,控制打印机打印所扫出来的条形码对WinCE一个星期的接触觉得自己对这个东西还是蛮感兴趣,因为个人比较喜欢玩硬件,这个任务刚好有硬件可以玩,下面给大家分享一下这一个星期所遇到的问题。(此文章纯属笔录,大神看见不要喷)
1、第1个问题就是所编写的程序不能正常在WinCE扫描枪上运行,始终报错无法找到Device.dll文件。
解决方案:询问供应商所给的文件有问题。
2、第2个问题无法解决扫描时的声音
解决方案:创建类Sound
public class Sound
{
private byte[] m_soundBytes;
private string m_fileName;
private enum Flags
{
SND_SYNC = 0x0000, /* play synchronously (default) */
SND_ASYNC = 0x0001, /* play asynchronously */
SND_NODEFAULT = 0x0002, /* silence (!default) if sound not found */
SND_MEMORY = 0x0004, /* pszSound points to a memory file */
SND_LOOP = 0x0008, /* loop the sound until next sndPlaySound */
SND_NOSTOP = 0x0010, /* don't stop any currently playing sound */
SND_NOWAIT = 0x00002000, /* don't wait if the driver is busy */
SND_ALIAS = 0x00010000, /* name is a registry alias */
SND_ALIAS_ID = 0x00110000, /* alias is a predefined ID */
SND_FILENAME = 0x00020000, /* name is file name */
SND_RESOURCE = 0x00040004 /* name is resource name or atom */
}
[DllImport("CoreDll.DLL", EntryPoint = "PlaySound", SetLastError = true)]
private extern static int WCE_PlaySound(string szSound, IntPtr hMod, int flags);
[DllImport("CoreDll.DLL", EntryPoint = "PlaySound", SetLastError = true)]
private extern static int WCE_PlaySoundBytes(byte[] szSound, IntPtr hMod, int flags);
///
/// Construct the Sound object to play sound data from the specified file.
///
public Sound(string fileName)
{
m_fileName = fileName;
}
///
/// Construct the Sound object to play sound data from the specified stream.
///
public Sound(Stream stream)
{
// read the data from the stream
m_soundBytes = new byte[stream.Length];
stream.Read(m_soundBytes, 0, (int)stream.Length);
}
///
/// Play the sound
///
public void Play()
{
// if a file name has been registered, call WCE_PlaySound,
// otherwise call WCE_PlaySoundBytes
if (m_fileName != null)
WCE_PlaySound(m_fileName, IntPtr.Zero, (int)(Flags.SND_ASYNC | Flags.SND_FILENAME));
else
WCE_PlaySoundBytes(m_soundBytes, IntPtr.Zero, (int)(Flags.SND_ASYNC | Flags.SND_MEMORY));
}
}
运用方法: //播放声音
Sound sound = new Sound(@"Scan.wav");
sound.Play();
3、蓝牙打印机PCL语言指令集
//打印条码
void Print_Car()
{
string MM1 = "";
string MM2 = "";
string MM3 = "";
string MM4 = "";
string MM5 = "";
string MM6 = "";
string MM7 = "";
string MM8 = "";
string MM_ALL = "";
MM1 = this.txt_Car1.Text.Trim();
MM2 = this.txt_Car2.Text.Trim();
MM3 = this.txt_Car3.Text.Trim();
MM4 = this.txt_Car4.Text.Trim();
MM5 = this.txt_Car5.Text.Trim();
MM6 = this.txt_Car6.Text.Trim();
MM7 = this.txt_Car7.Text.Trim();
MM8 = this.txt_Car8.Text.Trim();
MM_ALL = this.txt_Car_All.Text.Trim();
string CPCLStr = "! 0 200 200 ALL 1" + "\r\n";
string CPCLStr1 = "PW 575" + "\r\n" +
"TONE 0" + "\r\n" +
"SPEED 3" + "\r\n" +
"ON-FEED IGNORE" + "\r\n" +
"NO-PACE" + "\r\n" +
"BAR-SENSE" + "\r\n" +
"ENDQR" + "\r\n";
string CPCLStr2 = "B 128 1 30 55 WB HB Qty1" + "\r\n" +
"BT OFF" + "\r\n" +
"T 4 0 WT HT Qty1" + "\r\n";
string CPCLStr3 = "B 128 1 30 55 WB HB Qty_ALL" + "\r\n" +
"BT OFF" + "\r\n" +
"T 4 0 WT HT Qty_ALL" + "\r\n";
string CPCLStr4 = "PRINT " + "\r\n";
string all = "";
string TM = "";
for (int i = 0; i < 8; i++)
{
string P = "220";
string WB = "100";
string WT = "95";
string HB = "0";
string HT = "50";
if (i == 0)
{
if (MM1 != "")
{
CPCLStr = CPCLStr.Replace("ALL", P);
string temp = "";
temp = CPCLStr2;
temp = temp.Replace("Qty1", MM1).Replace("WT", WT).Replace("WB", WB).Replace("HB", HB).Replace("HT", HT);
TM = CPCLStr3;
TM = TM.Replace("WB", "230").Replace("WT", "225").Replace("HB", "120").Replace("HT", "170").Replace("Qty_ALL", MM_ALL);
all = temp;
}
}
else if (i == 1)
{
if (MM2 != "")
{
CPCLStr = CPCLStr.Replace("ALL", P);
string temp = "";
temp = CPCLStr2;
temp = temp.Replace("WB", "350").Replace("WT", "345").Replace("HB", "0").Replace("HT", "50").Replace("Qty1", MM2);
TM = CPCLStr3;
TM = TM.Replace("WB", "230").Replace("WT", "225").Replace("HB", "120").Replace("HT", "170").Replace("Qty_ALL", MM_ALL);
all = all + temp;
}
}
else if (i == 2)
{
if (MM3 != "")
{
CPCLStr = CPCLStr.Replace("220", "340");
string temp = "";
temp = CPCLStr2;
temp = temp.Replace("WB", "100").Replace("WT", "95").Replace("HB", "120").Replace("HT", "170").Replace("Qty1", MM3);
TM = CPCLStr3;
TM = TM.Replace("WB", "230").Replace("WT", "225").Replace("HB", "240").Replace("HT", "290").Replace("Qty_ALL", MM_ALL);
all = all + temp;
}
}
else if (i == 3)
{
if (MM4 != "")
{
CPCLStr = CPCLStr.Replace("220", "340");
string temp = "";
temp = CPCLStr2;
temp = temp.Replace("WB", "350").Replace("WT", "345").Replace("HB", "120").Replace("HT", "170").Replace("Qty1", MM4);
TM = CPCLStr3;
TM = TM.Replace("WB", "230").Replace("WT", "225").Replace("HB", "240").Replace("HT", "290").Replace("Qty_ALL", MM_ALL);
all = all + temp;
}
}
else if (i == 4)
{
if (MM5 != "")
{
CPCLStr = CPCLStr.Replace("340", "460");
string temp = "";
temp = CPCLStr2;
temp = temp.Replace("WB", "100").Replace("WT", "95").Replace("HB", "240").Replace("HT", "290").Replace("Qty1", MM5);
TM = CPCLStr3;
TM = TM.Replace("WB", "230").Replace("WT", "225").Replace("HB", "360").Replace("HT", "410").Replace("Qty_ALL", MM_ALL);
all = all + temp;
}
}
else if (i == 5)
{
if (MM6 != "")
{
CPCLStr = CPCLStr.Replace("340", "460");
string temp = "";
temp = CPCLStr2;
temp = temp.Replace("WB", "350").Replace("WT", "345").Replace("HB", "240").Replace("HT", "290").Replace("Qty1", MM6);
TM = CPCLStr3;
TM = TM.Replace("WB", "230").Replace("WT", "225").Replace("HB", "360").Replace("HT", "410").Replace("Qty_ALL", MM_ALL);
all = all + temp;
}
}
else if (i == 6)
{
if (MM7 != "")
{
CPCLStr = CPCLStr.Replace("460", "580");
string temp = "";
temp = CPCLStr2;
temp = temp.Replace("WB", "100").Replace("WT", "95").Replace("HB", "360").Replace("HT", "410").Replace("Qty1", MM7);
TM = CPCLStr3;
TM = TM.Replace("WB", "230").Replace("WT", "225").Replace("HB", "480").Replace("HT", "530").Replace("Qty_ALL", MM_ALL);
all = all + temp;
}
}
else if (i == 7)
{
if (MM8 != "")
{
CPCLStr = CPCLStr.Replace("460", "580");
string temp = "";
temp = CPCLStr2;
temp = temp.Replace("WB", "350").Replace("WT", "345").Replace("HB", "360").Replace("HT", "410").Replace("Qty1", MM8);
TM = CPCLStr3;
TM = TM.Replace("WB", "230").Replace("WT", "225").Replace("HB", "480").Replace("HT", "530").Replace("Qty_ALL", MM_ALL);
all = all + temp;
}
}
}
string Both = CPCLStr + CPCLStr1 + all + TM + CPCLStr4;
byte[] bytes = Encoding.ASCII.GetBytes(Both);
bth.Port.Write(bytes, 0, bytes.Length);
4、穿件日志文件
创建LogerHelper.cs类文件
public static void WriteLog(String log)
{
//在CE中获取程序所在目录
string FileDir = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\lg\\";
if (!Directory.Exists(FileDir)) Directory.CreateDirectory(FileDir);
string FileName = FileDir + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
FileName = FileName.Remove(0, 1);// FileName.Substring(1, FileName.Length);
// MessageBox.Show(FileName);
try
{
using (StreamWriter sw = File.AppendText(FileName))
{
sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " --> " + log);
}
}
catch (Exception ex)
{
MessageBox.Show("写条码配置文件有问题!" + ex.Message, "ScanBarcode");
}
}
调用