仮想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 コマンドに何か関係があると思われますが、どうすればよいか少し戸惑っています。私のコードに何か問題がありますか?
事前に助けてくれてありがとう。
マーシャル