switch (ls_CmdType)//如果是开阀就按此格式添加
{
case "K"://开/关阀
ls_str = "I" + ls_teradds.Substring(ls_teradds.Length - 4, 4)
+ ls_TerRe
+ isml.Substring(isml.Length - 2, 2)
+ ls_CmdType
+ ls_CmdTypeStr;
break;
case "V"://检查温度
ls_str = "L" + ls_teradds.Substring(ls_teradds.Length - 4, 4)
+ ls_TerRe
+ isml.Substring(isml.Length - 2, 2)
+ ls_CmdType;
break;
case "T"://检查时间
ls_str = "L" + ls_teradds.Substring(ls_teradds.Length - 4, 4)
+ ls_TerRe
+ isml.Substring(isml.Length - 2, 2)
+ "&8013";
//MessageBox.Show(ls_str);
break;
default:
ls_str = "0027";
break;
}
string model = protocol.Getinstance(ls_str)._Model;
if (!spPort.IsOpen)
{
OpenComm();
}
spPort.Write(model);//将指令写入串口缓冲区
System.Threading.Thread.Sleep(500);
dt.Clear();
dt.Dispose();
spPort.DiscardInBuffer();//清理输入缓冲区
spPort.DiscardOutBuffer();//清理输出缓冲区
//}
}
}
catch (System.Exception e)
{
MessageBox.Show(e.ToString(), "警告");
} }
void spPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
int bytes = spPort.BytesToRead;
// 创建字节数组
byte[] buffer = new byte[bytes];
// 读取缓冲区的数据到数组
spPort.Read(buffer, 0, bytes);
string data = Encoding.ASCII.GetString(buffer);//ByteArrayToHexString(buffer);
isstr += data;
isstr = isstr.Replace(" ", "");
}
当我触发检测时间按钮时,我发现缓冲区里数据还有时间的返回值,导致时间解析错误,
#B0035290&8013000000000014000137039052000000000000005D!#B0035290V000001002390CD!
#B0035290&8013000000000014000137039052000000000000005D!
这是我在逐步执行中捕捉到的数据,我触发一次命令,返回一个数据,但是导致数据在缓冲区积累,让我无法解析,怎么才能执行一次就清除缓冲区呢?c#缓冲区
{
case "K"://开/关阀
ls_str = "I" + ls_teradds.Substring(ls_teradds.Length - 4, 4)
+ ls_TerRe
+ isml.Substring(isml.Length - 2, 2)
+ ls_CmdType
+ ls_CmdTypeStr;
break;
case "V"://检查温度
ls_str = "L" + ls_teradds.Substring(ls_teradds.Length - 4, 4)
+ ls_TerRe
+ isml.Substring(isml.Length - 2, 2)
+ ls_CmdType;
break;
case "T"://检查时间
ls_str = "L" + ls_teradds.Substring(ls_teradds.Length - 4, 4)
+ ls_TerRe
+ isml.Substring(isml.Length - 2, 2)
+ "&8013";
//MessageBox.Show(ls_str);
break;
default:
ls_str = "0027";
break;
}
string model = protocol.Getinstance(ls_str)._Model;
if (!spPort.IsOpen)
{
OpenComm();
}
spPort.Write(model);//将指令写入串口缓冲区
System.Threading.Thread.Sleep(500);
dt.Clear();
dt.Dispose();
spPort.DiscardInBuffer();//清理输入缓冲区
spPort.DiscardOutBuffer();//清理输出缓冲区
//}
}
}
catch (System.Exception e)
{
MessageBox.Show(e.ToString(), "警告");
} }
void spPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
int bytes = spPort.BytesToRead;
// 创建字节数组
byte[] buffer = new byte[bytes];
// 读取缓冲区的数据到数组
spPort.Read(buffer, 0, bytes);
string data = Encoding.ASCII.GetString(buffer);//ByteArrayToHexString(buffer);
isstr += data;
isstr = isstr.Replace(" ", "");
}
当我触发检测时间按钮时,我发现缓冲区里数据还有时间的返回值,导致时间解析错误,
#B0035290&8013000000000014000137039052000000000000005D!#B0035290V000001002390CD!
#B0035290&8013000000000014000137039052000000000000005D!
这是我在逐步执行中捕捉到的数据,我触发一次命令,返回一个数据,但是导致数据在缓冲区积累,让我无法解析,怎么才能执行一次就清除缓冲区呢?c#缓冲区
解决方案 »
- 超出当前范围。 (异常来自 HRESULT:0x8002000A (DISP_E_OVERFLOW))
- C#图像处理相关问题
- C# 中调用可执行程序,怎么才能够确认所调用的可执行文件已经运行完了。
- this.Button1.Attributes.Add("OnClick", "return window.confirm(\"确认?\");");
- 请问在插件里面调用formatter.Deserialize(stream),反序列化有问题?
- 连接数据库问题 问我的程序在服务器上可以运行 到客户端程序就不能连接数据库了
- 虚拟目录明明存在,为什么 Parent.Children.Find("Sea","IIsVirtualDir"); 总是跑出错误hresult中的异常:0x80005008....
- 动态绑定List<Dictionary<string, string>>和datagrid
- 如何判断一个特定的时间属于3个时间段中的一个?
- C# 中打出"/"的问题,求指教
- 怎么用C# 将现有表名为A的改名为B,并创建一个A代替原先的表A
- 想实现当用户输入一串字母后弹出一个窗口,用于后台配置,用什么方法检测用户任意时刻输入一串字母
写入之前先清除下或者释放。然后再写。