473,405 Members | 2,176 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Serial COM.Read

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

Sep 28 '07 #1
2 1900
Hi,

What serial object are you using?

If you are using VS2005, then your best bet is to use the built-in serial
port object. If you are using VS2002 or VS2003, you might want to use my
DesktopSerialIO dll (or the Serial object from www.opennetcf.org). All of
the heavy lifting is done for you. See Downloads on my homepage.

You should not PurgeComm, IMO. Suppose that data already have been buffered
by the UART FIFO, prior to this call? Data will be discarded. There really
is no benefit to be derived that I can think of.

If you were to use DesktopSerialIO, you code might look something like this
(written without testing)

Dim Timeout As Now.TimeOfDat.TotalMilliseconds + SomeNumber
Dim PacketReceived As Boolean = False
Do Until Now.TimeOfDat.TotalMilliseconds >= Timeout
With SerialPort
If .InBufferCount >= 7 Then
Dim Buffer(.InBufferCount -1) As Byte
Buffer = .InputArray()
PacketReceived = True
Exit Do
End If
End With
Loop
If PacketReceived = True Then
Process Buffer 'you write this code
Else
ThereWasATimeout
End If

This is only one way - there are several others that I can think of.

Dick
--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
See www.hardandsoftware.net for details and contact information.
Sep 29 '07 #2
BTW, if you are using VS2005, and the built-in serial port control, use the
BytesToRead property and the Read method instead.

--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
See www.hardandsoftware.net for details and contact information.
Sep 29 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: willie | last post by:
Hi, I'm writing a program which requires the use of three serial ports and one parallel port. My application has a scanning devices on each port, which I can access fine with pyserial. ...
14
by: McBooCzech | last post by:
Hi I am just trying to analyze (parse) data from the serial port (I have connected GPS receiver to the ttyS0, so I can read ASCII characters in the CSV form on the serial port 1). I am doing this...
3
by: collinm | last post by:
hi i send a command to a led display, the led display is suppose to return me some character i write a string on a serial port void ledDisplayExist() { char msg={'\0', '\0', '\0', '\0',...
6
by: Casey Bralla | last post by:
I'd like to read ASCII data from a serial port, but (once again) I'm having trouble getting started. (Can't seem to find the basic level of docs to get going <sigh>) I'd like to use only...
9
by: Mircea | last post by:
Hi everybody, I have a big problem. I am trying to use serial communication in C# 2. The problem is that the event DataReceived is never fired. I have tried on two computers and it does not...
2
by: joaquimfpinto | last post by:
Dear All, I made an app in c# that uses several serial ports. For the serial ports I use a pnp Sunix board, some with 8 serial ports other with 4 or even 2 serial ports. Whenever I use the...
4
by: rowan | last post by:
I'm writing a driver in Python for an old fashioned piece of serial equipment. Currently I'm using the USPP serial module. From what I can see all the serial modules seem to set the timeout when...
4
by: max_mont | last post by:
Hi all, I'm a newbie in .NET technology. I've already developed Serial communication applications in C++ (WIN32). And I wanted to migrate to .NET technology. There is a serial component in...
6
by: terry | last post by:
Hi, I am trying to send a character to '/dev/ttyS0' and expect the same character and upon receipt I want to send another character. I tired with Pyserial but in vain. Test Set up: 1. Send...
1
by: lutherchp | last post by:
A baffling end to my week! I open my serial port on my Debian PC (Debian version 5.0.1) I have a decent Null Modem lead going to another PC (I'll call it PC#2), with its port open with the same...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.