Quick Question: C++, Serial Cables, And the Stamp
DiablodeMorte
Posts: 238
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:
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:
HERE is the C++ code:
this program returns the following:
So.. Anybody want to attempt my massive post?
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
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
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen
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
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
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
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
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?!
Post Edited (DiablodeMorte) : 4/14/2006 11:19:44 PM GMT
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
(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
I believe that's what it takes to convince the SERIN 'DEC' modifier that the variable is in fact completely recieved.