简单的串口通讯
--------------------编程问答-------------------- 调用方法
请告诉我怎样使用这个方法,第一个参数应该怎么填写
/// </summary>
/// <param name="ReadBuf">串口数据缓冲</param>
/// <param name="ReadRoom">串口数据缓冲空间大小</param>
/// <param name="ByteTime">字节间隔最大时间</param>
/// <returns>从串口实际读入的字节个数</returns>
public int ReadBlock(out byte[] ReadBuf, int ReadRoom, int ByteTime)
{
//throw new System.NotImplementedException();
ReadBuf = new byte[1024];
Array.Clear(ReadBuf, 0, ReadBuf.Length);
sbyte nBytelen;
//long nByteRead;
if (serialPort1.IsOpen == false)
return 0;
nBytelen = 0;
serialPort1.ReadTimeout = ByteTime;
while (nBytelen < (ReadRoom - 1))
{
try
{
ReadBuf[nBytelen] = (byte)serialPort1.ReadByte();
nBytelen++; // add one
}
catch (Exception ex)
{
throw new Exception(ex.Message);
break;
}
}
ReadBuf[nBytelen] = 0x00;
return nBytelen;
}
byte[] ReadBuf;
ReadBlock(out byte[] ReadBuf, 10, 1000);
简单说明下:ReadBuf是返回结果,10读取次数,1000为延时时间
--------------------编程问答-------------------- 直接传递一个数组变量,不需要初始化,该函数内部实现了初始化过程 --------------------编程问答--------------------
哦 没注意 楼主注释!!
调用就这么调吧。说明不用看了,嘿嘿。瞎猜的。 --------------------编程问答-------------------- 对的
补充:.NET技术 , C#