Shop OBEX P1 Docs P2 Docs Learn Events
How to send a byte to the prop using VB Express 2008 .net — Parallax Forums

How to send a byte to the prop using VB Express 2008 .net

AnubisbotAnubisbot Posts: 112
edited 2008-09-14 20:01 in Propeller 1
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 :
    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

  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2008-09-14 17:58
    Hi Anubishot, the Write instruction writes a string of ASCII characters to the serial port but has two overloads (meaning you can use the Write instruction in 3 different ways). To transmit a byte or series of bytes you will need to create an array buffer to hold your data.

    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.
  • AnubisbotAnubisbot Posts: 112
    edited 2008-09-14 18:27
    Unsoundcode said...
    Hi Anubishot, the Write instruction writes a string of ASCII characters to the serial port but has two overloads (meaning you can use the Write instruction in 3 different ways). To transmit a byte or series of bytes you will need to create an array buffer to hold your data.


    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
    freaked.gif
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2008-09-14 18:38
    Hi Anubishot, not sure why you·have the errors and don't have the time to check it out right now but heres another way to handle it and it pre assigns·a dummy value(s)

    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.

  • AnubisbotAnubisbot Posts: 112
    edited 2008-09-14 18:47
    Unsoundcode said...
    Hi Anubishot, not sure why you have the errors and don't have the time to check it out right now but heres another way to handle it and it pre assigns a dummy value(s)

    Make scope of the array cover the class

    Form1

    [noparse][[/noparse]color=#0000ff]Dim[noparse][[/noparse]/color][noparse][[/noparse]color=#0000ff]As[noparse][[/noparse]/color][noparse][[/noparse]color=#0000ff]Byte[noparse][[/noparse]/color][noparse][[/noparse]color=#0000ff]New[noparse][[/noparse]/color][noparse][[/noparse]color=#0000ff]Byte[noparse][[/noparse]/color]<FONT size=2>(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..
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2008-09-14 20:01
    Good deal,

    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.
Sign In or Register to comment.