Activex在没有电子秤api的情况下获取串口数据

大二做B/S架构的项目使用了安衡电子秤CHS-D+R和一款扫码枪,两个设备的串口使用一样,这款电子秤是相当的坑,没有开发的api,无奈只能自己开发Activex了,在B/S架构中进行引用Activex的Guid能够达到使用本地串口的最终目的.

Activex中首先要进行安全签名的设置

[ComImport, GuidAttribute("CB5BDC81-93C1-11CF-8F20-00805F2CD064")]
[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
public interface IObjectSafety
{
[PreserveSig]
int GetInterfaceSafetyOptions(ref Guid riid, [MarshalAs(UnmanagedType.U4)] ref int pdwSupportedOptions, [MarshalAs(UnmanagedType.U4)] ref int pdwEnabledOptions);

[PreserveSig()]
int SetInterfaceSafetyOptions(ref Guid riid, [MarshalAs(UnmanagedType.U4)] int dwOptionSetMask, [MarshalAs(UnmanagedType.U4)] int dwEnabledOptions);
}

下面进行Activex的开发

private void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
//string str = serialPort.ReadLine();
//ReceiveTbox1.Text += str;
string str = serialPort.ReadLine();
textBox3.Text = str.Substring(7);

}

public string GetData()
{
string str = serialPort.ReadLine();
return str;
}

 

posted @ 2019-07-16 20:21  王小魁  阅读(722)  评论(0编辑  收藏  举报