網(wǎng)上有很多關(guān)于pos機串口號,小邁網(wǎng)關(guān)功能——通過VBScript腳本實現(xiàn)串口通信的知識,也有很多人為大家解答關(guān)于pos機串口號的問題,今天pos機之家(m.afbey.com)為大家整理了關(guān)于這方面的知識,讓我們一起來看下吧!
本文目錄一覽:
1、pos機串口號
pos機串口號
本例以讀取Modbus RTU中的9個保持型寄存器為例,演示如何通過腳本實現(xiàn)串口通信。
目標(biāo):實現(xiàn)站號為1,波特率9600,8位數(shù)據(jù)位,無校驗,1位停止位,保持型寄存器,起始地址為0,數(shù)量為9。
根據(jù)ModbusRTU協(xié)議算出需要發(fā)出的報文:
01 03 00 00 00 09 85 CC
正確返回的報文字節(jié)數(shù)為9*2=5=23個字節(jié)。
第一步:生成多個內(nèi)存變量。
Com1Connection為串口是否打開標(biāo)志。
RecvBuf為收到報文。
RecvBuf1到RecvBuf256,將收到的報文存放到這256個內(nèi)存變量。
VAL1到VAL9,實際采集到的數(shù)值。
第二步:本例需要2個腳本
第一個腳本為發(fā)Modbus報文,接受從站返回的報文,并解析到RecvBuf1到RecvBuf256的內(nèi)存變量中。
腳本內(nèi)容如下:
Dim rcBuf '接收到的報文
Dim sendBuf '發(fā)送的報文
Dim sendBuf1
Dim sendBuf2
Dim sendBuf3
Dim sendBuf4
Dim sendBuf5
Dim sendBuf6
Dim sendBuf7
Dim sendBuf9
Dim sendCode
Dim value_rcBuf
Dim dataNum
'modbus 讀,需要發(fā)送8個字節(jié)
Function GetValue(x)
b = rcBuf
MyPos = 0
i = 1
Do Until i = x
MyPos = InStr(1, b, " ", 1)
b = Right(b, (len(b) - MyPos))
i = i + 1
Loop
MyPos = InStr(1, b, " ", 1)
If MyPos > 0 Then
b = Left(b, MyPos)
MyPos = InStr(1, b, " ", 1)
If MyPos > 0 Then
b = Left(b, MyPos - 1)
End If
End If
value_rcBuf= b
End Function
'Com1Connection用于判斷是否串口打開,打開串口后設(shè)置為True,后續(xù)無需再打開
If document.GetPointValue("Com1Connection") =False Then
document.SetupConnection 1,9600,8,0,0 '串口1,波特率9600,數(shù)據(jù)位8位,無校驗,停止位1
document.OpenConnection 1,0
document.SetPointValue "Com1Connection",CBool(True),False
End If
sendCode=3
sendBuf1=CLng("&H" & 01) '站號 這里需要根據(jù)實際需求修改
sendBuf2=CLng("&H" & 03) '功能碼03 讀保持型寄存器 這里需要根據(jù)實際需求修改
sendBuf3=CLng("&H" & 00) '起始地址高字節(jié) 這里需要根據(jù)實際需求修改
sendBuf4=CLng("&H" & 00) '起始地址低字節(jié) 這里需要根據(jù)實際需求修改
sendBuf5=CLng("&H" & 00) '數(shù)據(jù)個數(shù)高8位 這里需要根據(jù)實際需求修改
sendBuf6=CLng("&H" & 09) '數(shù)據(jù)個數(shù)低8位 這里需要根據(jù)實際需求修改
dataNum=9
sendBuf7=CLng("&H" & 85)
sendBuf8 = CLng("&H" & CStr("CC"))
sendBuf=""
sendBuf= sendBuf1& " " & sendBuf2& " " & sendBuf3& " " & sendBuf4& " " & sendBuf5& " " & sendBuf6& " " & sendBuf7& " " & sendBuf8
document.WriteCommBlock 1,sendBuf,len(sendBuf)
rcBuf=document.ReadCommBlock ( 1,dataNum*2+5,500) '串口號,長度 超時時間(單位毫秒)
For i=1 To dataNum*2+5
GetValue(i)
document.SetPointValue "RecvBuf" & i,CInt(value_rcBuf),False
Next
document.SetPointValue "RecvBuf",CStr(rcBuf),False'總的報文
第二個腳本功能為將RecvBuf1到RecvBuf256中對應(yīng)的數(shù)據(jù)寫入到VAL1到VAL9
returnAddr=document.GetPointValue("RecvBuf1") '返回的站號
returnCode=document.GetPointValue("RecvBuf2") '返回的功能碼
If returnAddr=1 And returnCode=3 Then
For i=0 To 8
v1=document.GetPointValue("RecvBuf" & i*2+4 ) '4為第一個數(shù)據(jù)的高字節(jié)
v2=document.GetPointValue("RecvBuf" & i*2+5) '5為第一個數(shù)據(jù)的低字節(jié)
v=v1*256+v2
document.SetPointValue "VAL" & i+1,CInt(v),False
Next
End If
實際測試效果如下:采用Modbus Slave測試軟件測試,數(shù)值準(zhǔn)確。
函數(shù)簡介:
打開串口
BOOL OpenConnection(int m_sPort,int m_sProtocolNo)。
m_sPort:串口號,1~N,支持的最大串口數(shù)。
m_sProtocolNo:協(xié)議號,0,默認(rèn),1,廠家1,2,廠家2
例程:打開串口1,協(xié)議號0
document.OpenConnection 1,0
設(shè)置串口通訊參數(shù)
BOOL SetupConnection(int m_sPort,long m_nBaud, int m_nDataBits, int m_nParity, int m_nStopBits)。
m_sPort:串口號,1~N,支持的最大串口數(shù)。
m_nBaud:波特率,300,600,1200,4800,9600,19200,38400,57600,115200。
m_nDataBits:數(shù)據(jù)位,7,8。
m_nParity:校驗位,0,無校驗,1,奇校驗,2,偶校驗。
m_nStopBits:0,1位,1,1.5位,2,2位。
例程:設(shè)置串口1,波特率9600,數(shù)據(jù)位8位,無校驗,停止位1
document.SetupConnection 1,9600,8,0,0
操作串口發(fā)送數(shù)據(jù)
BOOL WriteCommBlock(int m_sPort,LPSTR block,long num)。
m_sPort:串口號,1~N,支持的最大串口數(shù)。
block:發(fā)送的數(shù)據(jù)
num:發(fā)送的數(shù)據(jù)長度,字節(jié)數(shù)。
例程:
v="1 3 0 0 0 1 132 10" '16進制 01 03 00 00 00 01 84 0A
document.WriteCommBlock 1,v,len(v)
操作串口接收數(shù)據(jù)
VARIANT ReadCommBlock(int m_sPort,int nMaxLength,int timeout)。
m_sPort:串口號,1~N,支持的最大串口數(shù)。
nMaxLength:要接收的數(shù)據(jù)長度,字節(jié)數(shù)。
timeout:超時,毫秒。
例程:
v="1 3 0 0 0 1 132 10" '16進制 01 03 00 00 00 01 84 0A
document.WriteCommBlock 1,v,len(v)
rtnBuf= document.ReadCommBlock(1,7,500)
關(guān)閉串口
CloseConnection(int m_sPort)。
m_sPort:串口號,1~N,支持的最大串口數(shù)。
例程:關(guān)閉串口1
document.CloseConnection(1)
更多腳本函數(shù)可以查看邁思德物聯(lián)網(wǎng)網(wǎng)關(guān)用戶手冊。
以上就是關(guān)于pos機串口號,小邁網(wǎng)關(guān)功能——通過VBScript腳本實現(xiàn)串口通信的知識,后面我們會繼續(xù)為大家整理關(guān)于pos機串口號的知識,希望能夠幫助到大家!