Shop OBEX P1 Docs P2 Docs Learn Events
MSCOMM32.ocx — Parallax Forums

MSCOMM32.ocx

GeorgeLGeorgeL Posts: 131
edited 2008-12-08 03:37 in BASIC Stamp
Hey, anyone here use the MSCOMM in their program's like me. I cant seem to get data correctly from the BS2 anymore. In fact, I dont really understand as to how its supposed to work. If someone could give a tutorial similar to the http://www.theabramgroup.com/basicstamp/·tutorial, on sending data then ill just move my projects to VB2008 or similar, since I have that at home.

Comments

  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2008-12-04 15:43
    Hi George, this link http://forums.parallax.com/showthread.php?p=671804 explains a little about Visual Studio Visual Basic and sending / receiving data between a Stamp and PC

    There is also an interesting thread dealing with Real Basic at this link http://forums.parallax.com/showthread.php?p=766819·though Real Basic is not free.

    If you get the template up and running for the VB Express version (which is really quite simple) and you have a more specific problem you want to deal with then post your questions and we will see if they can be answered.

    Jeff T.
  • GeorgeLGeorgeL Posts: 131
    edited 2008-12-04 15:52
    See thats the problem. I tried the example template he had posted and I didnt know what code they wanted in the BS2, If you have a sall example then that would be great! That would let me use VB08 much easier. It looks much cleaner.
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2008-12-04 16:01
    Hi George ,·at·the end of the template doc there is a two line piece of code for a BS2 to accept 1 byte of information, it was left to the user to direct the variable to an output to verify the results.

    There is also an example towards the end of the thread called Com Template.zip that expands on the above by providing a GUI to control a device in 4 different directions, the Stamp code is also provided in that same post.

    Jeff T.
  • GeorgeLGeorgeL Posts: 131
    edited 2008-12-06 04:21
    Hello,

    I still cant get the VB2008 to work with the tutorial.

    How bout I make a more specific request, since I know my way around VB I just need 2 specific conversions.

    So, what is the VB08 equivalent of:

    MSComm1.Output = Chr(255) & Chr(1)

    and

    how would I recieve this line into VB08 from the BS2:

    SEROUT 16,16780,[noparse][[/noparse]1]
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2008-12-06 06:31
    Hi, the two write commands of the serial port in VB2008 are write and writeline which will write a string of ASCII characters, the difference is that writeline will automatically append the newline character. So to write the value 255 you might go SerialPort1.WriteLine("255"), the drawback here is that the serial output is actually an ASCII string of three character bytes and will transmit 50-53-53 followed by the appended NewLine character. Thats not a problem for the Stamp as there is a formatter that is specifically there to accept and read a string of characters and place them in a byte variable

    SERIN rx,baud,[noparse][[/noparse]DEC3 my_variable] will take the three character codes and place them into one byte as 255

    A more economical way to do the same is to place the value 255 into one byte variable before it is transmitted, this will cut the transmission time down by ~66%

    Possibly using this method

    Dim my_array As Byte()=New Byte(2) {0,0,0}· 'declare an array of bytes

    my_array(0) = 255·········· 'assign the first two

    my_array(1) = 1············· 'elements with your values

    SerialPort1.Write(my_array, 0, 2)· 'write element 0 and 1

    SerialPort1.Read(my_array, 2, 1)· 'read value from Stamp into element 2

    Jeff T.

  • GeorgeLGeorgeL Posts: 131
    edited 2008-12-06 06:42
    IDK? For some reason I just cant get it to work... This is what I did:
    For the VB2008 code I set up a form with 1 button and the SerialPort1, NOTE the only property i changed in the serial port is to set it for 2400Baud. COM1 is correct, idk about the rest (the rest is default)

    So heres the code in VB:

    Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim my_array As Byte() = New Byte(2) {0, 0, 0} 'declare an array of bytes

    my_array(0) = 255 'assign the first two

    my_array(1) = 1 'elements with your values
    SerialPort1.Open()

    SerialPort1.Write(my_array, 0, 2) 'write element 0 and 1

    SerialPort1.Read(my_array, 2, 1) 'read value from Stamp into element 2
    SerialPort1.Close()

    End Sub


    End Class


    Then in the Stamp, I wrote this:

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    dir VAR Word
    pulseCount VAR Byte
    SERIN 16,16780,[noparse][[/noparse]DEC3 dir]
    IF (dir=1) THEN
    FOR pulseCount = 0 TO 50
    PULSOUT 13,650
    PULSOUT 12,850
    NEXT
    ENDIF



    So....WHAT DID I DO WRONG!!!!!!!!!!!!! (cry)
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2008-12-06 16:12
    Hi George, your sending the values as a byte yet have the Stamp looking for an ASCII string. Lets do it the simplest way and send the values as a string.

    In your Button_Click event write it like this

    Dim sData As Byte················ 'create a variable to transmit for this button

    SerialPort1.ReadExisting()····· ·'clear the serial buffer before we transmit

    sData=1····························· 'assign a value to our variable

    SerialPort1.WriteLine(sData)··· 'send the data

    At the Stamp try this

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    dir VAR Byte········ 'make dir a bye value

    pulseCount VAR Byte
    SERIN 16,16780,[noparse][[/noparse]DEC3 dir]··· 'this instruction will take as much as a·3 digit value terminating when it sees a CR or LF
    IF (dir=1) THEN
    FOR pulseCount = 0 TO 50
    PULSOUT 13,650
    PULSOUT 12,850
    NEXT
    ENDIF

    see if that works for you and let me know

    Jeff T.
  • GeorgeLGeorgeL Posts: 131
    edited 2008-12-06 18:21
    Ok, I got it to work for sending the data, now I need to see if I can recieve data myself.
  • GeorgeLGeorgeL Posts: 131
    edited 2008-12-06 18:42
    Ya...I tried this link...and I failed.
    http://www.theabramgroup.com/basicstamp/
    This is my VB code (I just added a text box)
    Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    SerialPort1.Open()


    Dim sData As Byte 'create a variable to transmit for this button

    SerialPort1.ReadExisting() 'clear the serial buffer before we transmit

    sData = 1 'assign a value to our variable

    SerialPort1.WriteLine(sData) 'send the data

    TextBox1.Text = SerialPort1.ReadExisting()
    SerialPort1.Close()



    End Sub


    End Class


    and then my BS2 code:
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    dir VAR Byte 'make dir a bye value

    pulseCount VAR Byte
    DO
    SERIN 16,16780,[noparse][[/noparse]DEC3 dir] 'this instruction will take as much as a 3 digit value terminating when it sees a CR or LF
    IF (dir=1) THEN
    FOR pulseCount = 0 TO 50
    PULSOUT 13,650
    PULSOUT 12,850
    NEXT
    SEROUT 16,16780,[noparse][[/noparse]"h"]
    ENDIF

    LOOP
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2008-12-06 23:44
    Hi George, there is no need to keep bumping the post it's not being overlooked.

    If you are sending and receiving you can use the Data_Received event of the serial port, we can also use this event as a way to synchronize our transmissions. The Data_Received event will detect any data that the Stamp sends, when it does then we know exactly what point we are at in the Stamps program.

    There is a drawback in using the Data_Received event in the fact that it runs in a seperate thread, this gives you the problem that if you should try and pass certain information from this thread to another it will throw an exception, such as to a TextBox in another thread. You would normally overcome the problem using delegates but as that adds a little mor complexity I am not going into it right now. There is an instruction that allows you to ignore the cross thread calls and does no harm in simple examples.

    I am attaching a text file with an example of what you want to help you get started. Its not perfect but you should be able to work it to how you need

    Jeff T.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2008-12-08 03:37
    Jeff, bumps were removed...

    George, please do not post BUMP messages. If you have nothing additional to offer and have no replies then nobody has anything to post. Posting the word bump is considered off topic on these forums.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Engineering
Sign In or Register to comment.