473,468 Members | 1,307 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Rading values from a Serial Port in VB2005

The sub below reads the value of position from my serial write
command. However i need to read only the (3) and (4) index of the byte
array and then need to convert it into a integer. When i get to the
step invalue = bitconverter.toint32(Newpacket,0) line i trigger an
error saying that the byte array is less than the read byte array.
How can i only grab position (3) and (4) and then return there values
in integer.

Public invalue as integer

Public Sub ProbeBitValues()
Dim Packet(7) As Byte
Dim NewPacket(2) As Byte

SerialPort2.Read(Packet, 0, 7)
NewPacket(0) = Packet(3)
NewPacket(1) = Packet(4)
indvalue = BitConverter.ToInt32(NewPacket, 0)
End Sub
Nov 16 '07 #1
9 1605
"cmdolcet69" <co************@hotmail.comschrieb
The sub below reads the value of position from my serial write
command. However i need to read only the (3) and (4) index of the
byte array and then need to convert it into a integer.
I told you several times that an Integer consists of 4 Bytes.

Integer = Int32 = 32 bits = 4 Bytes
Short = Int16 = 16 bits = 2 Bytes

Still.
When i get to
the step invalue = bitconverter.toint32(Newpacket,0) line i trigger
an error saying that the byte array is less than the read byte
array. How can i only grab position (3) and (4) and then return
there values in integer.

Public invalue as integer

Public Sub ProbeBitValues()
Dim Packet(7) As Byte

You know that Packet contains 8 Bytes?

Dim NewPacket(2) As Byte

You know that NewPacket contains 3 Bytes?

SerialPort2.Read(Packet, 0, 7)
NewPacket(0) = Packet(3)
NewPacket(1) = Packet(4)
indvalue = BitConverter.ToInt32(NewPacket, 0)
End Sub
Why do you put the two bytes into a new array? You can pass variable Packet
to BitConverter.ToInt32 and specify the index 3 as the start index. But
again, this will take bytes at position 3, 4, 5 and 6, not only 3 and 4.
Armin

Nov 16 '07 #2
On Nov 16, 10:55 am, "Armin Zingler" <az.nos...@freenet.dewrote:
"cmdolcet69" <colin_dolce...@hotmail.comschrieb
The sub below reads the value of position from my serial write
command. However i need to read only the (3) and (4) index of the
byte array and then need to convert it into a integer.

I told you several times that an Integer consists of 4 Bytes.

Integer = Int32 = 32 bits = 4 Bytes
Short = Int16 = 16 bits = 2 Bytes

Still.
When i get to
the step invalue = bitconverter.toint32(Newpacket,0) line i trigger
an error saying that the byte array is less than the read byte
array. How can i only grab position (3) and (4) and then return
there values in integer.
Public invalue as integer
Public Sub ProbeBitValues()
Dim Packet(7) As Byte

You know that Packet contains 8 Bytes?
Dim NewPacket(2) As Byte

You know that NewPacket contains 3 Bytes?
SerialPort2.Read(Packet, 0, 7)
NewPacket(0) = Packet(3)
NewPacket(1) = Packet(4)
indvalue = BitConverter.ToInt32(NewPacket, 0)
End Sub

Why do you put the two bytes into a new array? You can pass variable Packet
to BitConverter.ToInt32 and specify the index 3 as the start index. But
again, this will take bytes at position 3, 4, 5 and 6, not only 3 and 4.

Armin
Is there any way i can only return byte position 3 and 4 only. I know
you have told me these several times, sorry
Nov 16 '07 #3
"cmdolcet69" <co************@hotmail.comschrieb
On Nov 16, 10:55 am, "Armin Zingler" <az.nos...@freenet.dewrote:
"cmdolcet69" <colin_dolce...@hotmail.comschrieb
The sub below reads the value of position from my serial write
command. However i need to read only the (3) and (4) index of
the byte array and then need to convert it into a integer.
I told you several times that an Integer consists of 4 Bytes.

Integer = Int32 = 32 bits = 4 Bytes
Short = Int16 = 16 bits = 2 Bytes

Still.
When i get to
the step invalue = bitconverter.toint32(Newpacket,0) line i
trigger an error saying that the byte array is less than the
read byte
array. How can i only grab position (3) and (4) and then return
there values in integer.
Public invalue as integer
Public Sub ProbeBitValues()
Dim Packet(7) As Byte
You know that Packet contains 8 Bytes?
Dim NewPacket(2) As Byte
You know that NewPacket contains 3 Bytes?
SerialPort2.Read(Packet, 0, 7)
NewPacket(0) = Packet(3)
NewPacket(1) = Packet(4)
indvalue = BitConverter.ToInt32(NewPacket, 0)
End Sub
Why do you put the two bytes into a new array? You can pass
variable Packet to BitConverter.ToInt32 and specify the index 3 as
the start index. But again, this will take bytes at position 3, 4,
5 and 6, not only 3 and 4.

Armin

Is there any way i can only return byte position 3 and 4 only. I
know you have told me these several times, sorry
Short/Int16 is 2 bytes. Conclusion: Use BitConverter.ToInt16
Armin
Nov 16 '07 #4
On Nov 16, 11:59 am, "Armin Zingler" <az.nos...@freenet.dewrote:
"cmdolcet69" <colin_dolce...@hotmail.comschrieb


On Nov 16, 10:55 am, "Armin Zingler" <az.nos...@freenet.dewrote:
"cmdolcet69" <colin_dolce...@hotmail.comschrieb
The sub below reads the value of position from my serial write
command. However i need to read only the (3) and (4) index of
the byte array and then need to convert it into a integer.
I told you several times that an Integer consists of 4 Bytes.
Integer = Int32 = 32 bits = 4 Bytes
Short = Int16 = 16 bits = 2 Bytes
Still.
When i get to
the step invalue = bitconverter.toint32(Newpacket,0) line i
trigger an error saying that the byte array is less than the
read byte
array. How can i only grab position (3) and (4) and then return
there values in integer.
Public invalue as integer
Public Sub ProbeBitValues()
Dim Packet(7) As Byte
You know that Packet contains 8 Bytes?
Dim NewPacket(2) As Byte
You know that NewPacket contains 3 Bytes?
SerialPort2.Read(Packet, 0, 7)
NewPacket(0) = Packet(3)
NewPacket(1) = Packet(4)
indvalue = BitConverter.ToInt32(NewPacket, 0)
End Sub
Why do you put the two bytes into a new array? You can pass
variable Packet to BitConverter.ToInt32 and specify the index 3 as
the start index. But again, this will take bytes at position 3, 4,
5 and 6, not only 3 and 4.
Armin
Is there any way i can only return byte position 3 and 4 only. I
know you have told me these several times, sorry

Short/Int16 is 2 bytes. Conclusion: Use BitConverter.ToInt16

Armin- Hide quoted text -

- Show quoted text -
Ok armin after apply the correct conversion above, thank by the way. I
have this sub

Public Sub ProbeBitValues()
SerialPort2.Read(Packet, 0, 7)
indvalue = BitConverter.ToInt16(Packet, 3)
Me.lblIndicatorValues.Text = indvalue
End Sub

I have the Serialport2 connect to a USB to serial converter cause i
only have (1) serial port on my PC. This shouldn;t matter but
anyways,
my issue comes that i should be getting values in the range of 3902 -
2036, however when i put my sub on a timer, there are time i return
wierd far out number such as 20111 and 30222. my indvalue variable is
a integer type.

Can you think of any way im getting those wierd number?
Nov 16 '07 #5
"cmdolcet69" <co************@hotmail.comschrieb
dim ShortBytes(1) as byte

shortbytes(0) = packet(4)
shortbytes(1) = packet(3)

indvalue = BitConverter.ToInt16(shortbytes, 0)

Armin- Hide quoted text -

- Show quoted text -

I see, I looked at the senario and noticed that when I put your
logic into the sub it wouldn;t return the wild crazy number anymore,
but this time it did do some unexpected stuff, now it will toggle
between the valid numbers (3002) and 0 which i think its because i
have the com1.write on a timer. I dont know, never of had to do such
crazy
amounts of work for somthing to work.

How do you verify that data is available from the serial port? You seem to
read max. 7 bytes, but how do you know that 7 bytes are available?
Armin

Nov 16 '07 #6
On Nov 16, 5:16 pm, "Armin Zingler" <az.nos...@freenet.dewrote:
"cmdolcet69" <colin_dolce...@hotmail.comschrieb


dim ShortBytes(1) as byte
shortbytes(0) = packet(4)
shortbytes(1) = packet(3)
indvalue = BitConverter.ToInt16(shortbytes,0)
Armin- Hide quoted text -
- Show quoted text -
I see, I looked at the senario and noticed that when I put your
logic into the sub it wouldn;treturnthe wild crazy number anymore,
but this time it did do some unexpected stuff, now it will toggle
between the valid numbers (3002) and0which i think its because i
have the com1.write on a timer. I dont know, never of had to do such
crazy
amounts of work for somthing to work.

How do you verify that data is available from the serialport? You seem to
read max. 7 bytes, but how do you know that 7 bytes are available?

Armin- Hide quoted text -

- Show quoted text -
I actually don;t verify any data. All i do i read the com port each
time the timer is executed. Do you think that is why sometimes it will
read 0? and not a correct value, is because i rad the COM port too
early?
Nov 20 '07 #7
"cmdolcet69" <co************@hotmail.comschrieb
How do you verify that data is available from the serialport? You
seem to read max. 7 bytes, but how do you know that 7 bytes are
available?

I actually don;t verify any data. All i do i read the com port each
time the timer is executed. Do you think that is why sometimes it
will read 0? and not a correct value, is because i rad the COM port
too early?
Probably.
Armin
Nov 20 '07 #8
On Nov 20, 2:27 pm, "Armin Zingler" <az.nos...@freenet.dewrote:
"cmdolcet69" <colin_dolce...@hotmail.comschrieb
How do you verify that data is available from the serialport? You
seem to read max. 7 bytes, but how do you know that 7 bytes are
available?
I actually don;t verify any data. All i do i read the com port each
time the timer is executed. Do you think that is why sometimes it
will read 0? and not a correct value, is because i rad the COM port
too early?

Probably.

Armin
So how would you control this. I can think of two ways. 1- have a
sleep on the main thread so that the timer is executed, it will sleep
for a sec then read the COM port. Or 2- slow down the timer that i
have the code placed in. Which one in your professional opinion would
you use?
Nov 21 '07 #9
"cmdolcet69" <co************@hotmail.comschrieb
On Nov 20, 2:27 pm, "Armin Zingler" <az.nos...@freenet.dewrote:
"cmdolcet69" <colin_dolce...@hotmail.comschrieb
How do you verify that data is available from the serialport?
You seem to read max. 7 bytes, but how do you know that 7
bytes are available?
I actually don;t verify any data. All i do i read the com port
each time the timer is executed. Do you think that is why
sometimes it will read 0? and not a correct value, is because i
rad the COM port too early?
Probably.

Armin

So how would you control this. I can think of two ways. 1- have a
sleep on the main thread so that the timer is executed, it will
sleep for a sec then read the COM port. Or 2- slow down the timer
that i have the code placed in. Which one in your professional
opinion would you use?
It depends on the transmission protocol. When does the port receive data?
Which class is SerialPort2? Is it System.IO.Ports.SerialPort (IIRC it is
not)? It has the DataReceived event.

Could you please describe in detail what you are trying to achieve?
Armin

Nov 21 '07 #10

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

Similar topics

4
by: ^CeFoS^ | last post by:
Hello to everybody, I've done an application that draws in a frame the trajectory of a robot. The robot position is readed through the serial port, and several commands are wrote through the...
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. ...
13
by: Al the programmer | last post by:
I need to access the serial ports on my webserver from an asp.net page. I have no problem accessing the serial ports from a windows form application, but the code doesn't work in asp.net. I have...
1
by: Jan | last post by:
Hello, is there someone who know's how to read and write data to a serial port using visualbasic 8 thanx in advance
4
by: Frank | last post by:
Hello, how to get information about all serial ports in the PC? I use the following code, but i got only the data of the FIRST serial port. All other serial port information are not available...
3
by: SatCom | last post by:
Hello, I had originally posted this in the winforms.controls discussion, forgive the double post, Here is where I need help... I have been porting some VB6 to VB2005 and here is the issue with...
1
by: Jan | last post by:
Hello, I want to send some simple strings to com1 using vb2005. I want to read it in Hyperterminal. I succeeded to send some data en read it in Hyperterminal, only the data I received is not...
0
by: =?Utf-8?B?SmFtZXNB?= | last post by:
I am trying to use the code shown in the VB2005 Help on how "To receive strings from the serial port". The code was cut and pasted and it gives no errors when "debugged" in VB2005, but it simply...
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: 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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.