大二做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;
}