码迷,mamicode.com
首页 > 其他好文 > 详细

电子称 弹钱箱脉冲

时间:2015-08-11 13:39:16      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:

try
{

if (this.serialPort1.IsOpen)
{
Thread.Sleep(300);

      int num = port1.BytesToRead;
      char[] readchar = new char[num];
      port1.Read(readchar, 0, num);
      string str="";
      for (int i = 0; i < 4; i++)//readchar.Length 4位 读取前4位 还有6位的情况
      {
      str += readchar[i].ToString();
      }

      return str;

}
}
catch (Exception ex)
{

}

public double GetWeight()
{
//权限
//明细
//是否可以称重
try
{
Posset posset = SystemConfig.getPosset();
Printerset printerSet = pl.GetPrintersetList(posset.PosCode).First();
SerialPort sPort = new SerialPort(printerSet.scalesComPort, printerSet.scalesBaudRate.Value, Parity.None, printerSet.scalesDataBits.Value, (StopBits)printerSet.scalesStopBits.Value);
serialPortControl = new SerialPortControl(sPort, port1_datareceived);
serialPortControl.OpenPort();
serialPortControl.SendCommand("$");
Stools.MyLognet.WriteInfo(weight.ToString());
return weight;
}
catch (Exception ex)
{
Stools.MyLognet.WriteInfo("打开端口失败" + ex.Message+DateTime.Now.ToString());
ModernDialog.ShowMessage("打开端口失败", "提示", System.Windows.MessageBoxButton.OK);
return 0;
}
}

private void port1_datareceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
SerialPort port1 = serialPortControl.port1;
StringBuilder currentline = new StringBuilder();
if (port1.IsOpen)
{
Thread.Sleep(500);
while (port1.BytesToRead > 0)
{
char ch = (char)port1.ReadByte();
currentline.Append(ch);
}
string[] currentWeight = currentline.ToString().Split(‘ ‘);
if (!string.IsNullOrEmpty(currentWeight[1]))
{
weight =ArithUtil.div(Double.Parse(currentWeight[1]),1000);
// Stools.MyLognet.WriteInfo(weight.ToString());
}
else
{
return;
}
}
serialPortControl.ClosePort();
}
catch (Exception ex)
{
Stools.MyLognet.WriteInfo("电子秤读取失败" + ex.Message + DateTime.Now.ToString());
ModernDialog.ShowMessage("电子秤读取失败", "提示", System.Windows.MessageBoxButton.OK);
}
finally {
serialPortControl.ClosePort();
}
}

电子称 弹钱箱脉冲

标签:

原文地址:http://www.cnblogs.com/pengde/p/4720512.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!