110,580
社区成员
发帖
与我相关
我的任务
分享
string ComportName = "COM1";
comPort = new SerialPort(ComportName, 14400, Parity.None, 8, StopBits.One);
comPort.ReadTimeout = 100;
comPort.WriteTimeout = 100;
comPort.DataReceived += ByteReceived;
//comPort.ReceivedBytesThreshold = 1;
// comPort.WriteBufferSize = 3;
comPort.Open();
int byteToRead = comPort.BytesToRead;
byte[] resultByte = new byte[byteToRead];
comPort.Read(resultByte, 0, byteToRead);
while (buffer.Count >= 12)
{
byte CRCH, CRCL; //这个是我CRC校验用的,
byte[] crcTemp = new byte[10];
buffer.CopyTo(0, crcTemp, 0, 10);
CRCData.CalculateCrc16(crcTemp, out CRCH, out CRCL);//一直到这 这个是我CRC校验用的,
if (buffer[10] != CRCH && buffer[11] != CRCL)//如果CRC校验不对,做下面的处理
{
buffer.RemoveAt(0);//删除BUFF中的第一个数据。
continue;//继续下一次循环
}
//系统延时
public void Delay(int ys)
{
for (int i = 1; i <= ys; i++)
{
//使用sleep(1)会比sleep(1000)好,因为让进程循环1000休息1ms=1s比直接让系统休眠1s好
//如果直接让进程休眠1s,让人感觉进程好像卡了,反应很慢。
System.Threading.Thread.Sleep(1);
Application.DoEvents();
}
}