I have a usb device that is supposed to send a number when queried.
The instructions I have are:
- Connect the USB port to a PC and then open the virtual serial port at 9600 baud, 8 data bits, no parity, 1 stop bit.
- Send ESC (I think it's char 27)
- You'll get ">" as an acknowledgement
- Send "c" and you will get the count followed by "<"
I have looked everywhere and must not be understanding what I am reading because I cannot figure out how to get a response from the unit.
- Am I sending the "ESC" key properly?
- Am I querying a response properly?
- Am I displaying an assumed response properly?
- Is
libusb
something I need to research? I assumed it was a library for earlier versions of .NET.
Thanks in advance.
Here is my code:
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());
//}
}
}