1

仮想comポートのバーコードスキャナーから入力を読み取り、データをwinformのテキストボックスに書き戻す小さなwinformアプリケーションに取り組んでいます。私はC#が初めてなので、苦労しています。私の現在のコードは以下で、ここから適応されています

namespace Barcode_Scanner
{
    public partial class Form1 : Form
    {
        SerialPort sp;
        

       public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string[] ports = SerialPort.GetPortNames();
            comboBox1.DataSource = ports;
            Application.DoEvents();
            

          
            
        }

        private void btn_getComData_Click(object sender, EventArgs e)
        {
            try
            {
                if (!sp.IsOpen)
                    sp.Open();
                sp.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
                
            }
            catch (Exception ex)
            {
                MessageBox.Show("There was a problem with the Serial Port: " + ex.Message, "Error!");
            }
        }
        void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            sp = (SerialPort)sender;
            string data = sp.ReadExisting(); 
            txt_comData.Text = data;           
            Application.DoEvents();
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            // Makes sure serial port is open before trying to write
            string portname = comboBox1.SelectedItem.ToString();
            sp = new SerialPort(portname, 9600, Parity.None, 8, StopBits.One);
            sp.Handshake = Handshake.None;
            sp.Open();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            sp.Close();
        }
    }
}

スキャンしようとしている完全な文字列は「3894038」ですが、テキスト ボックスに一度に 1 文字しか表示できません。私の .ReadExisting コマンドに何か関係があると思われますが、どうすればよいか少し戸惑っています。私のコードに何か問題がありますか?

事前に助けてくれてありがとう。

マーシャル

4

1 に答える 1

2

コードにはかなりの問題があります。ReadExistingあなたが説明した問題は、値をテキストボックスに追加するのではなく、割り当てているという事実が原因です。私はそれを修正し、以下の他のいくつかの問題を修正しました。

ノート:

  • 割り当ての代わりに使用AppendTextして、テキスト ボックスのテキストの末尾に新しいデータを追加します
  • 電話する正当な理由は事実上ありませんApplication.DoEvents
  • 2 つの異なる場所で一貫性のない方法でシリアル ポートを開く
  • spクラスレベルですでに定義されています。イベント送信者のキャストで非表示にする必要はありません。

固定コード:

namespace Barcode_Scanner
{
    public partial class Form1 : Form
    {
        SerialPort sp;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string[] ports = SerialPort.GetPortNames();
            comboBox1.DataSource = ports;
        }

        private void btn_getComData_Click(object sender, EventArgs e)
        {
            try
            {
                if (!sp.IsOpen)
                {
                    button1_Click(null, EventArgs.Empty);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("There was a problem with the Serial Port: " + ex.Message, "Error!");
            }
        }

        void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            string data = sp.ReadExisting();
            txt_comData.Appendtext(data);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Makes sure serial port is open before trying to write
            string portname = comboBox1.SelectedItem.ToString();
            sp = new SerialPort(portname, 9600, Parity.None, 8, StopBits.One);
            sp.Handshake = Handshake.None;
            sp.Open();
            sp.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            sp.Close();
        }
    }
}
于 2016-03-24T18:53:30.657 に答える