1. 程式人生 > 其它 >無人地磅自助機開發總結(五)物理開關,8路繼電器

無人地磅自助機開發總結(五)物理開關,8路繼電器

1.這個開關直接監聽串列埠是沒有用的,需要向串列埠傳送資料,然後再監聽判斷哪一個按鈕被按下

2.開關的配置檔案配置在xml檔案裡

3.讀取xml類

 public class XmlHelper
    {
        public static string GetElementByName(string xmlFileName, string tagName)
        {
            try
            {
                string result = null;
                XmlDocument doc = new
XmlDocument(); doc.Load(xmlFileName); XmlNodeList nodes = doc.GetElementsByTagName(tagName); foreach (XmlNode v in nodes) { result = v.InnerText; } return result; }
catch { throw new Exception("開啟檔案錯誤"); } } }

4.開啟開關串列埠

SerialPort sport = new SerialPort();

   if (sport.IsOpen == true)
            {
                sport.Close();
            }
            string sIniFile = appPath + "wmsconfig" + "\\" + "SwitchSetting.xml
"; if (File.Exists(sIniFile)) { sport.BaudRate = Convert.ToInt32(XmlHelper.GetElementByName(sIniFile, "BaudRate")); sport.DataBits = Convert.ToInt32(XmlHelper.GetElementByName(sIniFile, "DataBits")); sport.StopBits = (StopBits)Convert.ToInt32(XmlHelper.GetElementByName(sIniFile, "StopBits")); sport.Parity = (Parity)Convert.ToInt32(XmlHelper.GetElementByName(sIniFile, "Parity")); var portName = XmlHelper.GetElementByName(sIniFile, "PortName"); if (null != portName) sport.PortName = portName; } sport.BaudRate = 9600; sport.DataBits = 8; sport.StopBits = StopBits.One; //sport.PortName = "COM1"; sport.Parity = Parity.None; try { sport.Open();//開啟串列埠 sport.DtrEnable = true;//設定DTR為高電平 sport.RtsEnable = true;//設定RTS位高電平 } catch (Exception ex) { MessageBox.Show(ex.Message); }

5.寫一個定時器不停的向串列埠傳送資料,監聽串列埠

DispatcherTimer timer;
timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromMilliseconds(1000);
timer.Tick += timer2_Tick;
timer.Start();
sport.DataReceived -= new SerialDataReceivedEventHandler(serialPort_DataReceived);//訂閱委
sport.DataReceived += new SerialDataReceivedEventHandler(serialPort_DataReceived);//訂閱委

 private void timer2_Tick(object sender, EventArgs e)
        {
            if (sport.IsOpen == true)
            {
                strReceiveMessage = "";
                byte[] sendData = new byte[] { 0x21, 0x02, 0x00, 0x00, 0x00, 0x08, 0x7E, 0xAC };
                ////傳送資料
                sport.Write(sendData, 0, sendData.Length);
            }

        }

6.判斷是開始按鈕還是結束按鈕

  //接收按鈕資料事件
        private void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            Thread.Sleep(100);  //(毫秒)等待一定時間,確保資料的完整性 int len        
            int len = sport.BytesToRead;
            string receivedata = string.Empty;
            if (len != 0)
            {
                byte[] buff = new byte[len];
                sport.Read(buff, 0, len);
                for (int i = 0; i < buff.Length; i++)
                {
                    strReceiveMessage += buff[i].ToString("X2");  //16進位制顯示  
                }
                ts2 = new TimeSpan(DateTime.Now.Ticks);
                            
                if (strReceiveMessage!=null)
                {
                    if (strReceiveMessage.Contains("21020104AB8B"))
                    {// MessageBox.Show("開始按鈕");
                      
                    }
         }
       }
    }