我發現這個代碼,並使用它:
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