namespace Test
{
[Kingdee.BOS.Util.HotUpdate]
[Description("读取串口发送的数据")]
public class ComTest : AbstractBillPlugIn
{
private string netWeightData = string.Empty;
public override void AfterBindData(EventArgs e)
{
base.AfterBindData(e);
KDSerialPortConfig cfg = new KDSerialPortConfig();
cfg.PortName = "COM2";
cfg.Rate = 1200;
cfg.Parity = 0;
cfg.Bits = 8;
cfg.StopBits = 1;
cfg.Timeout = -1;
cfg.EncodingName = "ASCII";//new KDSerialPortConfig[]{cfg}
SerialPortControl SPC= this.View.GetControl<SerialPortControl>("F_RPIH_SerialPortCtrl");
SPC.Init(cfg);
}
/// <summary>
/// 接受串口发送的数据
/// </summary>
/// <param name="e"></param>
public override void PortDataReceived(KDSerialPortEventArgs e)
{
base.PortDataReceived(e);
netWeightData += e.Value.ToString();
this.Model.SetValue("F_RPIH_Remarks", HexToStr(netWeightData));
}
// 返回十六进制代表的字符串
public string HexToStr(string mHex)
{
mHex = mHex.Replace(" ", "");
if (mHex.Length <= 0)
{
return "";
}
byte[] vBytes = new byte[mHex.Length / 2];
for (int i = 0; i < mHex.Length; i += 2)
{
if (!byte.TryParse(mHex.Substring(i, 2), NumberStyles.HexNumber, null, out vBytes[i / 2]))
vBytes[i / 2] = 0;
}
return ASCIIEncoding.Default.GetString(vBytes);
}
}
}
推荐阅读