0

皆さんこんにちは。私が理解していない2つの問題について、誰かが私を助けてくれることを願っています。

private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        // If the com port has been closed, do nothing
        if (!comport.IsOpen) return;

        // This method will be called when there is data waiting in the port's buffer

        // Determain which mode (string or binary) the user is in

            // Read all the data waiting in the buffer
            string data = comport.ReadExisting();
            textBox1.AppendText(data);
            textBox1.ScrollToCaret();
            //scnbutton.PerformClick();

            // Display the text to the user in the terminal


    }

バーコードを扱っています。スキャナーがバーコードをスキャンすると、S08A07934068721 が返されます。UPC がテキストボックスに追加したい値である 07934068721 になるたびに、この値を返します。

     string data1= data.Substring(4);
     textBox1.AppendText(data1);

これは、サブストリングに使用しようとしたものの例です。

文字列データをサブストリング化しようとするたびに、断片化してしまい、それを止める方法がわかりません。これを修正した後、以下のコードで問題が発生します

textBox1.Text = textBox1.Text.PadLeft(13, '0');

これはうまく機能し、常に13桁を埋めます。しかし、UPC などのタイプの先頭に 0 がある場合、12 桁に下がるのはなぜですか?

4

2 に答える 2

0

いろいろ遊んだ後、テキストボックスに目に見えない文字があることを発見したので、

textBox1.Text = textBox1.Text.Trim();

これにより、非表示の文字が削除され、パディングが正しく機能するようになりました。次に、データ受信イベントをタイマーに変更して、このようなクロススレッドの問題を回避しました

private void timer3_Tick(object sender, EventArgs e)
    {

        data = comport.ReadExisting();

        try
        {
            if (data != "")
            {
                textBox1.Clear();
                textBox1.AppendText(data);
                timer3.Stop();
                scan();
                timer3.Start();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
        comport.DiscardInBuffer();

    }

私のプログラムは現在、必要なように機能しています。ご協力ありがとうございましたuser2019047

于 2013-05-29T13:06:57.993 に答える