我发现这个代码,并使用它:
void comPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
//determine the mode the user selected (binary/string)
switch (CurrentTransmissionType)
{
//user chose string
case TransmissionType.Text:
//read data waiting in the buffer
string msg = comPort.ReadExisting();
//display the data to the user
DisplayData(MessageType.Incoming, msg + "\n");
break;
//user chose binary
case TransmissionType.Hex:
//retrieve number of bytes in the buffer
int bytes = comPort.BytesToRead;
//create a byte array to hold the awaiting data
byte[] comBuffer = new byte[bytes];
//read the data and store it
comPort.Read(comBuffer, 0, bytes);
//display the data to the user
DisplayData(MessageType.Incoming, ByteToHex(comBuffer) + "\n");
break;
default:
//read data waiting in the buffer
string str = comPort.ReadExisting();
//display the data to the user
DisplayData(MessageType.Incoming, str + "\n");
break;
}
}
并且收到这个“40 32 00 01 D2 A6 B2 “(十六进制格式)我想第一个字节意味着这个设备是第一个,第二个字节意味着按钮编号N被按下等等... ...
你说你”读它“。你怎么读它(以什么格式 - 字符串,字节数组,??)? – 2011-04-25 23:16:06
您可以发布一些代码(只是读取传入数据的部分)。 – ChrisWue 2011-04-25 23:26:16