本帖最后由 haylang 于 2019-2-12 10:24 编辑
private void Form1_Load(object sender, EventArgs e)
{
serialPort1.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);//必须手动添加事件处理程序
SearchAndAddSerialToComboBox(serialPort1, comboBox1);
count = 0; idcount = 0; timecnt = 0;
}
private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)//串口数据接收事件
{
string dataRe;
byte[] byteRead = new byte[serialPort1.BytesToRead];// 重新定义
DeleUpdateTextbox deleupdatetextbox = new DeleUpdateTextbox(UpdateTextbox);
serialPort1.Read(byteRead, 0, byteRead.Length);//获取串口接收到的字
for (int i = 0; i < byteRead.Length; i++)
{
byte temp = byteRead;
dataRe = temp.ToString("X2");// +" ";//字符串格式控制符 X为十六进制 2为每次都是两位数
textBox1.Invoke(deleupdatetextbox, dataRe);
}
} |