0

I have a Electronic balance device which is plugged in to a USB port. The output is continuous at all times, and I can read it by LibUsbDotNet in C#. Transmission codes are ASCII 8-bit codes. And other device information is:

start bit:1bit, Data bit:8bits, Parity bit:0/1 bits, Stop bit:2bits

Data bits (D1-D7) or (D1-D8) are a numerical value (0-9); codes are 30H-39H.

My questions are:

  1. How can I read the last data from the device?

  2. How do I convert them (bytes) to ASCII 8-bit (30H-39H) so that understand their numbers?

My code is:

private void cmdRead_Click(object sender, EventArgs e)
{
    cmdRead.Enabled = false;
    byte[] readBuffer = new byte[64];
    int uiTransmitted;
    ErrorCode eReturn;
    if ((eReturn = mEpReader.Read(readBuffer, 1000, out uiTransmitted)) == ErrorCode.None)
    {
        tsStatus.Text = uiTransmitted + " bytes read.";
        showBytes(readBuffer, uiTransmitted);
    }
    else
        tsStatus.Text = "No data to read! " + eReturn;

    cmdRead.Enabled = true;
}
private void showBytes(byte[] readBuffer, int uiTransmitted)
{
    if (ckShowAsHex.Checked)
    {
        // Convert the data to a hex string before displaying
        string hex = GetHexString(readBuffer, 0, uiTransmitted).ToString();
        tRecv.AppendText(hex);
        tRecv.AppendText(Environment.NewLine);
    }
    else
    {
        // Display the raw data
        tRecv.AppendText(Encoding.UTF8.GetString(readBuffer, 0, uiTransmitted));
        tRecv.AppendText(Environment.NewLine);
    }
}
private static StringBuilder GetHexString(byte[] data, int offset, int length)
{
    StringBuilder sb = new StringBuilder(length * 3);
    for (int i = offset; i < (offset + length); i++)
    {
        sb.Append(data[i].ToString("X2") + " ");
    }
    return sb;
}

I can upload the project if required. For example, when the device shows 3.04, the value that the application returns is:

01 62 21 60 68 38 7F 60 78 61 60 28 6D 7F 61 40 7F 69 61 62 79 69 79 79 78 79 79 20 78 69 61 21 20 68 20 61 68 7F 6B 63 61 7B 60 78 39 6D 7F 60 41 28 69 7F 60 63 01 4F 7F 60 78 69 79 69 40 69 

How can I find the value that devices show from the above response or how can I get correct data (correct encode) from the device?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Hamid
  • 817
  • 1
  • 13
  • 29
  • `System.Text.Encoding.ASCII.GetString` – Matt Burland Aug 01 '14 at 17:18
  • possible duplicate of [Converting byte\[\] to string in C#](http://stackoverflow.com/questions/1003275/converting-byte-to-string-in-c-sharp) – Matt Burland Aug 01 '14 at 17:18
  • Encoding.Ascii results in "□b!`h8`xa`(ma@iabyiyyxyy xia! h ahkca{`x9m`A(i`c□O`xiyi@i", so it won't solve the problem, at least for now. @Hamid Are you sure that the device sends ASCII codes through USB? What does documentation say about package formats? – Eugene Podskal Aug 01 '14 at 17:20
  • @EugenePodskal: Actually, the data the OP has included here is clearly mangled. Looking at the docs it looks like it's just sending measurement over a serial port with a CR/LF separating readings. They should probably use `ReadLine` rather than `Read`. But I suspect they have something misconfigured anyway. Check the baud rate. – Matt Burland Aug 01 '14 at 17:24
  • 1
    @MattBurland Seems like it. So either the package format does not use ASCII(I acknowledge that I am too lazy to read those docs), or none of the start(parity) bits are removed, or some configuration(usage) problem, etc... Remote debugging of unknown program is difficult, remote debugging of the whole hardware-software system is even worse. – Eugene Podskal Aug 01 '14 at 17:33
  • @EugenePodskal: It is ASCII according to the docs that Hamid linked. So there's a config problem somewhere. Like you said, pretty much impossible to debug remotely. – Matt Burland Aug 01 '14 at 17:35
  • Do you know how can convert number 1 to 31H or vice versa. – Hamid Aug 01 '14 at 17:48
  • How can read only last data? last bytes? – Hamid Aug 01 '14 at 18:15
  • 1
    You are mixing up a USB bus with a serial port. The manual only ever mentions a serial port, this device ought to come with a USB driver that emulates one. You use the SerialPort class in C# to read it. The USB frame data isn't going to be helpful at all, manufacturers do not document it. Use Device Manager to verify that you get the COM port after you connect the scale. – Hans Passant Aug 01 '14 at 18:53
  • @HansPassant, How can I verify that get COM port from USB port? – Hamid Aug 01 '14 at 19:30
  • 1
    I already mentioned that: "Use Device Manager". Google it if you don't know what that means. – Hans Passant Aug 01 '14 at 19:34
  • You sure they aren't [UTF-8 digits](http://www.fileformat.info/info/charset/UTF-8/list.htm) 30H-39H? [Rhetorical question] .NET conveniently uses UTF-8 by default in character streams. That should simplify your code. Use `ReadLine` as @MattBurland suggested. – Tom Blodget Aug 01 '14 at 23:39
  • @HansPassant, I knew where and how to use "Device Manager" but I didn't knew that the COM2USB adapter has installed a new COM port itself. It works as a normal COM port correctly. Thanks – Hamid Aug 02 '14 at 06:02

1 Answers1

0

As mentioned at this comment by Hans Passant, The COM-To-USB port adapter had installed a new COM port and that works exactly as what manufacture was mentioned in its document. All received lines from that COM port was ready to use ASCII codes.

Community
  • 1
  • 1
Hamid
  • 817
  • 1
  • 13
  • 29