緣分2013
級(jí)別: 探索解密
|
RT,小弟由于剛接觸VB不久,用VB6.0與FX2N通訊,向PLC的D寫入值老是不對(duì),不知道是不是格式不對(duì),我是利用MSComm控件通訊的。。。outstring = Chr(&O2) + "1" + CStr(qiuhe) + "02" + CStr(xieru) + Chr(&O3) + hejiaoyan |
---|---|
|
zsm9614501
級(jí)別: 略有小成
|
VB6.0與FX2N通訊連接,可以用三凌MX-COMPONENT中的通訊控件,很容易連接的 |
|
---|---|---|
|
tk758
級(jí)別: 略有小成
|
Private Sub Command1_Click() '寫入D0=0 Dim Address As String Dim SenData As String MSComm1.Settings = "9600,E,7,1" MSComm1.InputMode = comInputModeText Address = "1" + "1000" + "02" + "0000" + Chr(3) SenData = Chr(2) & Address & SumChk(Address) MSComm1.PortOpen = True MSComm1.Output = SenData End Sub Private Function SumChk(Dats$) As String 'CheckSum Dim i& Dim CHK& For i = 1 To Len(Dats) CHK = CHK + Asc(Mid(Dats, i, 1)) Next i SumChk = Right(Hex$(CHK), 2) End Function |
---|---|
|
緣分2013
級(jí)別: 探索解密
|
回復(fù)3樓,為什么我把"0000"改成0001實(shí)際寫進(jìn)去的數(shù)據(jù)是256,改成0002寫進(jìn)去的數(shù)據(jù)是512,0000是需要寫進(jìn)去的數(shù)據(jù)的16進(jìn)制數(shù)據(jù)還是其他的關(guān)系? |
---|---|
|
tk758
級(jí)別: 略有小成
|
寫入的數(shù)據(jù)16進(jìn)制數(shù)據(jù), 是先寫低字節(jié), .再寫高字節(jié). 因此如寫"1"進(jìn)去PLC, 應(yīng)寫成"0100", 寫"255"進(jìn)去PLC, 應(yīng)寫成"FF00" |
---|---|
|
緣分2013
級(jí)別: 探索解密
|
謝謝你呀,昨天通過(guò)你這個(gè)代碼和我自己的結(jié)合找出了這個(gè)規(guī)律,現(xiàn)在還有個(gè)問(wèn)題請(qǐng)教下,怎樣把動(dòng)態(tài)的text.text賦給一個(gè)變量,意思就是通過(guò)一個(gè)函數(shù)比如說(shuō)date(P),p是text的下標(biāo),然后把textp。text賦給一個(gè)變量,能實(shí)現(xiàn)嗎? |
---|---|
|
tk758
級(jí)別: 略有小成
|
‘可用數(shù)組變量 Dim strText(10) As String Private Sub Form_Load() For i = 0 To 10 strText(i) = Text1.Text & i List1.AddItem strText(i) Next i End Sub |
---|---|
|
緣分2013
級(jí)別: 探索解密
|
嗯,真的非常感謝你呀,還有個(gè)問(wèn)題困擾了我好久了 Private Sub Timer2_Timer() MSComm1.InBufferCount = 0 '設(shè)置讀緩沖區(qū)為空 MSComm1.OutBufferCount = 0 '設(shè)置寫緩沖區(qū)為空 outstring1 = Chr(&O2) + "0" + "0080" + "02" + Chr(&O3) + SUM_check1("0080", "02") MSComm1.Output = outstring1 Do DoEvents Loop Until MSComm1.InBufferCount >= 8 End Sub 把這個(gè)放在timer事件里面進(jìn)行掃描,但是我用其他按鈕的單擊事件比如說(shuō) MSComm1.Output = Chr(&O2) + "7" + "0008" + Chr(&O3) + SUM_check2(7, "0008") 經(jīng)常出現(xiàn)timer事件卡死,不知道是不是在使用單擊事件的時(shí)候跟 Do DoEvents Loop Until MSComm1.InBufferCount >= 8 產(chǎn)生死循環(huán),但是沒(méi)有這個(gè)釋放控制權(quán)的又無(wú)法正常掃描狀態(tài),指導(dǎo)下。。。 |
---|---|
|