我有一个 USB 设备,它应该在查询时发送一个号码。
我的指示是:
- 将 USB 端口连接到 PC,然后以 9600 波特、8 个数据位、无奇偶校验、1 个停止位打开虚拟串行端口。
- 发送 ESC(我认为是 char 27)
- 你会得到“>”作为确认
- 发送“c”,您将得到计数,后跟“<”
我到处看了看,一定不明白我在读什么,因为我不知道如何从单位得到回应。
- 我是否正确发送“ESC”键?
- 我是否正确查询响应?
- 我是否正确显示了假设的响应?
libusb
我需要研究什么吗?我认为它是 .NET 早期版本的库。
提前致谢。
这是我的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
namespace SerialPortTest
{
public partial class Form1 : Form
{
System.IO.Ports.SerialPort counter = new SerialPort("COM5", 9600, Parity.None, 8, StopBits.One);
public Form1()
{
InitializeComponent();
}
private void btnWrite_Click_1(object sender, EventArgs e)
{
// Get a list of serial port names.
string[] ports = System.IO.Ports.SerialPort.GetPortNames();
lblOut.Text = ("The following serial ports were found:");
// Display each port name to the console.
txtOut.Clear();
foreach (string port in ports)
{
txtOut.Text = (port);
}
}
private void Form1_KeyDown_1(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.A)
{
//configuring the serial port
//serialPort1.PortName = "COM5";
//serialPort1.BaudRate = 9600;
//serialPort1.DataBits = 8;
//serialPort1.Parity = Parity.None;
//serialPort1.StopBits = StopBits.One;
//Open the serial port
serialPort1.Open();
//Write data to serial port
serialPort1.Write(Keys.Escape.ToString());
//Read data from serial port
//string[] number;
//counter.DataReceived += counter.ReadExisting();
//counter.ReadLine();
//new SerialDataReceivedEventHandler(counter_DataReceived);
txtOut.Text = counter.ReadLine();
//Close the serial port
serialPort1.Close();
}
}
//private static SerialDataReceivedEventHandler counter_DataReceived(object sender, SerialDataReceivedEventArgs e)
//{
// //throw new NotImplementedException();
// SerialPort sp = (SerialPort)sender;
// string indata = sp.ReadExisting();
// return (sp.ReadExisting());
//}
}
}