473,659 Members | 2,681 Online
Bytes | Software Development & Data Engineering Community
+ 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.to int32(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.Rea d(Packet, 0, 7)
NewPacket(0) = Packet(3)
NewPacket(1) = Packet(4)
indvalue = BitConverter.To Int32(NewPacket , 0)
End Sub
Nov 16 '07 #1
9 1611
"cmdolcet69 " <co************ @hotmail.comsch rieb
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.to int32(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.Rea d(Packet, 0, 7)
NewPacket(0) = Packet(3)
NewPacket(1) = Packet(4)
indvalue = BitConverter.To Int32(NewPacket , 0)
End Sub
Why do you put the two bytes into a new array? You can pass variable Packet
to BitConverter.To Int32 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...@free net.dewrote:
"cmdolcet69 " <colin_dolce... @hotmail.comsch rieb
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.to int32(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.Rea d(Packet, 0, 7)
NewPacket(0) = Packet(3)
NewPacket(1) = Packet(4)
indvalue = BitConverter.To Int32(NewPacket , 0)
End Sub

Why do you put the two bytes into a new array? You can pass variable Packet
to BitConverter.To Int32 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.comsch rieb
On Nov 16, 10:55 am, "Armin Zingler" <az.nos...@free net.dewrote:
"cmdolcet69 " <colin_dolce... @hotmail.comsch rieb
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.to int32(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.Rea d(Packet, 0, 7)
NewPacket(0) = Packet(3)
NewPacket(1) = Packet(4)
indvalue = BitConverter.To Int32(NewPacket , 0)
End Sub
Why do you put the two bytes into a new array? You can pass
variable Packet to BitConverter.To Int32 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.To Int16
Armin
Nov 16 '07 #4
On Nov 16, 11:59 am, "Armin Zingler" <az.nos...@free net.dewrote:
"cmdolcet69 " <colin_dolce... @hotmail.comsch rieb


On Nov 16, 10:55 am, "Armin Zingler" <az.nos...@free net.dewrote:
"cmdolcet69 " <colin_dolce... @hotmail.comsch rieb
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.to int32(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.Rea d(Packet, 0, 7)
NewPacket(0) = Packet(3)
NewPacket(1) = Packet(4)
indvalue = BitConverter.To Int32(NewPacket , 0)
End Sub
Why do you put the two bytes into a new array? You can pass
variable Packet to BitConverter.To Int32 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.To Int16

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.Rea d(Packet, 0, 7)
indvalue = BitConverter.To Int16(Packet, 3)
Me.lblIndicator Values.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.comsch rieb
dim ShortBytes(1) as byte

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

indvalue = BitConverter.To Int16(shortbyte s, 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...@free net.dewrote:
"cmdolcet69 " <colin_dolce... @hotmail.comsch rieb


dim ShortBytes(1) as byte
shortbytes(0) = packet(4)
shortbytes(1) = packet(3)
indvalue = BitConverter.To Int16(shortbyte s,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;treturnt he 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.comsch rieb
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...@free net.dewrote:
"cmdolcet69 " <colin_dolce... @hotmail.comsch rieb
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.comsch rieb
On Nov 20, 2:27 pm, "Armin Zingler" <az.nos...@free net.dewrote:
"cmdolcet69 " <colin_dolce... @hotmail.comsch rieb
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
9086
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 same port to change the direction of the robot. The trajectory frame is managed by an applet, and the project works good when the applet is called by a html document allocated in the same local machine under W98 where the classes and the serial port...
2
13176
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. However, I'm unsure of how exactly I should be designing the program, I thought I could use threading to start class: class scanner(Thread): def __init__(self,port):
13
4815
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 been told it is not possible to access the serial ports from asp.net. The application is used to control custom hardware. The hardware is connected to a PC through serial ports. Our customer wants to control the hardware from a remote...
1
7013
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
17803
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 with this code sample: ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select * from WIN32_SerialPort");
3
7122
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 the serial port. I am looking to update controls on the main form (textbox, listbox, etc) when a call comes in. I can retrieve the ring status, caller-id etc, but when I go to update a textbox, I receive a threading exception issue. As I...
1
2473
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 the data I send. This is the code i used: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
0
1240
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 does not seem to do anything. I'm not to familiar with using methods and functions and that might be the problem. The code shown uses "My.Computer.Ports.OpenSerialPort" and "ReadLine" methods. I guess the real question is: If ReadLine is "reading"...
6
6651
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 '%' to serial port and make sure it reached the serial port. 2. Once confirmed, send another character.
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8751
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8535
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8629
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7360
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6181
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5650
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4176
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1739
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.