110,534
社区成员
发帖
与我相关
我的任务
分享
Byte[] BSendTemp = { 0x00, 0xff, 0xff, 0xcc, 0x01, 0x01, 0x02, 0x40, 0x00, 0x44 }; //建立临时字节数组对象
serialPort.Write(BSendTemp, 0, 10);//发送命令
Thread.Sleep(4000); // 等待设备响应。
int count;
do
{
count = serialPort.BytesToRead; // 读取缓冲区数据长度
Thread.Sleep(10); // 延时10毫秒
} while (count != serialPort.BytesToRead); // 判断刚才读取的长度与缓冲区长度相同表示已经接收完成,如果不同则将缓冲区长度赋给cout再延时10毫秒判断
serialPort.Read(BReceived, 0, count);
MessageBox.Show(count.ToString());
private void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
int bytes = 0;
String strMoney = String.Empty;//钱数
String strNumber = String.Empty;//币数
String strMoneyAndNumber = String.Empty;//钱数+币数
bytes = serialPort.BytesToRead;//读取串口中的数据
byte[] buffer = new byte[bytes];//转换成数组
for (int i = 0; i < bytes; i++)
{
buffer[i] = (byte)(serialPort.ReadByte());//循环遍历每个元素
}
if (bytes != 0)//判断数组是否为0
{.......}
System.IO.MemoryStream ms = new System.IO.MemoryStream(100);
void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
byte[] bytes = new byte[100];
int count = serialPort.Read(bytes, 0, 100);
ms.Write(bytes, 0, count);
}
Byte[] BSendTemp = { 0x00, 0xff, 0xff, 0xcc, 0x01, 0x01, 0x02, 0x40, 0x00, 0x44 }; //建立临时字节数组对象
ms.Position = 0; --将内存流重置
serialPort.Write(BSendTemp, 0, 10);//发送命令
MessageBox.Show(ms.Position.ToString());--内存流当前位置就是接收到的字节数