固定扫描枪的C#程序

1. new SerialPort 对象 serialPort1
2. 给serialPort1的端口名赋值.
3. 设置serialPort1的参数值
 如:
private void SettingComm(object serialPortobj)
        {
            SerialPort serialPort = (SerialPort)serialPortobj;
            serialPort.BaudRate = 9600;
            serialPort.Parity = Parity.None;
            serialPort.StopBits = StopBits.One;
            serialPort.DataBits = 8;
            serialPort.DiscardNull = true;

        }
4. 打开端口   serialPort1.Open();
5. 将事件挷定到端口上.
 serialPort1.DataReceived += serialportReceiveData;
6. 接收来自端口的数据
        private void serialportReceiveData(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {
                SerialPort serialPort = (SerialPort)sender;
                ReceiveData(serialPort);
            }
            catch (Exception ex)
            {
                ScannerEventLog.WriteEntry("Error receiving data. Error Msg=" + ex.Message);
            }
        }

 private void ReceiveData(SerialPort serialPort)
        {
            try
            {
                Thread threadReceiveSub = new Thread(new ParameterizedThreadStart(AsyReceiveData));
                threadReceiveSub.Start(serialPort);
            }
            catch (Exception ex)
            {
                ScannerEventLog.WriteEntry("Error receiving data. Error Msg=" + ex.Message);
            }
        }

7.
private void AsyReceiveData(object serialPortobj)
        {

            string strXML = "";
            try
            {
                SerialPort serialPort = (SerialPort)serialPortobj;
         

                System.Threading.Thread.Sleep(100);
           
                string str = serialPort.ReadExisting();
               
                if (str == string.Empty)
                {
                    return;
                }

                // Station ID is hardcoded for the moment
              
                //strXML = "<STATIONID>" + System.Configuration.ConfigurationSettings.AppSettings[serialPort.PortName].ToString()+ "</STATIONID><DATA>";
                       
                // remove redundant 1st char.
                //str = str.Substring(1, str.Length - 1);

                //strXML = "<PRODUCTION_REQUEST><COMPLETE><SFC_LIST><SFC><SITE>" + System.Configuration.ConfigurationSettings.AppSettings["SITE"].ToString() + "</SITE>";
                //strXML += "<ACTIVITY>" + System.Configuration.ConfigurationSettings.AppSettings["ACTIVITY"].ToString() + "</ACTIVITY>";
                //strXML += "<ID>" + str + "</ID>";
                //strXML += "<RESOURCE>" + System.Configuration.ConfigurationSettings.AppSettings["RESOURCE"].ToString() + "</RESOURCE>";
                //strXML += "<OPERATION>" + System.Configuration.ConfigurationSettings.AppSettings["OPERATION"].ToString() + "</OPERATION>";
                //strXML += "<USER>" + System.Configuration.ConfigurationSettings.AppSettings["USER"].ToString() + "</USER>";
                //strXML += "<QUICK_COMPLETE>" + System.Configuration.ConfigurationSettings.AppSettings["QUICK_COMPLETE"].ToString() + "</QUICK_COMPLETE>";
                //strXML += "</SFC></SFC_LIST></COMPLETE></PRODUCTION_REQUEST>";

                strXML = str + "      哈哈哈..mmmmmmmmmmmmmmghggggggggggggggg///////wwwwwwww<<<<<<<>?>>>>>>>>>>>.";

                System.Console.Write(strXML);
                HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(strUrl);
                HttpWebResponse res = (HttpWebResponse)wr.GetResponse();
            }
            catch (Exception ex)
            {
                ScannerEventLog.WriteEntry("Error receiving data. Error Msg=" + ex.Message);
            }
        }

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭