托利多電子秤硬件接口開發心得


   做erp總是免不了與各種硬件設備做對接,本人做畜牧行業erp。領導給分配了一個需求,就是開發托利多t800電子秤接口與erp對接!

生豬出籠稱重,數據對接erp增加至數據庫!

    當然接口類工作都是非常簡單的,無論是軟件接口微信接口,支付寶接口等等還是硬件接口如溫濕度接口,電子秤接口!

但是硬件設備公司都是很流氓的,不想軟件公司,接口都會有api開發文檔,參數詳細。有的好友demo;當然此次開發電子秤接口也是如此,

公司只給了一份電子秤使用說明書!別說demo了,說起來都是淚呀!開始在網上尋找api開發文檔,當然第一步肯定是現在,托利多官網尋找開發文檔,不出所料

也是三無,沒有開發文檔,沒有api接口說明書,沒有demo。去網上找吧,最終找到了電子秤demo也就是講解SerialPort類監控com串口!經過一番搗鼓,是能接口數據了可是數據類型,怎么確定哪,測試數據怎么傳哪?還是網上找吧!不說廢話了經過一番尋找好在都找到了!

    開始正式講解電子秤開發接口

    1,安裝 virtual-serial-port-kit用來帶卡com串口(可以從文章地府附件內下載,也可以網上搜索)

    2,下載ComDebug.exe用於發送測試數據        (可以從文章地府附件內下載,也可以網上搜索)

    3,測試數據                                              (可以從文章地府附件內下載,也可以網上搜索)

    4,開發軟件監控com串口接收數據                (可以從文章地府附件內下載demo)

    5,根據需求處理接收到的數據 

    6,可以與我交流吆QQ:809451989

    

demo代碼如下

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;
using Microsoft.Win32;

namespace TEXTCOM
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private SerialPort Sp = new SerialPort();
        public delegate void HandleInterfaceUpdataDelegate(string text); //委托,此為重點 
        private HandleInterfaceUpdataDelegate interfaceUpdataHandle;

        private bool qiii = false;

        public void Sp_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            string strTemp = "";
            double iSecond = 0.5;

            DateTime dtOld = System.DateTime.Now;
            DateTime dtNow = System.DateTime.Now;
            TimeSpan dtInter;
            dtInter = dtNow - dtOld;

            int i = Sp.BytesToRead;
            if (i > 0)
            {
                try
                {
                    strTemp = Sp.ReadExisting();
                }
                catch
                { }
                if (strTemp.ToLower().IndexOf("\r") < 0)
                {
                    i = 0;
                }
                else
                {
                    this.Invoke(interfaceUpdataHandle, strTemp);
                }
            }
            while (dtInter.TotalSeconds < iSecond && i <= 0)
            {
                dtNow = System.DateTime.Now;
                dtInter = dtNow - dtOld;
                i = Sp.BytesToRead;
                if (i > 0)
                {
                    try
                    {
                        strTemp += Sp.ReadExisting();
                    }
                    catch
                    { }
                    if (strTemp.ToLower().IndexOf("\r") < 0)
                    {
                        i = 0;
                    }
                    else
                    {
                        this.Invoke(interfaceUpdataHandle, strTemp);
                    }
                }
            }
            // do null  

        }

        private void UpdateTextBox(string text)
        {
            textBox2.Text = text;
        }

        /// <summary> 
        /// 執行AT指令並返回 成功失敗 
        /// </summary> 
        /// <param name="ATCmd">AT指令</param> 
        /// <param name="StCmd">AT指令標准結束標識</param> 
        /// <returns></returns> 
        private void ATCommand3(string ATCmd, string StCmd)
        {
            string response = "";
            response = ATCommand(ATCmd, StCmd);


        }

        /// <summary> 
        /// 執行AT指令並返回結果字符 
        /// </summary> 
        /// <param name="ATCmd">AT指令</param> 
        /// <param name="StCmd">AT指令標准結束標識</param> 
        /// <returns></returns> 
        private string ATCommand(string ATCmd, string StCmd)
        {
            string response = "";
            int i;
            if (!ATCmd.EndsWith("\x01a"))
            {
                if (!(ATCmd.EndsWith("\r") || ATCmd.EndsWith("\r\n")))
                {
                    ATCmd = ATCmd + "\r";
                }
            }
            Sp.WriteLine(ATCmd);

            //第一次讀響應數據 
            if (Sp.BytesToRead > 0)
            {
                response = Sp.ReadExisting();

                //去除前端多可能多讀取的字符 
                if (response.IndexOf(ATCmd) > 0)
                {
                    response = response.Substring(response.IndexOf(ATCmd));
                }
                else
                {

                }

                if (response == "" || response.IndexOf(StCmd) < 0)
                {
                    if (response != "")
                    {
                        if (response.Trim() == "ERROR")
                        {
                            //throw vError = new UnknowException("Unknown exception in sending command:" + ATCmd); 
                        }
                        if (response.IndexOf("+CMS ERROR") >= 0)
                        {
                            string[] cols = new string[100];
                            cols = response.Split(';');
                            if (cols.Length > 1)
                            {
                                string errorCode = cols[1];
                            }
                        }
                    }
                }
            }

            //讀第一次沒有讀完的響應數據,直到讀到特征數據或超時 
            for (i = 0; i < 3; i++)
            {
                Thread.Sleep(1000);
                response = response + Sp.ReadExisting();
                if (response.IndexOf(StCmd) >= 0)
                {
                    break;
                }
            }

            return response;


        }

        /// <summary> 
        /// 從注冊表獲取系統串口列表 
        /// </summary> 
        private void GetComList()
        {
            RegistryKey keyCom = Registry.LocalMachine.OpenSubKey("Hardware\\DeviceMap\\SerialComm");
            if (keyCom != null)
            {
                string[] sSubKeys = keyCom.GetValueNames();
                this.comboBox1.Items.Clear();
                foreach (string sName in sSubKeys)
                {
                    string sValue = (string)keyCom.GetValue(sName);
                    this.comboBox2.Items.Add(sValue);
                }
            }
        }

        private void cmID_DropDown(object sender, EventArgs e)
        {
            GetComList();
        } 


   

        private void Form1_Load_1(object sender, EventArgs e)
        {

            GetComList();
            btPause.Enabled = false;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Sp.Close();

        }

        private void btPause_Click_1(object sender, EventArgs e)
        {
            Sp.Close();
            btENT.Enabled = true;
            btPause.Enabled = false; 

        }

        private void btEnt_Click_1(object sender, EventArgs e)
        {
            if ((this.comboBox2.Text.Trim() != "") && (this.comboBox1.Text != ""))
            {
                interfaceUpdataHandle = new HandleInterfaceUpdataDelegate(UpdateTextBox);//實例化委托對象 
                Sp.PortName = this.comboBox2.Text.Trim();
                Sp.BaudRate = Convert.ToInt32(this.comboBox1.Text.Trim());
                Sp.Parity = Parity.None;
                Sp.StopBits = StopBits.One;
                Sp.DataReceived += new SerialDataReceivedEventHandler(Sp_DataReceived);
                Sp.ReceivedBytesThreshold = 1;
                try
                {
                    Sp.Open();

                    ATCommand3("AT+CLIP=1\r", "OK");


                    btPause.Enabled = true;
                    btENT.Enabled = false;
                }
                catch
                {
                    MessageBox.Show("端口" + this.comboBox2.Text.Trim() + "打開失敗!");
                }
            }
            else
            {
                MessageBox.Show("請輸入正確的端口號和波特率!");
                this.comboBox2.Focus();
            }
        }
    }
}

軟件開發之后怎么使用哪?

1,打開Virtual Serial Port Kit

2,增加com串口

     

3,打開ComDebug.exe並設置剛才添加的串口com 填入發送的數據 點擊打開端口 

4,打開開發的軟件 

5,點擊comdebug.exe 的發送

6,如果沒錯的話我們開發的demo就應該收到comdebug.exe發送的數據了

 

   

    

    

     

 

文章引用:http://www.qishunwang.net/knowledge_show_151.aspx


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM