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:
How can I read the last data from the device?
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?
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.