Shop OBEX P1 Docs P2 Docs Learn Events
Negative Numbers — Parallax Forums

Negative Numbers

kenwtnkenwtn Posts: 250
edited 2008-07-02 03:17 in BASIC Stamp
Hi Again All,
·
··········· I know when I pass a positive number everything works fine, but when I try to pass a negative number from Visual Basic Express to the Basic Stamp the number always turns positive. Can anyone help me out with how to pass negative numbers?

Code Line From VB Express:·

SerialPort1.WriteLine("++^++" & Cmd & vbCr & Format(CmdParm, "G"))



And Code Lines From BS2:

Command···· VAR·· BYTE··············· 'Command From Master
CmdParm···· VAR·· WORD··············· 'Command Parameter

SERIN 16,396, [noparse][[/noparse]WAIT("++^++"), DEC Command ,SDEC CmdParm]

·
Ken

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Keep buying replacement parts and sooner or later you will get it RIGHT!

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-07-02 01:29
    What's 'Format(CmdParm,"G")' do?

    The SERIN ... SDEC <var> should work if the Format function supplies a "+" or "-" followed by a string of digits.

    Post Edited (Mike Green) : 7/2/2008 1:36:47 AM GMT
  • kenwtnkenwtn Posts: 250
    edited 2008-07-02 01:32
    Formats the number as ### or -###

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Keep buying replacement parts and sooner or later you will get it RIGHT!
  • Mike GreenMike Green Posts: 23,101
    edited 2008-07-02 01:36
    Make sure there's a delimiter like a carriage return or space after the signed number

    Like I said, it should work. Do you have a dump of the text sent to the Stamp? That might show what's wrong.
  • kenwtnkenwtn Posts: 250
    edited 2008-07-02 01:41
    I don't have a dump of the text. I do know that positive numbers get passed correctly since I am controlling many things based on what is passed and all positive numbers work fine. I don't know how to get a dump since the line is tried up sending the data. I check the data before it is passed and it shows the -### or ### correctly. The .Writeline in VB Express always ends with CR.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Keep buying replacement parts and sooner or later you will get it RIGHT!
  • kenwtnkenwtn Posts: 250
    edited 2008-07-02 02:17
    I hate work arounds, but since I know the range is -250 to +250 for speed control I can add 250 to the number before I pass it and then subtract 250 before I use it.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Keep buying replacement parts and sooner or later you will get it RIGHT!
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2008-07-02 02:43
    Hi kenwtn,
    kenwtn said...
    Formats the number as ### or -###
    That is not strictly true, the "G" format is a general number without separators and should not be needed.
    I am assuming your value is declared as an integer in VB and needs to be transmitted as a string

    SerialPort1.WriteLine("++^++" & Cmd & vbCr & CmdParm.ToString)

    Using SDEC3 to capture the value.

    If you are then still having problems the cause could be the·way it is used·in the PBasic code.

    Jeff T.
  • kenwtnkenwtn Posts: 250
    edited 2008-07-02 02:48
    I have tried send with and without the formatter and get the same results. The number passed is used on touched and when I do the work around I said above everything works fine.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Keep buying replacement parts and sooner or later you will get it RIGHT!
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2008-07-02 02:54
    Hi kenwtn, if the workaround is doing the job thats great and is sometimes what we have to go with. For future reference though I know the above works and maybe at another time you will get the opportunity to try it out with another application.

    Jeff T.
  • kenwtnkenwtn Posts: 250
    edited 2008-07-02 02:59
    It may have something to do with the fact I send the command from the PC to a BS2 I use to transmitt the command to another BS2 which is the Command Module and it sends the command VIA Serial link to a slave BS2 that does all the work. Anyways there is alot going on.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Keep buying replacement parts and sooner or later you will get it RIGHT!
  • kenwtnkenwtn Posts: 250
    edited 2008-07-02 03:04
    ···· Attached is my little nightmare. I can't remember did I ground that wire? LOL

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Keep buying replacement parts and sooner or later you will get it RIGHT!
    2048 x 1536 - 806K
  • Harry CallahanHarry Callahan Posts: 6
    edited 2008-07-02 03:16
    How are you using CmdParm after the SERIN? PBasic may store the value correctly in its version of a negative number, but if you retrieve it later and use it in, for example, comparisons it will be treated as a positive number. Try assigning a negative number to CmdParm after the SERIN to convince yourself.

    Harry
  • kenwtnkenwtn Posts: 250
    edited 2008-07-02 03:17
    I will do some more testing tomorrow getting late here. Thanks All

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Keep buying replacement parts and sooner or later you will get it RIGHT!
Sign In or Register to comment.