none
Rs232 條碼掃描機問題 RRS feed

  • 問題

  •  private void Form1_Load(object sender, EventArgs e)
            {           
                _serialPort = new SerialPort("COM1", 19200, Parity.None, 8, StopBits.One);
                _serialPort.Handshake = Handshake.None;
                _serialPort.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
                _serialPort.Open();
                _serialPort.ReadTimeout = 1000;
                _serialPort.WriteTimeout = 1000;
              
            }
    
            private void btnStart_Click(object sender, EventArgs e)
            {
                // Makes sure serial port is open before trying to write
                try
                {
                    if (!_serialPort.IsOpen)
                        _serialPort.Open();
                     _serialPort.Write("SI\r\n");
                     MessageBox.Show("OPEN");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error opening/writing to serial port :: " + ex.Message, "Error!");
                }
            }
    
            void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
            {
                Thread.Sleep(500);
                //_serialPort.ReadTimeout=10000;
                string data = _serialPort.ReadChar().ToString();
                byte[] bufferData = new byte[_serialPort.BytesToRead]; 
                this.BeginInvoke(new SetTextDeleg(si_DataReceived), new object[] { data});
                
            }
    
            private void si_DataReceived(string data)
            {
                textBox1.Text += data;        
            }
    請問為何我同一個條碼每次讀出來的值都不一樣...
    2015年2月5日 上午 02:33

解答