Пример #1
0
        public static void sp_datarecived(object sender, SerialDataReceivedEventArgs e)
        {
            SerialPort sPort = (SerialPort)sender;
            //String data = sPort.ReadExisting();
            int readByte = sPort.BytesToRead;

            byte[] buffer = new byte[readByte];

            try
            {
                sPort.Read(buffer, 0, readByte);
            }
            catch (Exception ex) {
                readByte = 0;
            }
            foreach (byte ch in buffer)
            {
                if (fm.richTextBox1.InvokeRequired)
                {
                    SetRichTextValue st = new SetRichTextValue(sp_datarecived);
                    fm.Invoke(st, new object[] { sender, e });
                }
                else
                {
                    fm.richTextBox1.Text += String.Format("{0:X2}", ch) + " ";
                }
            }
        }
Пример #2
0
        void SetrichTextBox(string value)
        {
            if (tb_ReceiveMessage.InvokeRequired)
            {
                SetRichTextValue d = new SetRichTextValue(SetrichTextBox);
                tb_ReceiveMessage.Invoke(d, value);
            }
            else
            {
                if (tb_ReceiveMessage.Lines.Length > 5000)
                {
                    tb_ReceiveMessage.Clear();
                }

                //========richtextbox滚动条自动移至最后一条记录
                //让文本框获取焦点
                tb_ReceiveMessage.Focus();
                //设置光标的位置到文本尾
                tb_ReceiveMessage.Select(tb_ReceiveMessage.TextLength, 0);
                //滚动到控件光标处
                tb_ReceiveMessage.ScrollToCaret();
                if (m_bolHighlight)
                {
                    tb_ReceiveMessage.SelectionFont  = new Font("Verdana", 9, FontStyle.Bold);
                    tb_ReceiveMessage.SelectionColor = Color.Red;
                }
                tb_ReceiveMessage.AppendText(value + Environment.NewLine);
            }
        }
Пример #3
0
        private void pos_received_data(object sender, SerialDataReceivedEventArgs e)
        {
            //ArrayList arr = new ArrayList();
            int iBytesToRead = fm.pos_port.BytesToRead;

            byte[] comBuffer = new byte[iBytesToRead];
            int    i;

            // byte[] buffer = new byte[readByte];

            try
            {
                fm.pos_port.Read(comBuffer, 0, iBytesToRead);
            }
            catch (Exception ex) {
                Console.WriteLine("does't read port data somthing error");
            }



            //Thread.Sleep(50);
            foreach (byte ch in comBuffer)
            {
                // fm.richTextBox1.Text += (char)ch;
                str += (char)ch;
            }



            if (str != "")
            {
                String[] sp_arr = str.Trim().Split();
                //string an_s = "";

                foreach (string s2 in sp_arr)
                {
                    if (s2 != " " && s2 != null && s2 != "")
                    {
                        trim_arr.Add(s2);
                    }
                }
                //string[] sp_arr2 = an_s.Split();
                for (i = 0; i < trim_arr.Count; i++)
                {
                    //richTextBox1.Text += "[" + i + "]:" + sp_arr[i] + "\n";
                    //Debug.Write("[" + i + "]:" + trim_arr[i].ToString() + "\n");
                    if (sp_arr[i].ToString() == "!")
                    {
                        time = trim_arr[i].ToString() + " " + trim_arr[i + 1].ToString();

                        //break;
                    }

                    if (trim_arr[i].ToString() == "TRAN#")
                    {
                        trns_no = trim_arr[i + 1].ToString();
                        break;
                    }
                }

                foreach (string s in Setup.hotList)
                {
                    if (str.ToLower().Contains(s))
                    {
                        hot_key = s;
                    }
                }

                if (trns_no != "")
                {
                    reg_id = trns_no.Substring(0, 3);
                }

                Console.Write(time);
                Console.Write(trns_no + "\n");
                Console.Write(hot_key + "\n");
                Console.Write(reg_id + "\n");

                if (fm.pos_data_box.InvokeRequired)
                {
                    SetRichTextValue st = new SetRichTextValue(pos_received_data);
                    fm.Invoke(st, new object[] { sender, e });
                }
                else
                {
                    fm.pos_data_box.Text += "Time:" + time + " Transaction:" + trns_no + " POSID:" + reg_id + " HotKey:" + hot_key + "\n";
                }


                str = "";
                //Thread.Sleep(40);
                string query = "insert into posData values('" + trns_no + "','" + reg_id + "','" + hot_key + "','" + time + "');";
                DB.ExecuteNonQuery(query);

                trim_arr.Clear();
            }
        }