public void SendHexCmd(string cmd)
        {
            string[] sArray = Regex.Split(cmd, "0x", RegexOptions.IgnoreCase);
            byte[]   bArray = new byte[sArray.Length];
            for (int i = 1; i < sArray.Length; i++)
            {
                try
                {
                    bArray[i - 1] = Convert.ToByte(sArray[i]);
                }
                catch (Exception e)
                {
                    string message = string.Format("Error: Hex command invalid:{0} {1}", cmd, bArray[0]);
                    MessageBox.Show(message);
                    break;
                }
            }

            //string message1 = string.Format("Debug: Hex command invalid:{0} {1} {2} {3}", cmd, bArray[0], bArray[1], sArray[0]);
            // MessageBox.Show(message1);

            m_ComPort.ClearBuffer();
            m_ComPort.Write(bArray, 0, bArray.Length);
            int ret = m_ComPort.WaitDetect(500);
        }
Exemple #2
0
 public void Send(byte[] buffer)
 {
     try
     {
         serialPort.Write(buffer, 0, buffer.Length);
     }
     catch (Exception ex)
     {
         Console.WriteLine("serial port : {0}", (object)ex);
     }
 }
Exemple #3
0
        private static void OnTimerEvent(object sender, ElapsedEventArgs e)
        {
            m_StartTimer = true;
            byte[] by = null;
            try
            {
                for (int i = 0; i < 3; i++)
                {
                    if (m_StopTimer)
                    {
                        return;
                    }
                    foreach (string portName in SearchSerialPort.SerialPortNames)
                    {
                        if (m_StopTimer)
                        {
                            return;
                        }

                        m_Over = false;
                        if (m_SerialPort == null)
                        {
                            m_SerialPort = SerialPortManager.CreateSerialPort();
                            m_SerialPort.DataReceived += SerialPortDataReceived;
                        }

                        m_SerialPort.PortName = portName;
                        try
                        {
                            m_SerialPort.BaudRate = WinApi.B19200;
                            m_SerialPort.Open();

                            if (!SerialPortManager.Device1.SerialPortDevice.IsOpen)
                            {
                                if (m_StopTimer)
                                {
                                    return;
                                }
                                try
                                {
                                    by = PortAgreement.DistanceDeviceEncryption(DefaultParams.DEVICEDEFAULTCLIENTNUMBER, DefaultParams.DEVICEDEFAULTPASSWORD);
                                    m_SerialPort.Write(by);

                                    bool ret = WaitResult(SerialPortManager.Device1);
                                    if (ret)
                                    {
                                        continue;
                                    }
                                }
                                catch (Exception ex)
                                {
                                    Log4Helper.ErrorInfo(ex.Message, ex);
#if DEBUG
                                    Console.WriteLine(ex.Message);
#endif
                                }
                            }

                            m_SerialPort.SetBaudRate(WinApi.B9600);
                            Thread.Sleep(50);

                            if (!SerialPortManager.Device2.SerialPortDevice.IsOpen)
                            {
                                int address = 1;
                                try
                                {
                                    for (int number = 1; number <= 50; number++)
                                    {
                                        address = number;
                                        if (m_StopTimer)
                                        {
                                            return;
                                        }
                                        by = PortAgreement.CorridorReadCardData(number);
                                        m_SerialPort.Write(by);
                                        Thread.Sleep(50);
                                        if (m_Over)
                                        {
                                            ReplaceSerialPort(SerialPortManager.Device2);
                                            break;
                                        }
                                    }

                                    if (m_Over)
                                    {
                                        continue;
                                    }
                                    bool ret = WaitResult(SerialPortManager.Device2);
                                    if (ret)
                                    {
                                        continue;
                                    }
                                }
                                catch (Exception ex)
                                {
                                    Log4Helper.ErrorInfo(ex.Message, ex);
#if DEBUG
                                    Console.WriteLine(ex.Message);
#endif
                                }
                                finally
                                {
                                    SerialPortManager.Device2Address = address;
                                }
                            }

                            m_SerialPort.Close();
                        }
                        catch
                        {
                            continue;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log4Helper.ErrorInfo(ex.Message, ex);
#if DEBUG
                Console.WriteLine(ex.Message);
#endif
            }
            finally
            {
                by = null;
                m_Validation.Dispose();
                if (m_SerialPort != null && m_SerialPort.IsOpen)
                {
                    m_SerialPort.Close();
                }
                m_SerialPort = null;
                if (!m_StopTimer && (!SerialPortManager.Device1.SerialPortDevice.IsOpen && !SerialPortManager.Device2.SerialPortDevice.IsOpen))
                {
                    ViewCallFunction.ViewConnectionFailedMessage();
                }
                m_StartTimer = false;
            }
        }
Exemple #4
0
    protected virtual void OnButtonSendClicked(object sender, System.EventArgs e)
    {
        if (!MyPort.IsOpen)
        {
            togglebuttonPortSwitch.Active = true;
            if (!MyPort.IsOpen)
            {
                OntogglebuttonPortSwitchClicked(this, null);
            }
        }
        if (MyPort.IsOpen)
        {
            string strSend = textviewSend.Buffer.Text;
            byte[] sendByte;
            switch (SendMode)
            {
            case ConvertMode.Text:
                sendByte = new byte[StringConverts.StringToBytes(strSend).Length];
                sendByte = StringConverts.StringToBytes(strSend);
                break;

            case ConvertMode.Hex:
                sendByte = new byte[StringConverts.HexStringToBytes(strSend).Length];
                sendByte = StringConverts.HexStringToBytes(strSend);
                break;

            case ConvertMode.Dec:
                sendByte = new byte[StringConverts.DecStringToBytes(strSend).Length];
                sendByte = StringConverts.DecStringToBytes(strSend);
                break;

            default:
                sendByte = new byte[StringConverts.StringToBytes(strSend).Length];
                sendByte = StringConverts.StringToBytes(strSend);
                break;
            }
            MyPort.Write(sendByte, 0, sendByte.Length);
            SendStream.Write(sendByte, 0, sendByte.Length);
            moverTextS.Append(StringConverts.BytesToString(sendByte));
            moverHexS.Append(StringConverts.BytesToHexString(sendByte));
            moverDecS.Append(StringConverts.BytesToDecString(sendByte));

            labelTxStatus.Text = SendStream.Length.ToString();
            processScrollEvent(ConvertMode.Text, true);
            processScrollEvent(ConvertMode.Hex, true);
            processScrollEvent(ConvertMode.Dec, true);
//						TextIter iter;
//						iter = textviewTextS.Buffer.EndIter;
//						textviewTextS.Buffer.Insert (ref iter, StringConverts.BytesToString (sendByte));
//						if (checkbuttonAutoScrollSend.Active) {
//								// Console.WriteLine("\nLast\t{0}",GtkScrolledWindowTextS.Vadjustment.Value);
//								// Console.WriteLine("Lower\t{0}",GtkScrolledWindowTextS.Vadjustment.Lower);
//								// Console.WriteLine("S\t{0}",GtkScrolledWindowTextS.Vadjustment.StepIncrement);
//								// Console.WriteLine("P\t{0}",GtkScrolledWindowTextS.Vadjustment.PageIncrement);
//								// GtkScrolledWindowTextS.Vadjustment.Value = GtkScrolledWindowTextS.Vadjustment.Upper;
//								textviewTextS.Buffer.CreateMark ("EndMark", iter, false);
//								textviewTextS.ScrollToMark (textviewTextS.Buffer.CreateMark ("EndMark", iter, false), 0, false, 0, 0);
//								textviewTextS.Buffer.DeleteMark ("EndMark");
//						}
//
//						iter = textviewHexS.Buffer.EndIter;
//						textviewHexS.Buffer.Insert (ref iter, StringConverts.BytesToHexString (sendByte));
//						if (checkbuttonAutoScrollSend.Active) {
//								// GtkScrolledWindowHexS.Vadjustment.Value = GtkScrolledWindowHexS.Vadjustment.Upper;
//								textviewHexS.Buffer.CreateMark ("EndMark", iter, false);
//								textviewHexS.ScrollToMark (textviewHexS.Buffer.CreateMark ("EndMark", iter, false), 0, false, 0, 0);
//								textviewHexS.Buffer.DeleteMark ("EndMark");
//						}
//						iter = textviewDecS.Buffer.EndIter;
//						textviewDecS.Buffer.Insert (ref iter, StringConverts.BytesToDecString (sendByte));
//						if (checkbuttonAutoScrollSend.Active) {
//								// GtkScrolledWindowDecS.Vadjustment.Value = GtkScrolledWindowDecS.Vadjustment.Upper;
//								textviewDecS.Buffer.CreateMark ("EndMark", iter, false);
//								textviewDecS.ScrollToMark (textviewDecS.Buffer.CreateMark ("EndMark", iter, false), 0, false, 0, 0);
//								textviewDecS.Buffer.DeleteMark ("EndMark");
//						}
            // ---------------
            // Generate Linenumber,copyright by [email protected],^-^
            // TextViewUtils.GenerateLineNumber(textviewTextS,textviewTextSL,lineHeight);
        }
    }