modbus上位机运行界面:
Test_SerialPort的c#源程序如下:
- /*
- //读取串口中一个字节的数据
- 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;
- }
- )
- );
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
串口Mudbud.rar
(79.71 KB, 下载次数: 273)
|