/*
//读取串口中一个字节的数据
String ch = mySerialPort.ReadExisting();
switch (ch)
{
case "$":
//接收到串口头
ReceiveData = "";
break;
case "\n":
//接收到串口尾
//在拥有此控件的基础窗口句柄的线程上执行委托Invoke(Delegate)
//即在控件textBoxInformation的父窗口form中执行委托.
textBoxInformation.Invoke
(
new MethodInvoker
(
delegate
{
//textBoxInformation.AppendText(ReceiveData);
textBoxReceiveData.Text = ReceiveData;
}
)
);
break;
default:
ReceiveData += ch;
break;
}
int ch = mySerialPort.ReadByte();
string str = string.Empty;
switch (ch)
{
case 0x12:
//接收到串口头,清空数组
Array.Clear(ReceiveData, 0, ReceiveData.Length);
ReceiveDataIndex = 0;
break;
case 0x14:
//接收到串口尾,输出string
for (int i = 0; i < ReceiveData.Length; i++)
{
str += (ReceiveData[i] - '0').ToString();
}
//在拥有此控件的基础窗口句柄的线程上执行委托Invoke(Delegate)
//即在控件textBoxInformation的父窗口form中执行委托.
textBoxInformation.Invoke
(
new MethodInvoker
(
delegate
{
//textBoxInformation.AppendText(ReceiveData);
textBoxReceiveData.Text = str;
}
)
);
break;
default:
ReceiveData[ReceiveDataIndex] = ch;
ReceiveDataIndex++;
if (ReceiveDataIndex > ReceiveData.Length)
{
ReceiveDataIndex = ReceiveData.Length - 1;
}
break;
}
- 1
- 2
- 3
- 4
- 5
- 6
前往页