public override string Read(SerialPort serialPort) { int tempdatalenth = serialPort.BytesToRead; byte[] tempbytes = new byte[tempdatalenth]; serialPort.Read(tempbytes, 0, tempdatalenth); return(ConvertFrom.ToHexString(tempbytes)); }
private void MySerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e) { if (mySerialPort.IsOpen) { strlist.Clear(); int tempdatalenth = mySerialPort.BytesToRead; tempbytes = new byte[tempdatalenth]; mySerialPort.Read(tempbytes, 0, tempdatalenth); strlist.Add(ConvertFrom.ToHexString(tempbytes)); } if (tempbytes.Length > 0) { foreach (var item in strlist) { showMessageEvent(item); } } else { return; } }
unsafe protected string ReceiveData(uint FilterID, int timeout, uint deviceType, uint deviceIndex, uint cANIndex) { string dataStrList = $"读取{FilterID}失败!"; uint num = VCI_GetReceiveNum(deviceType, deviceIndex, cANIndex); if (num > 0) { var cancelTokenSource = new CancellationTokenSource(timeout); List <byte> Data_Frame = new List <byte>(); if (IsOpen) //是否打开CAN { while (!cancelTokenSource.IsCancellationRequested) //设置读取超时 { #region .net //UInt32 con_maxlen = 50; //int size = Marshal.SizeOf(typeof(VCI_CAN_OBJ)); //IntPtr pt = Marshal.AllocHGlobal(size * (int)con_maxlen); //uint result = VCI_Receive(deviceType, deviceIndex, cANIndex, pt, con_maxlen, 100); //for (uint i = 0; i < result; i++) //{ // VCI_CAN_OBJ obj = (VCI_CAN_OBJ)Marshal.PtrToStructure((IntPtr)((uint)pt + i * size), typeof(VCI_CAN_OBJ)); // if (obj.ID == FilterID) // { // if (obj.RemoteFlag == 0) // { // List<uint> temp = new List<uint>(); // byte len = (byte)(obj.DataLen % 9); // byte j = 0; // if (j++ < len) // { // for (int d = 0; d < len; d++) // { // temp.Add(obj.Data[d]); // } // temp.RemoveRange(0, 2);//去除CAN协议头俩位数据(东峻专用) // uintlist.AddRange(temp); // } // } // } #endregion #region 指针 VCI_CAN_OBJ[] canObj = new VCI_CAN_OBJ[100]; fixed(VCI_CAN_OBJ *pcanObj = canObj) { VCI_CAN_OBJ *pNewcanObj = pcanObj; int szie = sizeof(VCI_CAN_OBJ) * canObj.Length; uint result = VCI_Receive(deviceType, deviceIndex, cANIndex, pcanObj, 100, 400); for (uint i = 0; i < result; i++) { if (pNewcanObj->ID == FilterID) //判断是否为过滤的ID { if (pNewcanObj->RemoteFlag == 0) //判断是否为远程幁 { List <byte> temp = new List <byte>(); byte * pdata = pNewcanObj->Data + 2;//每一帧的前两位去掉(帧头和长度) for (int t = 0; t < 6; t++) { temp.Add(*pdata); pdata++; } Data_Frame.AddRange(temp); } } pNewcanObj++; } } #endregion #region old //string dataStr = string.Empty; //dataStr += Convert.ToString(obj.ID, 16); //if (obj.RemoteFlag == 0) //{ // //dataStr += " 数据: "; // byte len = (byte)(obj.DataLen % 9); // byte j = 0; // if (j++ < len) // { // for (int d = 0; d < len; d++) // { // dataStr += " " + string.Format("{0:X2}", obj.Data[d]); // } // } // if (obj.ID == FilterID)//0x77B // { // //dataStringList.Add(dataStr.ToUpper()); // } //} ////Console.WriteLine(dataStr.ToUpper()); //if (dataStringList.Count > 0) //{ // foreach (var item in dataStringList) // { // dataStrList += item; // } // return dataStrList; //}//判断是否有 指定的数据 //else //{ // dataStringList = null; // dataStrList = ""; // //ReceiveData(FilterID, timeout); // return $"CAN1 读取帧ID: {Convert.ToString(FilterID, 16).ToUpper()} 失败!"; //} //return $"CAN1 读取帧ID: {Convert.ToString(FilterID, 16).ToUpper()} 失败!"; #endregion } } if (Data_Frame.Count > 0) { Data_Frame.RemoveRange(0, 2); return(dataStrList = ConvertFrom.ToHexString(Data_Frame.ToArray())); } } return(dataStrList); }