C#串口接收和确认

时间:2014-07-09 14:16:15

标签: c# serial-port

我现在又回到了一个小项目,我现在被困住了,试图收到然后确认收据,以便从COM1设备获取下一组数据。

基本上该设备是摩托罗拉MC1000手持式条码扫描设备,其上的软件与该程序相关联,该程序不再有效但通过串口发送数据输出,我已设法手动获取数据通过连接到端口,然后手动发送"%0"到设备输出内存中的所有数据。

到目前为止,我编写了一个程序来连接到com设备,打开com端口,并接收缓冲区信息,但我需要发送一个自动确认来获取所有数据。

数据采用条形码数量的格式。

到目前为止,这是我的代码:

<!-- language: c# -->
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
 public partial class Form1 : Form
      {
          // Add this variable

          string RxString;
          SerialPort ComPort = new SerialPort();  

          public Form1()
          {
              InitializeComponent();
          }

          private void buttonStart_Click(object sender, EventArgs e)
          {
              ComPort.PortName = Convert.ToString(cboPorts.Text);
              serialPort1.PortName = ComPort.PortName;
              serialPort1.BaudRate = 9600;

              serialPort1.Open();
              if (serialPort1.IsOpen)
              {
                  buttonStart.Enabled = false;
                  buttonStop.Enabled = true;
                  textBox1.ReadOnly = false;
              }
          }

          private void buttonStop_Click(object sender, EventArgs e)
          {
              if (serialPort1.IsOpen)
              {
                  serialPort1.Close();
                  buttonStart.Enabled = true;
                  buttonStop.Enabled = false;
                  textBox1.ReadOnly = true;
              }

          }

          private void Form1_FormClosing(object sender, FormClosingEventArgs e)
          {
              if (serialPort1.IsOpen) serialPort1.Close();
          }

          private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
          {
              // If the port is closed, don't try to send a character.

              if(!serialPort1.IsOpen) return;

              // If the port is Open, declare a char[] array with one element.
              char[] buff = new char[1];

              // Load element 0 with the key character.

              buff[0] = e.KeyChar;

              // Send the one character buffer.
              //serialPort1.Write(buff, 0, 1);

              if (e.KeyChar == (char)13) // enter key  
              {
                  serialPort1.Write("%0");
                  //textBox1.Text = "";
              }
              else if (e.KeyChar < 32 || e.KeyChar > 126)
              {
                  e.Handled = true; // ignores anything else outside printable ASCII range  
              }
              else
              {
                  serialPort1.Write(e.KeyChar.ToString());
              }


              // Set the KeyPress event as handled so the character won't
              // display locally. If you want it to display, omit the next line.
              //e.Handled = true;
          }

          private void DisplayText(object sender, EventArgs e)
          {
              textBox1.AppendText(RxString);
          }

          private void serialPort1_DataReceived
            (object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
          {
              RxString = serialPort1.ReadExisting() + "\n";
              this.Invoke(new EventHandler(DisplayText));
          }

          private void Form1_Load(object sender, EventArgs e)
          {
              string[] ArrayComPortsNames = null;
              int index = -1;
              string ComPortName = null;

              //Com Ports
              ArrayComPortsNames = SerialPort.GetPortNames();
              do
              {
                  index += 1;
                  cboPorts.Items.Add(ArrayComPortsNames[index]);
              } while (!((ArrayComPortsNames[index] == ComPortName) ||
                (index == ArrayComPortsNames.GetUpperBound(0))));
              Array.Sort(ArrayComPortsNames);

              if (index == ArrayComPortsNames.GetUpperBound(0))
              {
                  ComPortName = ArrayComPortsNames[0];
              }

              //get first item print in text
              cboPorts.Text = ArrayComPortsNames[0];
          }

          private void cboPorts_SelectedIndexChanged(object sender, EventArgs e)
          {

          }
      }
  }

我只是需要自动输出这一切。

0 个答案:

没有答案