Option Explicit Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long) As Long Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long Private Type POINTAPI X As Long Y As Long End Type Dim dc As Long Dim p As POINTAPI Public data As String Sub Form_Load() Timer1.Interval = 200 Timer1.Enabled = True MSComm1.CommPort = 1 MSComm1.Settings = "9600,E,7,1" '设置串口参数 MSComm1.InputMode = comInputModeText MSComm1.InBufferSize = 1024 MSComm1.PortOpen = True End Sub Private Sub Timer1_Timer() GetColorUnderCursor GetColorUndersCursor Dim receiveData As String AutoRedraw = True receiveData = MSComm1.Input Text1 = receiveData '打印PLC返回的数据 'MSComm1.PortOpen = False End Sub Private Sub GetColorUnderCursor() GetCursorPos p Cls AutoRedraw = True dc = GetDC(0) Print GetPixel(dc, p.X, p.Y) ReleaseDC 0, dc End Sub Private Sub GetColorUndersCursor() GetCursorPos p dc = GetDC(0) Dim color As Long color = GetPixel(dc, p.X, p.Y) ReleaseDC 0, dc Dim data As String If color = 11250603 Then data = Chr(2) & "110C8020200" & Chr(3) & "34" Text2.Text = data MSComm1.Output = data End If End Sub mscomm1与PLC通信协议为RS-232c,要求修改代码,让plc返回值为十六进制
时间: 2024-01-03 08:04:16 浏览: 145
java中long数据类型转换为int类型
可以将返回的值转换为十六进制格式,代码如下所示:
```
Option Explicit
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long
Private Type POINTAPI
X As Long
Y As Long
End Type
Dim dc As Long
Dim p As POINTAPI
Public data As String
Sub Form_Load()
Timer1.Interval = 200
Timer1.Enabled = True
MSComm1.CommPort = 1
MSComm1.Settings = "9600,E,7,1" '设置串口参数
MSComm1.InputMode = comInputModeText
MSComm1.InBufferSize = 1024
MSComm1.PortOpen = True
End Sub
Private Sub Timer1_Timer()
GetColorUnderCursor
GetColorUndersCursor
Dim receiveData As String
AutoRedraw = True
receiveData = MSComm1.Input
Text1 = receiveData '打印PLC返回的数据
'MSComm1.PortOpen = False
End Sub
Private Sub GetColorUnderCursor()
GetCursorPos p
Cls
AutoRedraw = True
dc = GetDC(0)
Print GetPixel(dc, p.X, p.Y)
ReleaseDC 0, dc
End Sub
Private Sub GetColorUndersCursor()
GetCursorPos p
dc = GetDC(0)
Dim color As Long
color = GetPixel(dc, p.X, p.Y)
ReleaseDC 0, dc
Dim data As String
If color = 11250603 Then
data = Chr(2) & "110C8020200" & Chr(3) & "34"
Text2.Text = data
MSComm1.Output = data
Dim receiveData As String
receiveData = MSComm1.Input
If receiveData <> "" Then
Dim hexData As String
Dim i As Integer
For i = 1 To Len(receiveData)
hexData = hexData & Hex(Asc(Mid(receiveData, i, 1)))
Next i
Text1.Text = hexData
End If
End If
End Sub
```
在GetColorUndersCursor子程序中,添加了转换为十六进制的代码。当PLC返回数据时,会将数据转换为十六进制格式,然后在Text1中显示。
阅读全文