C
cmdolcet69
Can anyone help me out? I have some code below that writes a packet of
bytes into the Serial COM. When i call the Ocp.Read (7) i pass in all
7 bytes and i can;t get it to return byte(2) and byte(3) position?
The mabtRxBuf stores all the byte written in however it return an
integer i want it to return the byte (2) and byte(3) so i can convert
this into an integer myself....
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Try
Packet(0) = 42 'Constant BMS message
Packet(1) = 4 'Constant Length of Table upload
Packet(2) = 6 ' Constant Command of Table upload
Packet(3) = 23
Packet(4) = 152
oCP.Write(Packet, 0, 4)
oCP.Read(7)
Thread.Sleep(5)
WriteMessage(TextBox1.Text, True)
Catch ex As Exception
' Warn the user.
MessageBox.Show("Unable to write to comm port")
Finally
TextBox1.Text = ""
TextBox1.Focus()
End Try
End Sub
'This is the oCp.Read function
Public Function Read(ByVal Bytes2Read As Integer) As Integer
Dim iReadChars, iRc As Integer
' If Bytes2Read not specified uses Buffersize
If Bytes2Read = 0 Then Bytes2Read = miBufferSize
If mhRS = -1 Then
Throw New ApplicationException( _
"Please initialize and open port before using this
method")
Else
' Get bytes from port
Try
' Purge buffers
'PurgeComm(mhRS, PURGE_RXCLEAR Or PURGE_TXCLEAR)
' Creates an event for overlapped operations
If meMode = Mode.Overlapped Then
pHandleOverlappedRead(Bytes2Read)
Else
' Non overlapped mode
ReDim mabtRxBuf(Bytes2Read - 1)
iRc = ReadFile(mhRS, mabtRxBuf, Bytes2Read,
iReadChars, Nothing)
If iRc = 0 Then
' Read Error
Throw New ApplicationException( _
"ReadFile error " & iRc.ToString)
Else
' Handles timeout or returns input chars
If iReadChars < Bytes2Read Then
Throw New IOTimeoutException("Timeout
error")
Else
mbWaitOnRead = True
Return (iReadChars)
End If
End If
End If
Catch Ex As Exception
' Others generic erroes
Throw New ApplicationException("Read Error: " &
Ex.Message, Ex)
End Try
End If
End Function
bytes into the Serial COM. When i call the Ocp.Read (7) i pass in all
7 bytes and i can;t get it to return byte(2) and byte(3) position?
The mabtRxBuf stores all the byte written in however it return an
integer i want it to return the byte (2) and byte(3) so i can convert
this into an integer myself....
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Try
Packet(0) = 42 'Constant BMS message
Packet(1) = 4 'Constant Length of Table upload
Packet(2) = 6 ' Constant Command of Table upload
Packet(3) = 23
Packet(4) = 152
oCP.Write(Packet, 0, 4)
oCP.Read(7)
Thread.Sleep(5)
WriteMessage(TextBox1.Text, True)
Catch ex As Exception
' Warn the user.
MessageBox.Show("Unable to write to comm port")
Finally
TextBox1.Text = ""
TextBox1.Focus()
End Try
End Sub
'This is the oCp.Read function
Public Function Read(ByVal Bytes2Read As Integer) As Integer
Dim iReadChars, iRc As Integer
' If Bytes2Read not specified uses Buffersize
If Bytes2Read = 0 Then Bytes2Read = miBufferSize
If mhRS = -1 Then
Throw New ApplicationException( _
"Please initialize and open port before using this
method")
Else
' Get bytes from port
Try
' Purge buffers
'PurgeComm(mhRS, PURGE_RXCLEAR Or PURGE_TXCLEAR)
' Creates an event for overlapped operations
If meMode = Mode.Overlapped Then
pHandleOverlappedRead(Bytes2Read)
Else
' Non overlapped mode
ReDim mabtRxBuf(Bytes2Read - 1)
iRc = ReadFile(mhRS, mabtRxBuf, Bytes2Read,
iReadChars, Nothing)
If iRc = 0 Then
' Read Error
Throw New ApplicationException( _
"ReadFile error " & iRc.ToString)
Else
' Handles timeout or returns input chars
If iReadChars < Bytes2Read Then
Throw New IOTimeoutException("Timeout
error")
Else
mbWaitOnRead = True
Return (iReadChars)
End If
End If
End If
Catch Ex As Exception
' Others generic erroes
Throw New ApplicationException("Read Error: " &
Ex.Message, Ex)
End Try
End If
End Function