سؤال

I have a usb device that is supposed to send a number when queried.

The instructions I have are:

  1. 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.
  2. Send ESC (I think it's char 27)
  3. You'll get ">" as an acknowledgement
  4. 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.

  1. Am I sending the "ESC" key properly?
  2. Am I querying a response properly?
  3. Am I displaying an assumed response properly?
  4. 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());
    //}
}

}

هل كانت مفيدة؟

المحلول

counter.ReadLine() will only return when a newline character is read, which according to the specifications given by you will never happen.

After the first input, you expect one byte of output back, so you should read one byte. Then you need to send a c, after which you'll have to read until you encounter an <.

So something like this should work:

SerialPort serialPort = new SerialPort("COM5", 9600, Parity.None, 8, StopBits.One);

var esc = new byte { 27 };
var c = new byte { 99 };

serialPort.Write(esc, 0, 1);

serialPort.ReadTo(">"); // Or ReadByte() and check if the byte read has a value of 62

serialPort.Write(c, 0, 1);

string data = serialPort.ReadTo("<");

Then you can parse the data string to whatever format you expect it to be.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top