由于程序中需要根据DBC文件来解析通讯数据,一个一个将DBC里的信号输入程序太麻烦了,且不同的设备DBC文件格式相同,但内容不同,为了程序方便解析同的设备的数据,下面以解析文本的形式将DBC文件自动加载至程序中;
public class TestDeviceVModel
{
string NS_;
string Memo_;
bool IsNs = false;
object CM_ = null;
private DeviceModel ImpDBCFile()
{
List<string> _dbc = new List<string>();
using (StreamReader _sr = new StreamReader(YiouConvert.GetOpenFilePath(), Encoding.Default))
{
string line;
while ((line = _sr.ReadLine()) != null)
{
line = line.TrimStart().Replace(" ", " "); ;
if (!string.IsNullOrWhiteSpace(line) && !line.StartsWith("//"))// 去掉空行和注释行
{
_dbc.Add(line);
}
}
_sr.Close();
}
//DeviceModel DevInfoCache = DeviceList.ToList().Find(n=>n.Name=="")??new DeviceModel();
DeviceModel DevInfoCache = new DeviceModel();
DevInfoCache.MessageList = new ObservableCollection<DevMessage>();
_dbc.ForEach(item =>
{
string[] info = item.Split(' ');
if (info.Length <= 0) { return; }
DevMessage _dev;
DevChannel _chan;
switch (info[0])
{
case "VERSION":
//GetDBCVersion(info);
break;
case "NS_":
CM_ = null;
IsNs = true;
NS_ = string.Empty;
break;
case "BS_:":
//GetNetWorkBAUD(info);
break;
case "BU_:":
//GetNetWorkNode(info);
break;
case "BO_":
_dev = GetMegInfoCache(info);
if (_dev != null)
DevInfoCache.MessageList.Add(_dev);
break;
case "SG_":
if (DevInfoCache.MessageList != null && DevInfoCache.MessageList.Count > 0)
{
_chan = GetDevChannel(info);
if (_chan != null)
{
_chan.CANID = DevInfoCache.MessageList[DevInfoCache.MessageList.Count - 1].CANID;
DevInfoCache.MessageList[DevInfoCache.MessageList.Count - 1].ChannelList.Add(_chan);
}
}
break;
case "CM_":
GetMemoContent(info, DevInfoCache);
break;
case "BA_DEF_":
break;
case "BA_DEF_DEF_":
break;
case "BA_":
break;
case "VAL_":
GetOption(info, DevInfoCache);
//_id = info.Length < 2 || string.IsNullOrWhiteSpace(info[1]) ? 0 : int.Parse((Convert.ToUInt32(info[1]) & 0x7FFFFFFF).ToString());
//_chan = DevInfoCache.MessageList.ToList().Find(n => n.MessageID == _id).ChannelList.ToList().Find(n => n.ChannelName == info[2]);
//for (int i = 3; i < info.Length; i++)
//{
// Memo_ += info[i];
//}
break;
default:
GetEnterContent(info, item);
break;
}
});
//NS_.Substring(0, NS_.Length - 1);
return DevInfoCache;
}
/// <summary>
/// 获取DBC文件版本号
/// </summary>
/// <param name="info"></param>
/// <returns></returns>
private string GetDBCVersion(string[] info)
{
string _str = string.Empty;
if (info.Length < 2 || string.IsNullOrWhiteSpace(info[1]))
{
GetEnterContent(info);
}
else
{
CM_ = null;
IsNs = false;
_str = info[1].Replace("\"", "");
}
return _str;
}
/// <summary>
/// 获取CAN网络波特率
/// </summary>
/// <param name="info"></param>
/// <returns></returns>
private string GetNetWorkBAUD(string[] info)
{
string _str = string.Empty;
if (info.Length < 2 || string.IsNullOrWhiteSpace(info[1]))
{
//波特率为空的时候info长度小于2,不应该调用“获取换行内容”
//GetEnterContent(info);
}
else
{
CM_ = null;
IsNs = false;
_str = info[1];
}
return _str;
}
/// <summary>
/// 获取CAN网络节点
/// </summary>
/// <param name="info"></param>
/// <returns></returns>
private string GetNetWorkNode(string[] info)
{
string _str = string.Empty;
if (info.Length < 2)
{
GetEnterContent(info);
}
else
{
CM_ = null;
IsNs = false;
for (int i = 1; i < info.Length; i++)
{
_str += string.IsNullOrWhiteSpace(info[i]) ? string.Empty : string.Format("{0},", info[i]);
}
_str = _str.Substring(0, _str.Length - 1);
}
return _str;
}
/// <summary>
/// 获取CAN消息
/// </summary>
/// <param name="info"></param>
/// <returns></returns>
private DevMessage GetMegInfoCache(string[] info)
{
DevMessage MegInfoCache = null;
if (info.Length != 5)
{
GetEnterContent(info);
}
else
{
CM_ = null;
IsNs = false;
MegInfoCache = new DevMessage();
MegInfoCache.ChannelList = new ObservableCollection<DevChannel>();
MegInfoCache.CANID = info.Length < 2 || string.IsNullOrWhiteSpace(info[1]) ? 0 : int.Parse((Convert.ToUInt32(info[1]) & 0x7FFFFFFF).ToString());
MegInfoCache.MessageName = info.Length < 3 || string.IsNullOrWhiteSpace(info[2]) ? string.Empty : info[2];
MegInfoCache.MessageLength = info.Length < 4 || string.IsNullOrWhiteSpace(info[3]) ? string.Empty : info[3];
MegInfoCache.MessageBUName = info.Length < 5 || string.IsNullOrWhiteSpace(info[4]) ? string.Empty : info[4];
}
return MegInfoCache;
}
/// <summary>
/// 获取CAN消息信号
/// </summary>
/// <param name="info"></param>
/// <returns></returns>
private DevChannel GetDevChannel(string[] info)
{
DevChannel _chan = null;
if (info.Length != 8)
{
GetEnterContent(info);
}
else
{
CM_ = null;
IsNs = false;
_chan = new DevChannel();
//0 SG_
//1 ECM_EngTarTq
//2 :
//3 31|11@0+
//4 (1,-1024)
//5 [-1024|1022]
//6 "Nm"
//7 TCU
_chan.ChannelName = info[1];
_chan.SigTypeDefinition = info[2];
string[] _arr = info[3].Split('|');
_chan.StartBit = _arr.Length < 1 ? 0 : YiouConvert.CInt(_arr[0], 0);
_arr = _arr.Length < 2 ? [] : _arr[1].Split('@');
_chan.SingalLen = _arr.Length < 1 ? 0 : YiouConvert.CInt(_arr[0], 0);
char[] _char = _arr.Length < 2 ? [] : _arr[1].ToCharArray();
_chan.SingalType = _char.Length < 1 ? 0 : YiouConvert.CInt(_char[0], 0);
_chan.IntType = _arr.Length < 2 ? string.Empty : _char[1].ToString();
_arr = info[4].Replace("(", "").Replace(")", "").Split(',');
_chan.Coefficient = _arr.Length < 1 ? 0 : Convert.ToDouble(_arr[0]);
_chan.Offset = _arr.Length < 2 ? 0 : Convert.ToDouble(_arr[1]);
_arr = info[5].Replace("[", "").Replace("]", "").Split('|');
_chan.DataMin = _arr.Length < 1 ? 0 : Convert.ToDouble(_arr[0]);
_chan.DataMax = _arr.Length < 2 ? 0 : Convert.ToDouble(_arr[1]);
_chan.SignalUnit = info[6].Replace("\"", "");
_chan.SigReceiveNode = info[7];
}
return _chan;
}
/// <summary>
/// 获取备注内容
/// </summary>
/// <param name="info"></param>
/// <returns></returns>
private void GetMemoContent(string[] info, DeviceModel dev)
{
if (info.Length < 2)
{
GetEnterContent(info);
}
else
{
if (!string.IsNullOrWhiteSpace(Memo_) && CM_ != null)
{
string _type = CM_.GetType().Name;
switch (_type)
{
case "DevMessage":
((DevMessage)CM_).Memo = Memo_;
break;
case "DevChannel":
((DevChannel)CM_).Memo = Memo_;
break;
case "DeviceModel":
((DeviceModel)CM_).Memo = Memo_;
break;
}
}
int _id;
CM_ = null;
IsNs = false;
Memo_ = string.Empty;
switch (info[1])
{
case "BO_":
_id = info.Length < 3 || string.IsNullOrWhiteSpace(info[2]) ? 0 : int.Parse((Convert.ToUInt32(info[2]) & 0x7FFFFFFF).ToString());
CM_ = dev.MessageList.ToList().Find(n => n.CANID == _id);
for (int i = 3; i < info.Length; i++)
{
Memo_ += string.IsNullOrWhiteSpace(info[i]) ? string.Empty : string.Format(" {0}", info[i]);
}
break;
case "SG_":
_id = info.Length < 3 || string.IsNullOrWhiteSpace(info[2]) ? 0 : int.Parse((Convert.ToUInt32(info[2]) & 0x7FFFFFFF).ToString());
CM_ = dev.MessageList.ToList().Find(n => n.CANID == _id).ChannelList.ToList().Find(n => n.ChannelName == info[3]);
for (int i = 4; i < info.Length; i++)
{
Memo_ += string.IsNullOrWhiteSpace(info[i]) ? string.Empty : string.Format(" {0}", info[i]);
}
break;
case "BU_":
break;
default:
CM_ = dev;
for (int i = 1; i < info.Length; i++)
{
Memo_ += string.IsNullOrWhiteSpace(info[i]) ? string.Empty : string.Format(" {0}", info[i]);
}
break;
}
Memo_ = string.IsNullOrWhiteSpace(Memo_) ? string.Empty : Memo_.Substring(1, Memo_.Length - 1).Replace("\"", "");
}
}
/// <summary>
/// 获取数值表示
/// </summary>
/// <returns></returns>
private void GetOption(string[] info, DeviceModel dev)
{
if (info.Length < 4)
{
GetEnterContent(info);
}
else
{
CM_ = null;
IsNs = false;
Memo_ = string.Empty;
int _id = info.Length < 2 || string.IsNullOrWhiteSpace(info[1]) ? 0 : int.Parse((Convert.ToUInt32(info[1]) & 0x7FFFFFFF).ToString());
DevChannel _chan = dev.MessageList.ToList().Find(n => n.CANID == _id).ChannelList.ToList().Find(n => n.ChannelName == info[2]);
for (int i = 3; i < info.Length; i++)
{
}
}
}
/// <summary>
/// 获取换行内容
/// </summary>
/// <param name="info"></param>
/// <param name="item"></param>
private void GetEnterContent(string[] info, string item = "")
{
if (IsNs)
{
NS_ += string.Format("{0},", info[0]);
}
if (CM_ != null)
{
Memo_ += item.Replace("\"", "");
}
}
}
public class DeviceModel : NotifyPropertyChangedBase
{
public DeviceModel()
{
_MessageList = new ObservableCollection<DevMessage>();
}
public int ID
{
get { return _ID; }
set { _ID = value; OnPropertyChanged("ID"); }
}
/// <summary>
/// 设备名称
/// </summary>
public string Name
{
get { return _Name; }
set { _Name = value; OnPropertyChanged("Name"); }
}
/// <summary>
/// 设备型号
/// </summary>
public string DeviceType
{
get { return _DeviceType; }
set { _DeviceType = value; OnPropertyChanged("DeviceType"); }
}
/// <summary>
/// 设备状态
/// </summary>
public string DeviceStatus
{
get { return _DeviceStatus; }
set { _DeviceStatus = value; OnPropertyChanged("DeviceStatus"); }
}
/// <summary>
/// 通讯类型
/// </summary>
public string CommType
{
get { return _CommType; }
set { _CommType = value; OnPropertyChanged("CommType"); }
}
/// <summary>
/// 设备参数
/// </summary>
public string CommPara
{
get { return _CommPara; }
set { _CommPara = value; OnPropertyChanged("CommPara"); }
}
/// <summary>
/// 备注
/// </summary>
public string Memo
{
get { return _Memo; }
set { _Memo = value; OnPropertyChanged("Memo"); }
}
/// <summary>
/// 消息列表
/// </summary>
public ObservableCollection<DevMessage> MessageList
{
get { return _MessageList; }
set { _MessageList = value; OnPropertyChanged("MessageList"); }
}
int _ID;
string _Name;
string _DeviceType;
string _DeviceStatus;
string _CommType;
string _CommPara;
string _Memo;
ObservableCollection<DevMessage> _MessageList;
}
public class DevMessage : NotifyPropertyChangedBase
{
public DevMessage()
{
_ChannelList = new ObservableCollection<DevChannel>();
}
public int ID
{
get { return _ID; }
set { _ID = value; OnPropertyChanged("ID"); }
}
/// <summary>
/// 设备ID
/// </summary>
public int DevID
{
get { return _DevID; }
set { _DevID = value; OnPropertyChanged("DevID"); }
}
/// <summary>
/// 消息ID
/// </summary>
public int CANID
{
get { return _CANID; }
set { _CANID = value; OnPropertyChanged("CANID"); }
}
/// <summary>
/// 消息名称
/// </summary>
public string MessageName
{
get { return _MessageName; }
set { _MessageName = value; OnPropertyChanged("MessageName"); }
}
/// <summary>
/// 消息长度
/// </summary>
public string MessageLength
{
get { return _MessageLength; }
set { _MessageLength = value; OnPropertyChanged("MessageLength"); }
}
/// <summary>
/// 消息节点名称
/// </summary>
public string MessageBUName
{
get { return _MessageBUName; }
set { _MessageBUName = value; OnPropertyChanged("MessageBUName"); }
}
/// <summary>
/// 备注
/// </summary>
public string Memo
{
get { return _Memo; }
set { _Memo = value; OnPropertyChanged("Memo"); }
}
/// <summary>
/// 信号列表
/// </summary>
public ObservableCollection<DevChannel> ChannelList
{
get { return _ChannelList; }
set { _ChannelList = value; OnPropertyChanged("ChannelList"); }
}
int _ID;
int _DevID;
int _CANID;
string _MessageName;
string _MessageLength;
string _MessageBUName;
string _Memo;
ObservableCollection<DevChannel> _ChannelList;
}
public class DevChannel : NotifyPropertyChangedBase
{
public int ID
{
get { return _ID; }
set { _ID = value; OnPropertyChanged("ID"); }
}
/// <summary>
/// 设备ID
/// </summary>
public int DevID
{
get { return _DevID; }
set { _DevID = value; OnPropertyChanged("DevID"); }
}
/// <summary>
/// 报文ID *
/// </summary>
public int CANID
{
get { return _CANID; }
set { _CANID = value; OnPropertyChanged("CANID"); }
}
/// <summary>
/// 消息ID *
/// </summary>
public int MessageID
{
get { return _MessageID; }
set { _MessageID = value; OnPropertyChanged("MessageID"); }
}
/// <summary>
/// 信号名称
/// </summary>
public string ChannelName
{
get { return _ChannelName; }
set { _ChannelName = value; OnPropertyChanged("ChannelName"); }
}
/// <summary>
/// 多路信号定义
/// </summary>
public string SigTypeDefinition
{
get { return _SigTypeDefinition; }
set { _SigTypeDefinition = value; OnPropertyChanged("SigTypeDefinition"); }
}
/// <summary>
/// 起始数据位 *
/// </summary>
public int StartBit
{
get { return _StartBit; }
set { _StartBit = value; OnPropertyChanged("StartBit"); }
}
/// <summary>
/// 信号数据长度
/// </summary>
public int SingalLen
{
get { return _SingalLen; }
set { _SingalLen = value; OnPropertyChanged("SingalLen"); }
}
/// <summary>
/// 修正系数
/// </summary>
public double Coefficient
{
get { return _Coefficient; }
set { _Coefficient = value; OnPropertyChanged("Coefficient"); }
}
/// <summary>
/// 偏移量
/// </summary>
public double Offset
{
get { return _Offset; }
set { _Offset = value; OnPropertyChanged("Offset"); }
}
/// <summary>
/// 信号最小值 *
/// </summary>
public double DataMin
{
get { return _DataMin; }
set { _DataMin = value; OnPropertyChanged("DataMin"); }
}
/// <summary>
/// 信号最大值 *
/// </summary>
public double DataMax
{
get { return _DataMax; }
set { _DataMax = value; OnPropertyChanged("DataMax"); }
}
/// <summary>
/// 信号类型
/// </summary>
public int SingalType
{
get { return _SingalType; }
set { _SingalType = value; OnPropertyChanged("SingalType"); }
}
/// <summary>
/// 信号单位
/// </summary>
public string SignalUnit
{
get { return _SignalUnit; }
set { _SignalUnit = value; OnPropertyChanged("SignalUnit"); }
}
/// <summary>
/// 信号数值类型
/// </summary>
public string IntType
{
get { return _IntType; }
set { _IntType = value; OnPropertyChanged("IntType"); }
}
/// <summary>
/// 信号接收节点
/// </summary>
public string SigReceiveNode
{
get { return _SigReceiveNode; }
set { _SigReceiveNode = value; OnPropertyChanged("SigReceiveNode"); }
}
/// <summary>
/// 信号值映射通道
/// </summary>
public string ValueMapp
{
get { return _ValueMapp; }
set { _ValueMapp = value; OnPropertyChanged("ValueMapp"); }
}
/// <summary>
/// 备注
/// </summary>
public string Memo
{
get { return _Memo; }
set { _Memo = value; OnPropertyChanged("Memo"); }
}
int _ID;
int _DevID;
int _CANID;
int _MessageID;
string _ChannelName;
int _StartBit;
int _SingalLen;
double _Coefficient;
double _Offset;
double _DataMin;
double _DataMax;
int _SingalType;
string _SignalUnit;
string _SigTypeDefinition;
string _IntType;
string _SigReceiveNode;
string _ValueMapp;
string _Memo;
}