Shop OBEX P1 Docs P2 Docs Learn Events
Quick Question: C++, Serial Cables, And the Stamp — Parallax Forums

Quick Question: C++, Serial Cables, And the Stamp

DiablodeMorteDiablodeMorte Posts: 238
edited 2006-04-16 17:45 in BASIC Stamp
Since I seem to be mentally incapable of connecting two compters togethor to test this:

Here's my situation:

I have a C++ program I wrote(sorta, at bottom) that writes to the serial port(yet again sorta).
Before in VB i would just do something like: mscomm.send("255",x,y,z) and VB seemed to handel it.

My BASIC code was something like:
x VAR     Byte
y VAR     Byte
z VAR     Byte

Main:
  SERIN 1, 16780, [noparse][[/noparse]WAIT("255"), DEC x, DEC y, DEC z]
//Branch or do something here
  END




Now, I have switched to C++ and I was wondering what i have to send out exactly to get the same effect. In some cases i'll be sending out a variable that ranges between 0-255 and i do not want it to be parsed wronge, but if i put DEC 3 and the number is 1 or two digit will it parse wronge? Or do i need to add a non-numeric character between data like:
Question said...
255@0@255@15


HERE is the C++ code:
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "serialFunctions.h"

int main(int argc, char **argv)
{
    HANDLE serial_port;                /* Handle to the serial port */
    long baud_rate = 2400;            /* Specified baud rate */
    char port_name[noparse][[/noparse]] = "COM1:";        /* Name of the serial port */
    unsigned long file_size;        /* File size to transmit in bytes */
    unsigned long bytes_written;    /* Bytes written to serial port */
    unsigned long file_name_size;    /* Size of file name */
    
    int lwp = 0;                    // Left Wheel Polarity
    int rwp = 0;                    // Right Wheel Polarity
    int lws = 0;                    // Left Wheel Speed
    int rws = 0;                    // Right Wheel Speed
    char message [noparse][[/noparse]50];
    int n;
    n=sprintf (message, "%d@%d@%d", lwp, rwp, lws, rws);
    
    //exit(0);
    /* Open up a handle to the serial port */
    serial_port = CreateFile(port_name, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);

    /* Make sure port was opened */
    if (serial_port == INVALID_HANDLE_VALUE)
    {
        fprintf(stderr, "Error opening port\n");
        CloseHandle(serial_port);
        exit(0);
    }

    /* Set up the serial port */
    set_up_serial_port(serial_port, baud_rate);

    /* Get the file size */
    //file_size = get_file_size(argv);
    
    /* Print out information */
    //printf("Preparing to transmit file %s: %lu bytes\n", argv, file_size);
    printf("Preparing to transmit data\n");
    printf ("DATA:[noparse][[/noparse]%s] is a %d chars string\n",message,n);
    //printf(argv);
    //exit(0);
    /* Write file name size to serial port */
    //file_name_size = (unsigned long)strlen(argv);
    //WriteFile(serial_port, (void *)&file_name_size, sizeof(unsigned long), &bytes_written, NULL);
    WriteFile(serial_port, (void *)&message, n, &bytes_written, NULL);
    
    if (bytes_written != n)
    {
        fprintf(stderr, "Error writing message.\n");
        CloseHandle(serial_port);
        exit(0);
    }
    else
    {
        fprintf(stderr, "DONE!\n");
    }

    printf("\n%lu bytes successfully transmitted.\n", file_size);
    */
    /* Close the handle */
    CloseHandle(serial_port);

    return 0;
}





this program returns the following:
Output said...
L:\>sertrans.exe
Preparing to transmit data
DATA:[noparse][[/noparse]0@0@0] is a 5 chars string
DONE!

So.. Anybody want to attempt my massive post?

Comments

  • DiablodeMorteDiablodeMorte Posts: 238
    edited 2006-04-14 03:36
    Ok.. I Still need help. Some one please help me!
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-04-14 04:54
    Your basic program waits for "255" then reads 3 integers

    Your c++ program must therefore send

    ····n=sprintf·(message,·"255,%d,%d,%d,%d\r",·lwp,·rwp,·lws,·rws);

    Notice I used the comma as seperator instead of @
    and I added a 4th %d for parameter rws, and a closing CR
    Your basic program will only receive lwp, rwp and lws

    regards peter
  • FranklinFranklin Posts: 4,747
    edited 2006-04-14 05:05
    Not sure I understand. The BASIC code reads data and as far as I can tell the c++ code writes data. can you explain better?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-04-14 05:32
    I believe the c++ code replaces vb code that was used to send data
    to the stamp. Obviously the same bytes must be send, starting with "255"
    otherwise the stamp waits forever.

    Why the sprintf statement uses 4 parameter instead of the 3 (x,y,z in the basic program)
    I don't know. The original poster should explain.

    regards peter
  • DiablodeMorteDiablodeMorte Posts: 238
    edited 2006-04-14 22:29
    Ok. I finally got the comp hooked up to another comp via serial. I'm convinced that the problem lies in the BASIC code.
    Here's my set up so far:

    When I connect to my laptop via serial and run terminal i get 255.0.0. on the laptop(9600 baud). My basic code is:
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    x VAR Byte
    y VAR Byte
    z VAR Byte
    a VAR Byte
    GOTO blink
    Main:

    HIGH 2
    DEBUG "Hola",CR
    SERIN 16, 16468, [noparse][[/noparse]DEC3 z, DEC x, DEC y] '9600 Baud. Inverted

    IF x = 0 THEN blink
    LOW 2
    GOTO Main
    END


    blink:
    HIGH 1
    PAUSE 500
    LOW 1
    PAUSE 500
    HIGH 1
    PAUSE 500
    LOW 1
    GOTO main


    Nothing happens. It just hangs on the serial connection. Any help? I thought that should work
  • DiablodeMorteDiablodeMorte Posts: 238
    edited 2006-04-14 22:47
    Ok. I would like to mention how FREAKING annoyed I am at this. Can someone PLEASE.. Please. I'm begging tell me how serin works. So far the most i can make it do is recieve 1.. yes,, just 1 varaible. Anythingmore than that doesn't work. And the WAIT() command seems to just mess things up farther. Now, i know what my C++ program is outputting but I NEED someone to tell me what to do so i can parse the varaiable. Please, someone help me. Here is my revised code:

    C++: n=sprintf (message,"0\r"); ///STAMP DOES REVIECE THIS VARAIBLE
    BASIC: SERIN 16, 16468, [noparse][[/noparse]DEC1 x] '''SEEMS TO WORK BUT CAN NOT PUT ANYMORE VARIABLES IN THERE
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-04-14 23:14
    If you use in basic

    SERIN 16, 16468, [noparse][[/noparse]wait "255", skip 1, DEC3 z, skip1, DEC3 x, skip 1,·DEC3 y] '9600 baud

    and you send in c++

    n=sprintf·(message,·"255 %03d %03d %03d\r",·z,·x,·y);

    then it should work.
    Notice, in message there is 1 space between variables, each variable occupies 3 characters.
    The 255 synchronizes the message.

    Also note that pin16 (the stamp programming port) echoes everything back
    to your pc.

    regards peter
  • DiablodeMorteDiablodeMorte Posts: 238
    edited 2006-04-14 23:16
    Trying. If this works... I'll think of something

    Nope. Didn't work. I don't think the stamp did anything.

    I did notice something though. Each time i added a varaible(see newold code at bottom) I had to run the program(the C++) as many times as there were varaibles. So if there were xyz i would run it 3 times and it would work. What is that?!

    n=sprintf (message,"1-1-1\r");
    
    

    SERIN 16, Baud, [noparse][[/noparse]DEC x, DEC y, DEC z, DEC a]
    

    Post Edited (DiablodeMorte) : 4/14/2006 11:19:44 PM GMT
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-04-14 23:45
    You could try 1 variable 3 times,

    SERIN 16, 16468, [noparse][[/noparse]wait "Z",·DEC3 z]
    SERIN 16, 16468, [noparse][[/noparse]wait "X",·DEC3 x]
    SERIN 16, 16468, [noparse][[/noparse]wait "Y",·DEC3 y]

    In c++ send
    n=sprintf·(message,·"Z%03d··· X%03d··· Y%03d\r",·z&255,·x&255,·y&255);

    A single non-digit for synchronization, variables forced to 0-255,
    4 spaces between variables to allow basic to move to
    the next statement

    regards peter
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-04-14 23:52
    Maybe you shouldn't close the serial port after sending the c++ bytes.
    (or add a long delay)
    The DTR may be toggled and thus the stamp reset. Also, keep the
    DTR disabled while the serial port is open.

    regards peter
  • allanlane5allanlane5 Posts: 3,815
    edited 2006-04-16 17:45
    I'm terribly sorry about this, but I believe the BS2 'SERIN' wants a space, a tab, or a line-feed between each variable. Perhaps it ONLY accepts a line-feed ('\n') but hopefully not.· It MAY only accept a '\r' character as a delimiter.

    I believe that's what it takes to convince the SERIN 'DEC' modifier that the variable is in fact completely recieved.
Sign In or Register to comment.