如果我是从串口接收到一串数据如:
FE 68 14 00 81 61 43 5F 43 60 43 60 43 E7 21 E7 21 E6 21 E6 21 16
前面FE 68 14 00 81的不变的,后面的数据时随即产生的,但长度是固定,如何把后面这些数据放到文本框中放置的格式是4361放一个文本框435F放一个文本框4360放一个文本框,后面的也是这么实现。最后的16也是固定的,不用管。如何实现这个功能,非常感谢
FE 68 14 00 81 61 43 5F 43 60 43 60 43 E7 21 E7 21 E6 21 E6 21 16
前面FE 68 14 00 81的不变的,后面的数据时随即产生的,但长度是固定,如何把后面这些数据放到文本框中放置的格式是4361放一个文本框435F放一个文本框4360放一个文本框,后面的也是这么实现。最后的16也是固定的,不用管。如何实现这个功能,非常感谢
BitConverter.ToInt16
BitConverter.ToString等等
高手啊,能不能帮我解决一下我的问题啊。
http://topic.csdn.net/u/20120830/11/0da6fbd6-7f9d-4e6a-bfc1-f4bae3b531c4.html?seed=51242454&r=79547227#r_79547227
string strTemp1=string.Empty;
byte[] bData = new byte[2];
bData[0] = 43;
bData[1] = 61;
strTemp1= ConvertByteArrayToString(bData);string strTemp2=string.Empty;
bData=new byte[2];
bData[0] = 43;
bData[1] = 5F;
strTemp2= ConvertByteArrayToString(bData);string strTemp3=string.Empty;
bData=new byte[2];
bData[0] = 43;
bData[1] = 60;
strTemp3= ConvertByteArrayToString(bData);再对你需要的strTemp1,strTemp2,strTemp3进行操作即可。
bData[1] = 0x61;
string response="FE 68 14 00 81 61 43 5F 43 60 43 60 43 E7 21 E7 21 E6 21 E6 21 16
";
string[]responseSplit=response.Split(" ");//分割
TextBox1.Text=Response[5].toString("X2")+Response[6].ToString("X2"); 6143
依次类推。手工敲入的,不知道有没有大小写错误。自己看
private string ConvertByteArrayToString(byte[] byteArray)
{
Encoding enc = Encoding.UTF8;
string text = enc.GetString(byteArray);
return text;
}
接收的时候可以定义一个List<byte>泛型,往里面添加就可以了。例如:
private List<byte> byteAllRec = new List<byte>();
// 创建字节数组
byte[] buffer = new byte[bytes];
// 读取缓冲区的数据到数组
serialPort1.Read(buffer, 0, bytes);
string str = string.Empty;
for (int i = 0; i < bytes; i++)
{
byteAllRec.Add(buffer[i]);
}
string result = "";
for (int i = 5; i < array.Length; i = i + 2)
{
if (i == array.Length - 1)
{
result += array[i];
break;
}
string temp = array[i + 1] + array[i]; result += temp + ",";
}
string result = "";
for (int i = 5; i < array.Length; i = i + 2)
{
if (i == array.Length - 1)
{
result += array[i];
break;
}
string temp = array[i + 1] + array[i]; result += temp + ",";
}
public void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
// This method will be called when there is data waiting in the port's buffer这个方法时将调用有数据等在港口的缓冲区
// Determain which mode (string or binary) the user is inDetermain这模式(字符串或二进制)用户 if (CurrentDataMode == DataMode.Text)
{
// Read all the data waiting in the buffer读取所有等待在缓冲区的数据 string data = comport.ReadExisting(); // Display the text to the user in the terminal向用户显示文本的终端 Log(LogMsgType.Incoming, data);
}
else
{
// Obtain the number of bytes waiting in the port's buffer获得的字节数放在串口的缓冲区 int bytes = comport.BytesToRead; // Create a byte array buffer to hold the incoming data创建一个字节数组缓冲来保存传入的数据 byte[] buffer = new byte[bytes]; // Read the data from the port and store it in our buffer从串口读取数据并将其存储在缓冲中 comport.Read(buffer, 0, bytes); // Show the user the incoming data in hex format十六进制格式显示用户传入的数据 Log(LogMsgType.Incoming, ByteArrayToHexString(buffer));
}
}
private List<byte> byteAllRec = new List<byte>();//你的方法片段
byte[] buffer = new byte[bytes];
comport.Read(buffer, 0, bytes);
for(int i=0;i<buffer.Length;i++)
{
byteAllRec.Add(buffer[i]);
}然后再看我写的处理方法,计算一下,改一改不就可以出来了么