Shop OBEX P1 Docs P2 Docs Learn Events
Serial communication (two-way) between VB '05 Express and BS — Parallax Forums

Serial communication (two-way) between VB '05 Express and BS

SeanTSeanT Posts: 17
edited 2006-11-03 15:30 in BASIC Stamp
Hi, I've run into a problem where I can't have my PBASIC program make a decision on values higher than one digit. Here's what is happening: values are entered to the Stamp via a serial connection to a VB program, and I want the stamp to analyze those values and do a task that will eventually scale the values. This is a BS2 and PBASIC 2.5.

GetInput:
  SERIN INPUT_PIN, BAUDMODE, [noparse][[/noparse]STR XY\4]
  X = XY(0)*10 + XY(1)
  Y = XY(2)*10 + XY(3)
  GOSUB Analyze
  RETURN

This is where the values come from, so from the VB program I send two numbers mushed together, and place them into separate bytes with PBASIC once received. So for X and Y values 10 or greater, would the byte array really·need to be of size 4? I figure if the stamp takes in a value of 1512 corresponding to X = 15 and Y = 12, I can take XY(0) and multiply that by 10 and add to it XY(1). This should give me for X the correct value I'm looking for, 15 (please comment and tell me if there is an easier way to do this or if this will actually work correctly).

Now for the strange part: when I go to the subroutine Analyze, for testing purposes I have this:
IF X = "15" AND Y = "12" THEN
    GOSUB Task1
    RETURN
    ELSE
    GOSUB Task4
    RETURN
  ENDIF

When I enter this in and try to·send to the stamp, the stamp editor tells me it is expecting a THEN right after the 1 from the 15. I can enter it as X = (15), but then the conditional doesn't work correctly and I get a Task1 incorrectly·if the value I send for X starts with a 1 at all, 10-19. Obviously, I want to avoid this. Perhaps there is some crucial syntax rule that I am missing?
I can also enter the conditional as X = 15 AND Y = 12, but I don't ever go to Task1, even if I send 1512 to the stamp, so it seems it doesn't recognize any of the data if there are no quotation marks (but perhaps this is my array math not working - but on second thought, I seem to remember it not working this way for single digits either, but it worked for the quotation marks).
If you see some simple error that I'm making, feel free to tell me. I'm having some trouble understanding why I can say my conditional requires X = 1, but the program says I've met that condition with an X value of 10. I'm also really confused as to why the program will allow only one digit between quotation marks. Please enlighten me.

Thanks,
Sean


Post Edited (SeanT) : 10/31/2006 5:40:52 PM GMT
«1

Comments

  • PJAllenPJAllen Banned Posts: 5,065
    edited 2006-10-28 13:24
    IF (X =·"15") AND (Y = "12")·THEN GOSUB Task1
    ··· RETURN
    ··· ELSE
    ··· GOSUB Task4
    ··· RETURN
    · ENDIF

    I think you need the parentheses.· If the X·AND Y conditions are not met then it will RETURN --·it'll never hit the ELSE.· Does the program branch to this IF...THEN as the result of a GOSUB?
  • SeanTSeanT Posts: 17
    edited 2006-10-28 13:48
    Hi,

    Yes this IF...THEN is branched to through a GOSUB. I'll check on your syntax later, I didn't try it like that.·However, it looks like I'll still get an error from the editor due to what's inside the parentheses, because they are only allowing one digit between quotation marks. Maybe in the format you gave me it should work without the quotation marks?...I'll test that and post back later. Thanks for the tip though...

    Sean
  • Martin HebelMartin Hebel Posts: 1,239
    edited 2006-10-28 14:08
    If X > 15 THen....

    By accepting it as a string, you are trying to do math on ASCII values. "1" is a byte value of $31, "2" is $32. While you could do this with your string:
    X = XY(0)-$30 * 10 + (XY(1)-$30)
    Y = XY(2)- $30 * 10 + (XY(3)-$30)

    then use
    IF X = 15 AND Y = 12 THEN
    ....

    But strings are hard to manipulate anyway, an easier method is to accept it back as DEC value:
    You need to use the DEC modifier and not use strings though you may need a space between them to seperate values or other work arounds.

    SERIN INPUT_PIN, BAUDMODE, [noparse][[/noparse]dec2 X, dec2 y]

    ** This code is in theory only, not tested.
    -Martin

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Martin Hebel
    StampPlot - Graphical Data Acquisition and Control
    AppBee -·2.4GHz Wireless Adapters & transceivers·for the BASIC Stamp & Other controllers·
  • Martin HebelMartin Hebel Posts: 1,239
    edited 2006-10-28 15:43
    OH, and I agree

    IF (X = 15) AND (Y = 12) THEN

    is best to ensure precedence is met.
    -Martin

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Martin Hebel
    StampPlot - Graphical Data Acquisition and Control
    AppBee -·2.4GHz Wireless Adapters & transceivers·for the BASIC Stamp & Other controllers·
  • SeanTSeanT Posts: 17
    edited 2006-10-30 07:41
    Thanks Martin, this helped a lot. However, I·do want to try using that DEC method, but the amount of points·might be of variable length, and I don't want a bunch of (0, 0) points stuck on the end if i send less than the maximum amount of points.·Is there a way for the stamp to realize that the·data has ended so it doesn't send anything after that?·The program I plan to design·will subtract the·last point from the new point·and use·the·ratio of X·to Y to scale the speeds of the motors so they finish their movements in the same amount of time (and use HYP to calculate the steps or time the motor needs to be on - I haven't decided whether to use steppers or servos yet), so sending a (0,·0) point would actually make it move·to the origin instead of not move. This means that in some·cases I would want to send the object to the origin, but if there are only 3 points out of a maximum of 10 (or whatever I can fit), I·might not want to send the object to the origin after my last "real" point request.·If there is a way to count how much data is transmitted and stop sending output to the servos after that many points, that would be exactly what I'm looking for. I'll write a simple example to show what I mean:
    SERIN Serial, Baud, [noparse][[/noparse]DEC2 X1, DEC2 Y1, DEC2 X2, DEC2 Y2, DEC2 X3, DEC2 Y3, DEC2 X4, DEC2 Y4 etc.]
    

    So if I send 14 15 21 23 08 04·to the stamp (only 3 points), how can I not have the stamp send servo output after those points are sent?·Is it possible to·read the amount of points actually filled and use a Do While Loop?

    Thanks,

    Sean
  • SeanTSeanT Posts: 17
    edited 2006-10-30 07:51
    Another note I forgot to add...the reason I want to use the DEC2 method is because right now, each byte in the array is storing only one digit, and it seems to me a waste of space. This means that I can only fit about 5 points right now because I've run out of RAM space, and I still need to fit variables for the speed scale and time needed. I realize since my last post that I don't need to use the HYP command to find the time adjustment, I just need to divide the distance travelled by the "maximum-speed-servo" by its speed - at least one servo will be at max speed for each movement, the slope scaling will just reduce the other servo's speed so both complete their task in the same time to produce a straight line.
  • metron9metron9 Posts: 1,100
    edited 2006-10-30 14:44
    "So if I send 14 15 21 23 08 04 to the stamp (only 3 points), how can I not have the stamp send servo output after those points are sent?"

    Sorry the question is not making much sense. Please restate the question.

    "Is it possible to read the amount of points actually filled and use a Do While Loop?"

    1. What are your maximum X and Y values to accept as input
    2. How many (X,Y) points do you want to accept as input? Fixed? or variable amount of points?

    You could set up a loop to accept the X and Y values like this to accept a variable number of XY pairs and limit the total number of pairs to a maximum. Without more information on what your min/max values are I cant help with variable storage options.


    Psudo code:

    array_pointer=0

    datainput:
    get X and y
    If x and y are 0 exit--- all done getting input
    store x and y in array
    increment array_pointer
    if maximum number of points have been entered exit
    goto datainput

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Think outside the BOX!
  • SeanTSeanT Posts: 17
    edited 2006-10-30 15:08
    OK, I'll try to have this make more sense:

    1. The maximum X or Y values are realistically going to be about 20-30, but the important thing is that there must be two digits for each X or Y value (01, 06) or (15, 10) - each value is two digits.
    2. The amount of points being sent is variable, not fixed.
    3. I may actually want to send a (0, 0) point to send the unit to the origin, so I can't exit the loop if X and Y are zero (but maybe exiting the loop when X and Y are zero will turn out to be the only way I can actually accomplish what I want, so this one is not concrete).
  • metron9metron9 Posts: 1,100
    edited 2006-10-30 15:22
    Well then perhaps you want to ask how many datapoints the user wishes to enter
    get the input variables in pairs as the above loop and exit when the number of variables has been entered
    Check the input for out of bounds numbers
    show error of out of bounds if any and ask for reentry of data.
    then display the input and ask if the data is correct and then process the data.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Think outside the BOX!
  • allanlane5allanlane5 Posts: 3,815
    edited 2006-10-30 16:08
    1. If you want to compare 'X' to the value 15, you should say "IF X = 15 ... "

    If you want to compare 'X' to the STRING "15", then "15" consists of two bytes, and the IF won't compare that.

    You COULD compare 'X' to the string "1" (which is one byte, I believe value is 49) which is why the compiler complains on the second character.

    2. The usual way to address this problem is with a SERIN with the 'DEC' modifier.

    X VAR BYTE ' Stores 0..255'
    Y VAR BYTE '

    SERIN 16, 16384, [noparse][[/noparse]DEC X, DEC Y]

    So if you send "30, 15 <CR>" -- then X gets the value 30, and Y gets the value 15
  • Mike GreenMike Green Posts: 23,101
    edited 2006-10-30 16:17
    Actually, although you can't do calculations on pairs of characters in quotes, you can do 16 bit compares which allows you to compare two characters. Specifically, you can write 'if x = (("0"<<8)+"1") then' to compare x for equality to "01". You would need to build the value in x the same way from two separate bytes that were read. If you had a 'SERIN pin,baud,[noparse][[/noparse]y,z]', you would write 'x = (y << 8) + z'. This would even work for less than and greater than, but you couldn't use this to do arithmetic (because you're really comparing strings, not numeric values).

    Despite the fact that "you can do it", it's not very efficient, either in speed or memory. Note that the DEC formatter also accepts a character count like DEC2 which reads 2 digit characters and produces the binary equivalent and may be more the sort of thing you want.
  • SeanTSeanT Posts: 17
    edited 2006-10-30 16:52
    OK, so I could use the VB program to count how many points need to be sent, then tell the stamp

    VB - "I need to transfer X amount of points to you"
    BS - "OK, I'm ready for X amount of points, bring it on."
    VB - (send first point)
    BS - (process first point) "Done with first point, send next point."
    VB - (send second point)
    BS - (process second point) "Done, send next."
    et cetera
    BS - (process last point)

    So I tried to have the VB program send the data through a regular SerialPort.Write command and send everything at once for simplicity, because I could not get the Event command to recognize the stamp sending data over. Perhaps I was going about it the wrong way, but I'll check some more on MS forums for that.

    Now a question: if I have a SERIN command on the stamp, will it wait at the command until there is some serial data transmitted, or will it have to be in a loop instead?
  • allanlane5allanlane5 Posts: 3,815
    edited 2006-10-30 17:05
    If you use a 'plain' SERIN (a SERIN without a time-out parameter) it WILL wait forever, or until enough data has been recieved to 'satisfy' it.

    You can use the pseudo-pin '16' in a SEROUT and SERIN to talk/listen to the 'programming' port, if you dont' want to install a MAX232 chip for the RS-232 level shifting.
  • SeanTSeanT Posts: 17
    edited 2006-10-30 17:50
    Yeah, the pin 16 method was working fine for receiving data from my VB program and it was also sending information back, but I wasn't too sure on sending because it was sending back different values even if I set it to just send a constant...I'll have to work on that. But thanks for the SERIN info.
  • metron9metron9 Posts: 1,100
    edited 2006-10-30 18:35
    I did not know you were using VB, inthat case why not make a command structure so you dont have to send back and forth all the Ok to send , send next byte etc

    I would set it up like this:

    BS waits for a byte
    VB sends a command byte that gives BS all the information it needs to receive the following packet of data.
    BS reads in all data
    BS processes data
    Loop

    So for example use the following example

    Command byte --
    Use the high nibble for the command up to 15 commands
    use the low nibble for the number of bytes to receive

    Say our first command is a simple command that tells the BS to read in 6 bytes. We might use %1000 0110
    The first nibble is the command %1000 BS knows that means "read bytes"
    It then uses the lower nibble as the number of bytes to read in.

    All the input stuff is handled by your VB program, input, data chacking, etc and it only sends valid data.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Think outside the BOX!
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2006-10-30 20:49
    Sean -

    Just remember when you're using Pin Port 16, all data sent through that port will be echoed back to the sender. There is no way to circumvent this so long as you are using Pin Port 16. This doen't happen when using any other Pin Port.

    Regards,

    Bruce Bates

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    <!--StartFragment -->
  • SeanTSeanT Posts: 17
    edited 2006-10-31 03:43
    OK, I did not know that. Does that mean that If I use a Read command in the VB program right after I send anything that it will intercept the echoing information? Also, do you know if I would need to clear the buffer in VB each time I want to take information in?

    Edit: Metron, if I used that method, how would I get around the fact that VB will loop and send each piece of data faster than the stamp can process?
  • allanlane5allanlane5 Posts: 3,815
    edited 2006-10-31 04:08
    Well, hopefully your VB program knows what you sent, so can pull that off of the input buffer before you read the BS2's response.
  • SeanTSeanT Posts: 17
    edited 2006-10-31 04:20
    Any hints on how to do that? I've been trying to read up on some Buffer commands for VB but real examples are scarce and I can't seem to get it right.
  • metron9metron9 Posts: 1,100
    edited 2006-10-31 05:47
    "Metron, if I used that method, how would I get around the fact that VB will loop and send each piece of data faster than the stamp can process?"

    Would not the baud rate take care of that, i thought you were using serial communication.


    I have Visual Studio, If you want to post the VB file I could have a look. I have not used VB for quite a few years though so don't expect much but I am interested in using VB to communicate with these devices so I would give it a go.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Think outside the BOX!
  • SeanTSeanT Posts: 17
    edited 2006-10-31 06:25
    Well I didn't think the baudrate could take care of timing issues because when I send a point to the stamp, it needs to loop for up to seconds to actually do the data processing (sending output to a servo/stepper and having it move to the desired location). So this would mean I can't read any new incoming points until after the stamp finishes the subroutine, hence the secret password game.·Here's what I've got right now, some of it may be incomplete or completely wrong -

    BS2:
    N VAR Nib
    C VAR Nib
    X VAR Byte Y VAR Byte
    PreviousX VAR Byte
    PreviousY VAR Byte
    DistX VAR Byte
    DistY VAR Byte
    CenterX VAR Word ' these are to be used in the Max subroutines later for servo movement
    CenterY VAR Word
     
    Main:
      DO
        X = 0
        N = 0
        PreviousX = 0
        PreviousY = 0
        SERIN 16, 16468, [noparse][[/noparse]N] 'retrieve the number of points to be sent from VB
        GOSUB Retrieve
      LOOP
     
    Retrieve:
        DO WHILE C < N
          SEROUT 16, 16468, [noparse][[/noparse]9 & " "]        'secret password to VB to allow next point sending (not working right now..but probably VB issues)
          SERIN 16, 16468, [noparse][[/noparse]DEC2 X, DEC2 Y]  'get next X and Y point values
          C = C + 1
          GOSUB ProcessData
        LOOP
      RETURN
     
    ProcessData:                               'find out which servo needs to be at max speed, and scale the other down accordingly
      DistX = X - PreviousX                    'eventually also include time constant kind of thing to loop the servo PULSOUTs enough times
      DistY = Y - PreviousY
      IF (DistX > DistY) THEN
        GOSUB MaxX
        ELSE : IF (DistX = DistY) THEN
        GOSUB MaxBoth
        ELSE : IF (DistX < DistY) THEN
        GOSUB MaxY
        ENDIF
        ENDIF
      ENDIF
     
    MaxX:
      FREQOUT 0, 500, 2000
    RETURN
     
    MaxBoth:
      FREQOUT 0, 500, 2500
    RETURN
     
    MaxY:
      FREQOUT 0, 500, 3000
    RETURN
    
    

    Note: the Max subroutines are not complete, I added the FREQOUTs for debugging. So far, I know that it accepts the N value from VB, but sending the OK password back to VB is where things get hairy.

    Here is the relevant part of the VB program:
    [color=#0000ff]Private[/color] [color=#0000ff]Sub[/color] btnSend_Click([color=#0000ff]ByVal[/color] sender [color=#0000ff]As[/color] System.Object, [color=#0000ff]ByVal[/color] e [color=#0000ff]As[/color] System.EventArgs) [color=#0000ff]Handles[/color] btnSend.Click
    [color=#0000ff]   If[/color] SerialPort.IsOpen [color=#0000ff]Then[/color]
          Steps = 0
          SerialPort.Write(Count)
    [color=#0000ff]      Do[/color] [color=#0000ff]While[/color] Steps < Count
             SerialPort.Read(Check, 0, 1)
             lblCheck.Text = Check(0)
    [color=#0000ff]         If[/color] Check(0) = [color=#800000]"9"[/color] [color=#0000ff]Then[/color]
                SerialPort.Write(lstHidden.Items.Item(Steps))
    [color=#0000ff]         End[/color] [color=#0000ff]If[/color]
             Steps = Steps + 1
    [color=#0000ff]         Loop[/color]
    [color=#0000ff]   Else[/color]
          MsgBox([color=#800000]"Please connect to the serial port before sending!"[/color], MsgBoxStyle.Exclamation, [color=#800000]"Error!"[/color])
    [color=#0000ff]   End[/color] [color=#0000ff]If[/color]
    [color=#0000ff]End[/color] [color=#0000ff]Sub[/color]
    
    

    So the VB program sends the Count variable just fine (N in the PBASIC program). The program then freezes and none of the points actually get sent. This is VB 2005 Express Edition (I have Visual Studio 2003, but the combination of·much easier Serial communications and freeness led me to Express). Now, a few things in this block I'm not too confident about -
    1. The SerialPort.Read command - So hopefully this is saying check the incoming data and read only byte 1 and store it in Check(0), but I'm not certain.
    2. Do I need to use a DiscardBuffer command after each password arrives to ensure proper password approval and timing? Also, the MS literature on the DiscardBuffer command is very very limited, I could only find about a line of explanation, and it wasn't deep enough for me - I still don't get it.
    3. The SerialPort.Write command for the list of points - Will this actually read the first item in the list, then loop and wait for the second password and continue, or will my list syntax in the parentheses not work?

    If these questions are answerable, yay! This is my first time working with programming of any sort, so thanks for all the help.
  • metron9metron9 Posts: 1,100
    edited 2006-11-01 04:20
    Forget it, I can't update my stupid computer to SP2 to use the express VB. When I tried to follow a simple how too use a serial port on my Vstudio VB6.0 I could not find the Components anywhere, I hate instructions that say click on this or that and they don't exist, except I did open a window that had about 5 selections for serial communication listing SP2 SP5 SP6 versions or whatever, then I tried to use it and nothing happens.

    Now I know why i quit using VB , I hate it, give me a fixed list of program statements with some low level access and I can get it done but this oop programming is not my cup of tea. No wonder windows is so dam buggy if they use this stuff to make programs. I have programs with thousands of lines of code that run every day for the last 15 years, no updates no fluff, no pretty colors or fancy artwork and they run flawlessly under DOS/Novell I am going to remove that darn program to free up some disk space.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Think outside the BOX!
    754 x 707 - 17K
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2006-11-01 05:13
    Hi Sean,·I'm not sure if the program will work as you expect but I see a·couple of problems that you can iron out and help you progress.·Change SEROUT 16, 16468, [noparse][[/noparse]9 & " "] to SEROUT 16, 16468, [noparse][[/noparse]9,10], 10 is the decimal value of the new line character, VB will see this as the end of string, the same goes in your VB code, change SerialPort.Write(Count) to SerialPort.Write(Count & Chr(10)) the Stamp also expects an end of string character. Or you could use SerialPort.WriteLine(Count) which will append the new line for you. As you dont have any timeouts in your serial commands then this is where the programs·will surely hang if they dont see the new line. The PC buffer stores all the information sent by the stamp until they are read or cleared (including the echo back from a VB write and also including the new line characters), you may want to empty the buffer before you send from the Stamp and before you do a VB Read,·you can do that by using SerialPort.DiscardInBuffer this will "clean" the buffer ready for you "password".

    hope this helps

    Jeff T.
  • SeanTSeanT Posts: 17
    edited 2006-11-01 05:47
    Oh, so the 9 that I'm sending from the stamp is also not a 9? What is it and will the VB program read it? Should I send it as [noparse][[/noparse]"9",10]? I'm a little confused, because I set up my stamp to play a tone if the N (Count in VB) value was a certain number, and it worked with various numbers - so it seemed to be reading the Write(Count) command from VB properly. After that is when the VB program froze every time. So basically the VB program wasn't freezing because of lack of data in the buffer, it was because the password being sent didn't include an end byte? Now if I was to discard the VB's buffer right after writing the Count value to the port, would this be too fast to also catch and delete the echo? If this will fix my problem then I can get on with finding out if my list writing section will work. Thanks for the tips.

    Oh and metron, if you can get VB '05 Express to work, it can do some really amazing things (not that I would know yet, but I've been reading some books on it). I did read in several places that the serial commands in VB6 were pretty rotten and finicky. So far at least I know writing with '05 Express is pretty simple, I just need to find out how to do the reading properly.
  • metron9metron9 Posts: 1,100
    edited 2006-11-01 05:47
    Sorry I flipped out, but my son sold "MY" graphing calculator because he thought it was "HIS"

    Code at bottom for a VB form
    Picture of serial output using the free serial port monitor


    "Metron, if I used that method, how would I get around the fact that VB will loop and send each piece of data faster than the stamp can process?"


    Ok I went back in, found the MSDN site that has information on VB commands, *My HELP not working because I erased MSDN gigantic mammoth help files from my hard drive, god one day these people are going to run out of names to call stuff I swear to god)

    Here is the program I came up with, the user puts in 6 pairs of numbers and presses SEND
    The program is set to 300 baud, the stamp can do a word serial in command and process the data
    I would simply adjust the sleep time, (I have it set at 1000ms
    Look at it like a MIDI interface, just send the stuff at a rate the stamp can handle using the first byte as a command byte
    Like a MIDI if it misses it a not dosent get played and it just waits for command bytes to tell it what to do.
    In your app i would guess when the person presses send, something happens on your device for feedback to the user so he knows it has been sent. You could use a display output on the stamp to show the 6 pairs of numbers

    I used a "!!" for a command word and then just sent the data one variable at a time
    You will have to process the string data back to numbers in the stamp



    [noparse][[/noparse]/code]


    Private Declare Sub Sleep Lib "kernel32" ( _
    ByVal dwMilliseconds As Long)



    Private Sub Form_Load()
    Form1.Caption = "App1"
    With MSComm1
    .Handshaking = 2 - comRTS
    .RThreshold = 1
    .RTSEnable = True
    .Settings = "300,n,8,1"
    .SThreshold = 1
    .PortOpen = True
    ' Leave all other settings as default values.
    End With
    Command1.Caption = "&Send"

    End Sub

    Private Sub Command1_Click()

    MSComm1.Output = "!!"
    MSComm1.Output = Text1.Text
    Sleep 1000
    MSComm1.Output = Text2.Text
    Sleep 1000
    MSComm1.Output = Text3.Text
    Sleep 1000
    MSComm1.Output = Text4.Text
    Sleep 1000
    MSComm1.Output = Text5.Text
    Sleep 1000
    MSComm1.Output = Text6.Text



    End Sub

    Private Sub Form_Unload(Cancel As Integer)
    MSComm1.PortOpen = False
    End Sub



    [noparse][[/noparse]code]

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Think outside the BOX!
    929 x 733 - 40K
  • SeanTSeanT Posts: 17
    edited 2006-11-01 06:04
    Ah cool, I'll have to try all this later when I have the stamp. That serial port monitor is definitely going to be helpful, I didn't even think of looking up one of those.
  • metron9metron9 Posts: 1,100
    edited 2006-11-01 16:07
    I have another computer I installed VB express

    Tried to port the code I I posted that runs on VB6 but no luck

    Looks like the completely changed the interface/tools/commands/syntax/names/how everything works.

    What's the point in learning a language that changes so drastically from version to version, its a nightmare.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Think outside the BOX!
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2006-11-01 17:14
    Hi Sean, sorry I forgot the DEC formatter in the Stamp Serout command it should have read SEROUT 16, 16468, [noparse][[/noparse]DEC 9,10]. As metron points out there are a few differences between different versions of VB which can get frustrating but if you have not dealt with anything but 2005 Express it shouldnt get too confusing. Like you said timing is critical with serial comms, there is always more than one way to write a program to deal with timing but here is an example with a couple of solutions. You will need a VB form with 2 buttons 2 textbox's and a serialport, in textbox1 type a number between 1 and 3, press button1 then press button2. A value of 1 displays the buffer contents in textbox2 (echo,nl,password,nl),a value of 2 removes the echo (password,nl) and a value of 3 displays the password as you would want VB to see it.

    Stamp code

    VBdata VAR Byte
    main:
    SERIN 16,16468,[noparse][[/noparse]DEC Vbdata]
    PAUSE 50
    SEROUT 16,16468,[noparse][[/noparse]DEC 9,10]
    GOTO main

    VB code

    Dim count As Int16

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

    count = Val(TextBox1.Text)

    SerialPort1.Open()

    SerialPort1.Write(count & Chr(10))

    Do While SerialPort1.BytesToRead < 2

    '
    wait here for the Stamp to echo back

    Loop

    If count > 1 Then

    SerialPort1.DiscardInBuffer()

    End If

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    If count < 3 Then

    TextBox2.Text = SerialPort1.ReadExisting

    Else

    TextBox2.Text = SerialPort1.ReadLine()

    End If

    SerialPort1.DiscardInBuffer()

    SerialPort1.Close()

    End Sub

    Jeff T.
  • SeanTSeanT Posts: 17
    edited 2006-11-01 17:28
    Yeah, they changed it all, but supposedly because of the new 2.0·.NET framework it will now stay similar/the same.
    Here are the basic components you need:

    First add a serial port to the form, it's in the toolbox under components or all windows forms. In these examples, I named my serial port SerialPort (nice eh?).·You can change all the properties of the serial port component in the form designer if you click on the port. It should not show up on the form, but rather at the bottom of the designer window all by itself.

    Connect button:···
        Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
            If Not SerialPort.IsOpen Then
                SerialPort.Open()
            Else
                MsgBox("Port is already open!", MsgBoxStyle.Exclamation, "Error!")
            End If 
        End Sub
    
    

    Disconnect button:

    [color=#0000ff]Private[/color] [color=#0000ff]Sub[/color] btnDisconnect_Click([color=#0000ff]ByVal[/color] sender [color=#0000ff]As[/color] System.Object, [color=#0000ff]ByVal[/color] e [color=#0000ff]As[/color] System.EventArgs) [color=#0000ff]Handles[/color] btnDisconnect.Click
    [color=#0000ff]   If[/color] SerialPort.IsOpen [color=#0000ff]Then[/color]
          SerialPort.Close()
    [color=#0000ff]   Else[/color]
          MsgBox([color=#800000]"Port is already closed!"[/color], MsgBoxStyle.Exclamation, [color=#800000]"Error!"[/color])
    [color=#0000ff]   End[/color] [color=#0000ff]If[/color]
    [color=#0000ff]End[/color] [color=#0000ff]Sub[/color]
    
    

    ·Send button:
    ·
    [color=#0000ff]Private[/color] [color=#0000ff]Sub[/color] btnSend_Click([color=#0000ff]ByVal[/color] sender [color=#0000ff]As[/color] System.Object, [color=#0000ff]ByVal[/color] e [color=#0000ff]As[/color] System.EventArgs) [color=#0000ff]Handles[/color] btnSend.Click
    [color=#0000ff]   If[/color] SerialPort.IsOpen [color=#0000ff]Then[/color]
          SerialPort.Write(yourdatahere)
          SerialPort.DiscardInBuffer()
          SerialPort.Read(storeincomingdatahere, 0, 1) '0 here is the offset and 1 is the no. of bytes to read - i just cleared the buffer so no offset here
     
    [color=#0000ff]   Else[/color]
          MsgBox([color=#800000]"Please connect to the serial port before sending!"[/color], MsgBoxStyle.Exclamation, [color=#800000]"Error!"[/color])
    [color=#0000ff]   End[/color] [color=#0000ff]If[/color]
    [color=#0000ff]End[/color] [color=#0000ff]Sub[/color]
    
    

    ·
    So just add a simple text box to enter data into, then in the Write command·parentheses type·txtBox.Text (if your text box was named txtBox) instead of yourdatahere.

    I know the writing part works nice, but right now the read part is a guess, so don't expect it to work.
  • SeanTSeanT Posts: 17
    edited 2006-11-01 17:31
    Thanks Jeff, I'll test later and post the results.
Sign In or Register to comment.