开发文档

主页 > 开发文档 > 猿大师中间件串口通信浏览器小程序开发文档

猿大师中间件串口通信浏览器小程序开发文档

栏目:开发文档   日期:2021-04-22 15:03:23   浏览

猿大师中间件串口通信浏览器小程序开发原理:

猿大师中间件串口通信浏览器小程序基于猿大师中间件网络版开发,底层实现调用微软串口通讯控件MSComm32.ocx,最低可用在Chrome 41、Firefox 50、Edge 80(Chromium内核)、360极速/安全、IE 8、Oprea 36、QQ、搜狗等浏览器,也兼容运行于这些浏览器的最新版本。通过此小程序,可以在浏览器中通过JS非常方便的操作COMM串口打印机、GSM 猫、手机、扫描枪、屏显、钱箱等设备。小程序中涉及到的属性等含义,请直接参考 微软串口通讯控件MSComm32.ocx中的定义,也可以参考这里:https://blog.csdn.net/shenziheng1/article/details/52884444

猿大师中间件串口通信浏览器小程序安装步骤:
 
1)、如果是exe的安装程序,请直接运行安装;
2)、如果是绿色版的程序包,请解压到磁盘某个目录后,双击“InstallWrl.bat”进行安装;
3)、安装完成时会启动猿大师中间件服务,在安装目录下的Data子目录有ZbaService.txt日志文件输出运行情况。

猿大师中间件串口通信浏览器小程序体验步骤:
 
1、访问猿大师测试网页 http://test.yuanmaster.com/
 
2、请求安装串口小程序,在测试页面上输入 ws://localhost:83?sid=123&flag=1 先连接上中间件服务,然后输入一下内容点击发送:
 
{"req":"Plugin_Install","rid":1,"para":{"Name":"串口通信浏览器小程序","PID":"C0B01CD6-7DD9-4D3C-B668-04168D5236FC","Date":"2021-03-27","Desc":"猿大师中间件之上跨浏览器的串口通信小程序,可在Chrome、Edge、Firefox、IE、Opera、360、QQ等的浏览器最新版中调用","DownAddr":"http://local.zorrosoft.com/Files/Net/WrlComm.pid","MD5":"5EAAA49BE9FA170EECE023D46B70BD4A","Version":"2.1.3.1","Size":589824,"HideIns":0,"Type":1,"Cookie":"","Auth":"","TK":"9375C9ADA73EB7FA56C1CC06C833908B805567EB9EACCE3C835C798F6E27EEE65E5D64E80494D996B871004263999FA3AA7D543183612D6B76C9B171BDF218B626DA3BE6449FB692535E609FAE56F680A4B66D3A7CCDFFF0DB3968F0132B8574E199C6A6A4BDD3EFECF8C5C4211D78602E42168B5813AE61BD9714F440C3D0755D2691D10C9D94FC220FEA42FE8646996AEAC92603FA940B1EB251CDF5A6F31DE2EC2C43762AC8764FCCB8A795D11E03D1613360FF8CFA02D6EC4DB627BA9D2EE480FB48E5065680497FC008146AEC200DA247E726E2563A7B42727D47E675928196AEB5C90B0CAE425B1FDE7516FC4FB7979F82054A4F644DBB9E6E79DD235B"}}
 
3、请求串口小程序更新:
 
{"req":"Plugin_Update","rid":1,"para":{"Name":"串口通信浏览器小程序","PID":"C0B01CD6-7DD9-4D3C-B668-04168D5236FC","Date":"2021-03-27","Desc":"猿大师中间件之上跨浏览器的串口通信小程序,可在Chrome、Edge、Firefox、IE、Opera、360、QQ等的浏览器最新版中调用","DownAddr":"http://local.zorrosoft.com/Files/Net/WrlComm_Update.pid","MD5":"04DC398D7EDC8DF8FF9CC3B3E13933D3","Version":"2.1.3.1","Size":491520,"HideIns":0,"Type":1,"Cookie":"","Auth":"","TK":"5902B5953BF79D06D5981839E9B8B997D8111C287FF2F84532D282A8AC8E5CF24A64535425098F40B9F4BC5B67192516DA85ECCE5B892EA9F858DB7819AEE16ECBC6D169E9BADEC9B08C582EEE108C10C576BECB6F44D6E6A2DBA3200AB127BFD0B65ECE66E16E5A9F25668C84D83C2794229B4E6D67E055EE6A6DB5C9B3C6EA68B3F4473E4A39016F7ECF3BFC64B81320C62504819506268E190C7F08B1D5B929E5791204EE3E9966DA28ED0967ACD01D1B958200A30509963C88306A8DD67FCCADBD2EBF3D10B75A5F34F70E341C8770A35F3AE149D896119E6F96B2DE5DE37CBD57164443E5F86922681BDCC5BC722B6CB95F2381354C64A0738560983598"}}
 
以上安装和升级的JSON请求包,可能因版本升级导致内容不同,如遇安装或升级时报错,请联系客服获取最新的请求包。 
 
4、连接请求串口通讯小程序服务:
说明:以下功能请通过Web Socket连接后可执行,如默认侦听端口不是80(关于如何配置侦听端口,请参考SDK包中的文档“猿大师开发者手册.doc”),请在连接地址中指定端口号进行连接。
打开测试网页,输入地址:ws://test.yuanmaster.com:83?sid=668&pid=407F58DA-67A5-4C74-B9F9-44A02A20B205&flag=1 点击连接;
支持的协议功能如下:
 
A、获取参数
{"req":"SP_Get","rid":1,"para":{"Items":["EventFlag","Base64","CDHolding","CDTimeout","CommID","CommPort","CTSHolding","CTSTimeout","DSRHolding","DSRTimeout","DTREnable","Handshaking","InBufferSize","InBufferCount","Break","InputLen","Interval","NullDiscard","OutBufferSize","OutBufferCount","ParityReplace","PortOpen","RThreshold","RTSEnable","Settings","SThreshold","Output","Input","CommEvent","EOFEnable","InputMode"]}} 
获取指定名称的参数值,可批量获取,也可以获取单个。其中EventFlag默认0,为1时采用事件驱动模式,收到内容自动通知到前端(SP_Event事件),Base64指定通讯内容是否经过Base64编码后传输,默认0,如果通讯内容是二进制推荐设置为1,Output和Input内容如果Base64设置为1表示是Base64编码后的结果,原通讯控件中布尔值规则1为真,0为假。其它参数都是串口通讯的属性。
返回内容格式如下:
{"ret":0,"rid":1,"data":{"EventFlag":0,"Base64":0,"CDHolding":0,"CDTimeout":0,"CommID":-1,"CommPort":1,"CTSHolding":0,"CTSTimeout":0,"DSRHolding":0,"DSRTimeout":0,"DTREnable":0,"Handshaking":0,"InBufferSize":1024,"InBufferCount":0,"Break":0,"InputLen":0,"Interval":1000,"NullDiscard":0,"OutBufferSize":1024,"OutBufferCount":0,"OutBufferCount":"?","PortOpen":0,"RThreshold":0,"RTSEnable":0,"Settings":"9600,n,8,1","SThreshold":0,"Output":"","Input":"","CommEvent":-4083,"EOFEnable":0,"InputMode":0}} 
请求Items中的每个名称都对应自己名称的值
 
简单举例1,获取当前操作的COM端口、波特率及是否打开:{"req":"SP_Get","rid":2,"para":{"Items":["PortOpen","CommPort","Settings"]}}
{"ret":0,"rid":2,"data":{"PortOpen":1,"CommPort":1,"Settings":"9600,n,8,1"}}  代表当前打开了COM1端口,使用的波特率是9600,n,8,1
简单举例2,获取端口收到的内容:{"req":"SP_Get","rid":3,"para":{"Items":["Output"]}} 
{"ret":0,"rid":3,"data":{"Output":"OK"}}  代表当前端口收到内容OK
 
B、设置参数
{"req":"SP_Put","rid":6,"para":{"CommPort":1,"CDTimeout":0,"CTSTimeout":0,"DSRTimeout":0,"DTREnable":0,"Handshaking":0,"InBufferCount":0,"Break":0,"Interval":1000,"NullDiscard":0,"OutBufferCount":0,"OutBufferCount":"?","RThreshold":0,"RTSEnable":0,"SThreshold":0,"CommEvent":0,"EOFEnable":0}} 
设置指定名称的参数值,规则和获取参数类似。
返回内容格式如下:
{"ret":0,"rid":6,"data":{"CommPort":1,"CDTimeout":1,"CTSTimeout":1,"DSRTimeout":1,"DTREnable":1,"Handshaking":1,"InBufferCount":1,"Break":1,"Interval":1,"OutBufferCount":1,"RThreshold":1,"RTSEnable":1,"SThreshold":1,"CommEvent":0,"EOFEnable":1}} 
每个设置结果,在对应的结果中,1成功,0失败
 
C、指定参数打开端口
EventFlag为1时收到内容自动通知到前端(SP_Event事件)
Base64 接收和发送的内容是否经过Base64编码
CommPort 指定打开的端口序号
Settings 设置波特率等参数如:115200,n,8,1
InBufferSize OutBufferSize 读写缓冲区大小都是1024,
InputLen 设置当前接收区数据长度为0,表示全部读取
InputMode 串口接收数据的模式 0文本,1二进制内容
RThreshold  接收缓冲区有1个以及1个以上字符时,将引发接收数据的OnComm事件
协议举例:
{"req":"SP_Open","rid":15,"para":{"EventFlag":0,"Base64":0,"CommPort":2,"Settings":"115200,n,8,1","InBufferSize":1024,"OutBufferSize":1024,"InputLen":0,"InputMode":0,"RThreshold":16}}
{"ret":0,"rid":19,"data":{"Ret":1,"Code":0}}  Ret为1代表打开成功,Code为错误时错误码
 
D、读取端口数据
{"req":"SP_Read","rid":18,"para":{}}
如果Base64设置为1,Content为Base64编码后的内容
{"ret":0,"rid":18,"data":{"Content":"OK"}}  取到Content内容为OK
 
E、写数据端口
如果Base64设置为1,则Content必须是经过Base64编码后的内容(用于二进制流) 写端口:
{"req":"SP_Write","rid":19,"para":{"Content":"AT"}}
{"ret":0,"rid":19,"data":{"Ret":1,"Code":0}}  Ret为1代表关闭成功,Code为错误时错误码
 
F、关闭端口协议:
{"req":"SP_Close","rid":20,"para":{}}
请求返回:
{"ret":0,"rid":20,"data":{"Ret":1,"Code":0}}  Ret为1代表关闭成功,Code为错误时错误码
 
5、事件通知
5.1 收到内容通知
{"event":"SP_Rec","data":{"Content":""}} 如果Base64为1,内容Content里是Base64编码后的结果,实际使用时请用Base64编码还原内容
 
5.2 发送内容通知
{"event":"SP_Send","data":{"Content":""}} 如果Base64为1,内容Content里是Base64编码后的结果,实际使用时请用Base64编码还原内容
 
5.3  通讯事件通知
{"event":"SP_Event","data":{"ID":""}} "ID为事件类型 1发送事件、3 clear-to-send 线变化、4 data-set ready 线变化、5 carrier detect 线变化、6 振铃检测、7 文件结束
 
5.3  错误事件通知
{"event":"SSP_Error","data":{"ID":""}} "ID为错误类型:
    comInvalidPropertyValue = 380,无效属性值
    comSetNotSupported = 383,属性为只读
    comGetNotSupported = 394,属性为只写
    comPortOpen = 8000,端口打开时操作不合法
    8001 超时值必须大于 0 
    comPortInvalid = 8002,无效端口号
    8003 属性只在运行时有效
    8004 属性在运行时为只读
    comPortAlreadyOpen = 8005,端口已经打开
    8006 设备标识符无效或不支持该标识符 
    8007 不支持设备的波特率
    8008 指定的字节大小无效
    8009 缺省参数错误
    8010 硬件不可用(被其它设备锁定)
    8011 函数不能分配队列 
    comNoOpen = 8012,设备没有打开
    8013 设备已经打开
    8014 不能使用通信通知 
    comSetCommStateFailed = 8015,不能设置端口状态
    8016 不能设置 comm 事件屏蔽 
    comPortNotOpen = 8018,仅当端口打开时操作才有效
    8019 设备忙
    comReadError = 8020,读端口设备错误
    comDCBError = 8021,为该端口检索设备控制块时的内部错误
 
    comBreak = 1001,接收到中断信号 
    comCTSTO = 1002, Clear-to-send 超时 
    comDSRTO = 1003,Data-set ready 超时 
    comFrame = 1004,帧错误 
    comOverrun = 1006,端口超速
    comCDTO = 1007,Carrier detect 超时 
    comRxOver = 1008,接收缓冲区溢出 
    comRxParity = 1009,Parity 错误 
    comTxFull = 1010,传输缓冲区满 
    comDCB = 1011检索端口 设备控制块 (DCB) 时的意外错误 
 
 
6、其它
请求参数的JSON数据包,请确保是UTF-8无签名的编码。
更多接口请求说明,请参考安装目录中的文档TestWrl.txt、SDK包中的“猿大师开发者手册.doc”及“猿大师中间件安全解决方案”。
如还有疑问请直接联系客服。


收缩