コード例 #1
0
        public RS232()
        {
            try
            {
                InitializeComponent();

                CommPort com = CommPort.Instance;


                int      found    = 0;
                string[] portList = com.GetAvailablePorts();
                for (int i = 0; i < portList.Length; ++i)
                {
                    string name = portList[i];
                    comboBoxPortName.Items.Add(name);
                    if (name == Settings.Port.PortName)
                    {
                        found = i;
                    }
                }
                if (portList.Length > 0)
                {
                    comboBoxPortName.SelectedIndex = found;
                }

                Int32[] baudRates =
                {
                    100,     300,   600,   1200,   2400,   4800, 9600, 14400, 19200,
                    38400, 56000, 57600, 115200, 128000, 256000, 0
                };
                found = 0;
                for (int i = 0; baudRates[i] != 0; ++i)
                {
                    comboBoxBaudRate.Items.Add(baudRates[i].ToString());
                    if (baudRates[i] == Settings.Port.BaudRate)
                    {
                        found = i;
                    }
                }
                comboBoxBaudRate.SelectedIndex = found;

                comboBoxDataBits.Items.Add("5");
                comboBoxDataBits.Items.Add("6");
                comboBoxDataBits.Items.Add("7");
                comboBoxDataBits.Items.Add("8");
                comboBoxDataBits.SelectedIndex = Settings.Port.DataBits - 5;

                foreach (string s in Enum.GetNames(typeof(Parity)))
                {
                    comboBoxParity.Items.Add(s);
                }
                comboBoxParity.SelectedIndex = (int)Settings.Port.Parity;

                foreach (string s in Enum.GetNames(typeof(StopBits)))
                {
                    comboBoxStopBits.Items.Add(s);
                }
                comboBoxStopBits.SelectedIndex = (int)Settings.Port.StopBits;

                foreach (string s in Enum.GetNames(typeof(Handshake)))
                {
                    comboBoxHandshake.Items.Add(s);
                }
                comboBoxHandshake.SelectedIndex = (int)Settings.Port.Handshake;

                com.StatusChanged += OnStatusChanged;

                //com.DataReceived += OnDataReceived;

                com.Open();
            }
            catch (Exception ex)
            {
                LogClass.WriteLogFile("Exception:" + ex.ToString());
            }
        }