internal static bool RdVerMessage(byte cardAddr, out string verMessage) { byte[] buffer = new byte[1]; buffer[0] = (byte)0x01; Protocol.FrameTransmit.Send(1, cardAddr, 7, buffer, 1); StringBuilder sb = new StringBuilder(); byte[] dataBuffer = new byte[40]; FrameUnit frameUnit = FrameManager.CreateFrame(dataBuffer, 0x07); if (frameUnit.WaitData(2000) == false) { verMessage = "查询失败"; } else { verMessage = ("版本号:" + dataBuffer[8].ToString() + "." + dataBuffer[9].ToString() + "\r\n" + "版本日期:" + dataBuffer[10].ToString() + "年" + dataBuffer[11].ToString() + "月" + dataBuffer[12].ToString()); } return(true); }
internal static bool WriteCoeff(byte cardAddr, short[] CoeffValues) { byte[] dataBuffer = new byte[4000]; FrameUnit frameUnit = FrameManager.CreateFrame(dataBuffer, 0xE2); byte[] buffer = new byte[1 + CoeffValues.Length * 2]; buffer[0] = 0x52; for (int i = 0; i < CoeffValues.Length; i++) { Int16 val; val = CoeffValues[i]; buffer[i * 2 + 1] = (byte)(val & 0xff); buffer[i * 2 + 2] = (byte)((val >> 8) & 0xff); } Protocol.FrameTransmit.Send(0x01, cardAddr, 0xE2, buffer, buffer.Length); try { if (frameUnit.WaitData(3000) == false) { return(false); } else { return(true); } } catch (ThreadAbortException) { } finally { FrameManager.DeleteFrame(frameUnit); } return(false); }
static public bool ReadDebugReg(byte boardAddress, UInt16 addr, out UInt16 val) { byte[] dataBuffer = new byte[100]; FrameUnit frameUnit = FrameManager.CreateFrame(dataBuffer, 0xf0); bool ret; try { byte[] buffer = new byte[2]; buffer[0] = (byte)(addr & 0xff); buffer[1] = (byte)(addr >> 8); Protocol.FrameTransmit.Send(1, boardAddress, 0xF0, buffer, 2); if (frameUnit.WaitData(3000) == false) { val = 0; ret = false; } else { val = dataBuffer[10]; val *= 256; val += dataBuffer[9]; ret = true; } } catch (ThreadAbortException) { ret = false; val = 0; } finally { FrameManager.DeleteFrame(frameUnit); } Thread.Sleep(20); return(ret); }
internal static bool RdScaleList(byte cardAddr, out Int16[] chValues) { byte[] dataBuffer = new byte[4000]; FrameUnit frameUnit = FrameManager.CreateFrame(dataBuffer, 0xE4); byte[] buffer = new byte[1]; buffer[0] = 0x52; Protocol.FrameTransmit.Send(0x01, cardAddr, 0xE4, buffer, 1); chValues = null; try { if (frameUnit.WaitData(3000) == false) { return(false); } else { if (frameUnit.FrameLength < 20) { return(false); } chValues = new Int16[(frameUnit.FrameLength - 4 - 8) / 2]; for (int i = 8; i < frameUnit.FrameLength - 4; i += 2) { chValues[(i - 8) / 2] = (Int16)(dataBuffer[i] + dataBuffer[i + 1] * 256); } return(true); } } catch (ThreadAbortException) { } finally { FrameManager.DeleteFrame(frameUnit); } return(false); }
static public bool ReadChVal(byte cardNo, byte chNo, int timeOut, out UInt16 chVal) { byte[] dataBuffer = new byte[4000]; FrameUnit chListen; chListen = FrameManager.CreateFrame(dataBuffer, 0x30); StringBuilder sb = new StringBuilder(); bool ret; try { byte[] buffer = new byte[1]; buffer[0] = chNo; Protocol.FrameTransmit.Send(1, cardNo, 0x30, buffer, 1); if (chListen.WaitData(timeOut)) { chVal = (UInt16)(dataBuffer[8] + dataBuffer[9] * 256); ret = true; } else { chVal = 0; ret = false; } } catch { chVal = 0; ret = false; } finally { FrameManager.DeleteFrame(chListen); } return(ret); }
static public bool ReadAnalogCh(byte boardAddr, byte ch, out Int16 val) { byte[] dataBuffer = new byte[1000]; FrameUnit frameUnit = FrameManager.CreateFrame(dataBuffer, 0x30); //接收任何数据帧 bool ret; try { byte[] buffer = new byte[1]; buffer[0] = ch; Protocol.FrameTransmit.Send(1, boardAddr, 0x30, buffer, 1); if (frameUnit.WaitData(3000) == false) { val = 0; ret = false; } else { val = dataBuffer[9]; val *= 256; val += dataBuffer[8]; ret = true; } } catch (ThreadAbortException) { ret = false; val = 0; } finally { FrameManager.DeleteFrame(frameUnit); } return(ret); }
internal static bool RdIdMessage(byte cardAddr, out String id, out DateTime calcTime) { id = String.Empty; calcTime = DateTime.MinValue; byte[] dataBuffer = new byte[4000]; FrameUnit frameUnit = FrameManager.CreateFrame(dataBuffer, 0xE4); byte[] buffer = new byte[1]; buffer[0] = 0x50; Protocol.FrameTransmit.Send(0x01, cardAddr, 0xE4, buffer, 1); try { if (frameUnit.WaitData(3000) == false) { return(false); } else { if (frameUnit.FrameLength < 8 + 16 + 4) { return(false); } int charCount = 12; for (int i = 8; i < frameUnit.FrameLength - 4 && i < (8 + 12); i++) { if (dataBuffer[i] == 0x00 || dataBuffer[i] == 0xff) { charCount = i - 8; break; } } try { id = Encoding.Default.GetString(dataBuffer, 8, charCount); } catch { } try { int year = dataBuffer[8 + 12] + (dataBuffer[8 + 13] << 8); calcTime = calcTime.AddYears(year - 1); calcTime = calcTime.AddMonths(dataBuffer[8 + 14] - 1); calcTime = calcTime.AddDays(dataBuffer[8 + 15] - 1); } catch { calcTime = calcTime.AddYears(2000); } return(true); } } catch (ThreadAbortException) { } finally { FrameManager.DeleteFrame(frameUnit); } return(false); }
static public bool SendParam(byte[] paramAry) { bool ret = true; byte[] dataBuffer = new byte[40000]; byte[] frameBuffer = new byte[40000]; int LimitValue, paramLength; byte boardAddr = paramAry[0]; FrameUnit frameUnit = FrameManager.CreateFrame(dataBuffer, 1); LimitValue = GetLimitValue(paramAry[1]); try { paramLength = paramAry.Length; if (paramAry.Length > 0) { int startIndex = 0; while (paramLength > 0) { if (paramLength > LimitValue) { frameBuffer[0] = (byte)(startIndex & 0xff); frameBuffer[1] = (byte)((startIndex >> 8) & 0xff); //帧序号 frameBuffer[2] = paramAry[1]; frameBuffer[3] = 0; frameBuffer[4] = 0; for (int i = 0; i < LimitValue; i++) { frameBuffer[5 + i] = paramAry[startIndex * LimitValue + i]; } ushort crc = Helper.CRC16.ComputeCRC16(frameBuffer, 0, LimitValue + 5); frameBuffer[LimitValue + 5] = (byte)(crc & 0xff); frameBuffer[LimitValue + 6] = (byte)((crc >> 8) & 0xff); //CPU板地址为1 Protocol.FrameTransmit.Send(1, paramAry[0], 0x01, frameBuffer, LimitValue + 7); startIndex++; if (frameUnit.WaitData(500) == false) { return(false); } } else { frameBuffer[0] = (byte)(startIndex & 0xff); frameBuffer[1] = (byte)(((startIndex >> 8) & 0xff) | 0x80); //帧序号,bit15为1代表最后一帧 frameBuffer[2] = paramAry[1]; frameBuffer[3] = 0; frameBuffer[4] = 0; for (int i = 0; i < paramLength; i++) { frameBuffer[5 + i] = paramAry[startIndex * LimitValue + i]; } ushort crc = Helper.CRC16.ComputeCRC16(frameBuffer, 0, paramLength + 5); frameBuffer[paramLength + 5] = (byte)(crc & 0xff); frameBuffer[paramLength + 6] = (byte)((crc >> 8) & 0xff); //CPU板地址为1 Protocol.FrameTransmit.Send(1, paramAry[0], 0x01, frameBuffer, paramLength + 7); //发送完毕 startIndex++; if (frameUnit.WaitData(500) == false) { return(false); } } paramLength -= LimitValue; } } else { frameBuffer[0] = (byte)(0 & 0xff); frameBuffer[1] = (byte)(((0 >> 8) & 0xff) | 0x80); //帧序号,bit15为1代表最后一帧 frameBuffer[2] = paramAry[1]; frameBuffer[3] = 0; frameBuffer[4] = 0; frameBuffer[5] = paramAry[0]; frameBuffer[6] = paramAry[1]; ushort crc = Helper.CRC16.ComputeCRC16(frameBuffer, 0, 7); frameBuffer[7] = (byte)(crc & 0xff); frameBuffer[8] = (byte)((crc >> 8) & 0xff); Protocol.FrameTransmit.Send(1, paramAry[0], 0x01, frameBuffer, 9); if (frameUnit.WaitData(500) == false) { return(false); } } } catch (ThreadAbortException) { ret = false; } finally { FrameManager.DeleteFrame(frameUnit); } return(ret); }