我試過了大概6種不同的方法。談到這一點,我只是一個初學者。我需要能夠獲得這個軸位置信息,下面是位置30000和0的示例。如何使用StreamReader讀取和轉換串口數據?
這是什麼進入串口後,我發送位置信息的請求。 - 其他時候,它可能是速度,我正在嘗試閱讀。這將是從100到33000
Here is what I am trying to read:
Hex Ascii
FF 2F 30 60 33 30 30 30 30 03 ÿ/0`30000.
有時候,這在任何地方:
FF 2F 30 60 30 03 ÿ/0`0.
我已發送請求給我的控制器。 它已經發回我的回覆。 我可以用我的終端程序看到它。但是我的程序不起作用。 我試圖使用.ReadByte和.ReadExisting,但沒有成功。
//BUTTON TO READ THE PORT AND ATTEMPT TO CONVERT DATA TO USABLE DATA.
private void Com10rcvbutton_Click(object sender, EventArgs e)
{
try
{
string line = serialPort1.ReadLine();
StreamReader sr = new StreamReader(line);
sr.BaseStream.Position = 0x04;
byte[] buffer = new byte[0];
sr.BaseStream.Read(buffer, 4, 8);
foreach (byte myByte in buffer)
textBox1.Text += myByte.ToString("X") + " ";
sr.Dispose();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
MessageBox.Show("Your Serial Test Code is Done");
}
}
感謝您的幫助提前...
它不會因爲工作的SerialPort。 ReadLine()只有在收到SerialPort.NewLine字符串時纔會返回。你收到的不是文本,而是二進制數據。你可能會跟'serialPort1.NewLine = new String((char)3,1)一起跛行;'和ReadLine()將會起作用。 –