How to send a byte to the prop using VB Express 2008 .net
Anubisbot
Posts: 112
Hi, i am toying around with VB Express 2008 and the serial port.
I have some communication going, but i have a strange problem.
When i try to send bytes i have to send it as a string or a byte arry is there a way to only send a single byte or do i have to convert the byte to a char.
right now i do that and it sends unicode, but i think i need some else ... since when i send a 1 as a byte it works , when i send 100 it works.
but when i send 250 to 255 and some other numbers i get only a hang up since i think it has to do with the unicode.
So does some one have a idea how to change it to the right acsii format??
VB Code here :
On the other side to it works good from data going in to the pc from 1-255 no problem with the bytes
Post Edited (Anubisbot) : 9/14/2008 5:36:41 PM GMT
I have some communication going, but i have a strange problem.
When i try to send bytes i have to send it as a string or a byte arry is there a way to only send a single byte or do i have to convert the byte to a char.
right now i do that and it sends unicode, but i think i need some else ... since when i send a 1 as a byte it works , when i send 100 it works.
but when i send 250 to 255 and some other numbers i get only a hang up since i think it has to do with the unicode.
So does some one have a idea how to change it to the right acsii format??
VB Code here :
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Dim associatedChar As Char ' Returns "A". associatedChar = Chr(1) SerialPort1.Write(associatedChar) TextBox1.Text = SerialPort1.Readbyte End Sub Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click Dim associatedChar As Char ' Returns "A". associatedChar = Chr(255) SerialPort1.Write(associatedChar) TextBox1.Text = SerialPort1.Readbyte End Sub
On the other side to it works good from data going in to the pc from 1-255 no problem with the bytes
Post Edited (Anubisbot) : 9/14/2008 5:36:41 PM GMT
Comments
This example could be placed inside your button click events,
Dim myarray() As Byte
myarray(0)=255
SerialPort1.Write(myarray,0,1) 'write array index 0 and 1 byte in length
Jeff T.
Thanx for the tip, but i have tried that bevore, but i get this nasty error when pushing the button,
Object reference not set to an instance of an object.??
And it says that the variable is used before some value is assigned...
Any idea what that means , i am searching and trying now for 2 days
Make·scope of the array cover the class
Public Class Form1
Dim testbyte() As Byte = New Byte(2) {0, 0, 0}
then in the button click event do the following
testbyte(0) = 255
SerialPort1.Write(testbyte, 0, 1)
Jeff T.
Great, works perfect, u saved me a long night on this.
I have tried the new statement before but could not get the syntax right.
That makes a lot easier.
Now , i know a little more[noparse]:)[/noparse]
Thanx..
and of course if the values in the array are constants you can pre define them when you declare the array and save yourself a line of code in each button click.
Jeff T.