none
串列埠偵測錯誤 RRS feed

  • Pergunta

  • 個人以 VS2012 開發應用系統, 使用 C# 語言, 其中需透過串列埠連線至發票機, 之前在 Windows 各平台皆可正常運作, 直到最近的 Windows 10

    ( 版本詳述於文末 ), 執行連線測試 ( ComPort.OpenPort ) 顯示連線失敗的訊息, 以下為程式片段, 祈求好心的大大們給予小弟協助, 謝謝 !!

    --------------------------------------------------------------------------------------------------------------------------------------

    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.IO;
    using System.IO.Ports;


            public string m_Port;
            // ----------------------------  For Serial Port Print   ---------------------------
            SerialPort ComPort = new SerialPort();
            Boolean flgGood2Go = false;

    ---- 主程式


                InitializeComponent();
                // ------------  For Serial Port Print  ------------------------------------------------------
                ComPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(port_DataReceived);
                ComPort.PinChanged += new System.IO.Ports.SerialPinChangedEventHandler(port_PinChanged);

                flgGood2Go = m_enumSerialPorts();


                sql = "select PrintKind, Printer from EFSaleCcrSetup where MachineNo = 'A'";
                con.Open();
                sCommand = new SqlCommand(sql, con);
                sAdapter = new SqlDataAdapter(sCommand);
                sBuilder = new SqlCommandBuilder(sAdapter);
                sDs = new DataSet();
                sAdapter.Fill(sDs, "EFSaleCcrSetup");
                sTable = sDs.Tables["EFSaleCcrSetup"];
                con.Close();
                m_Printer = sDs.Tables[0].Rows[0]["PrintKind"].ToString().Trim();
                m_Port = sDs.Tables[0].Rows[0]["Printer"].ToString().Trim();


    ----- 偵測發票機


                        if (!OpenService(true))
                        {
                            MessageBox.Show("發票機未連線, 發票列印失敗");
                            return;
                        }

    ------ OpenService 定義

            private Boolean OpenService(Boolean p_flgOpen)
            {
                if (p_flgOpen)
                {
                    if (!ComPort.IsOpen)
                    {
                        // ComPort.PortName = Convert.ToString(cboPorts.Text);
                        try
                        {
                           ComPort.PortName = m_Port;
                           ComPort.BaudRate = Convert.ToInt32(cboBaudRate.Text);
                           ComPort.DataBits = Convert.ToInt16(cboDataBits.Text);
                           ComPort.StopBits = (StopBits)Enum.Parse(typeof(StopBits), cboStopBits.Text);
                           ComPort.Handshake = (Handshake)Enum.Parse(typeof(Handshake), cboHandShaking.Text);
                           ComPort.Parity = (Parity)Enum.Parse(typeof(Parity), cboParity.Text);
                           ComPort.Open();
                        }
                        catch (Exception Ex)
                        {
                            MessageBox.Show(Ex.Message.ToString(), "發票機連接埠開啟失敗 : " + m_Port);
                        }
                    }
                   // txtStatus.Text = ComPort.PortName + (ComPort.IsOpen ? " Open: " : " Close: ");
                    return ComPort.IsOpen;
                }
                else
                {
                    if (ComPort.IsOpen)
                    {
                        ComPort.Close();
                    }
                  //  txtStatus.Text = ComPort.PortName + (ComPort.IsOpen ? " Open: " : " Close: ");
                    return true;
                }
            }

    -----------------------------------------------------------------------------------------------
    程式作業平台 : Windows 10 專業版

    版本 1909 ( OS 組建 18363.592 )

    開發工具 VS 2012 專業版 ( c# )

    • Movido AskaSu quarta-feira, 20 de maio de 2020 08:56
    quarta-feira, 20 de maio de 2020 06:52

Todas as Respostas