欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-11-16 17:06:52
大二做B/S架构的项目使用了安衡电子秤CHS-D+R和一款扫码枪,两个设备的串口使用一样,这款电子秤是相当的坑,没有开发的api,无奈只能自己开发Activex了,在B/S架构中进行引用Activex的Guid能够达到使用本地串口的最终目的. Activex中首先要进行安全签名的设置 [ComImp ......

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

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

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;
}