Hi, everyone.
I've got a project that sending control commands to the module (small device that moves camera pan and tilt) through serial port with C, C++, using VC++.
I want to send commands like these, "A50","B85".
then the device move.
I try to compile&run that open port and set the parameter the followings:
#include <windows.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
DCB dcb;
HANDLE hCom;
BOOL fSuccess;
char *pcCommPort = "COM2";
hCom = CreateFile( pcCommPort,
GENERIC_READ | GENERIC_WRITE,
0, // must be opened with exclusive-access
NULL, // no security attributes
OPEN_EXISTING, // must use OPEN_EXISTING
0, // not overlapped I/O
NULL // hTemplate must be NULL for comm devices
);
if (hCom == INVALID_HANDLE_VALUE)
{
// Handle the error.
printf ("CreateFile failed with error %d.\n", GetLastError());
return (1);
}
// Build on the current configuration, and skip setting the size
// of the input and output buffers with SetupComm.
fSuccess = GetCommState(hCom, &dcb);
if (!fSuccess)
{
// Handle the error.
printf ("GetCommState failed with error %d.\n", GetLastError());
return (2);
}
dcb.BaudRate = 9600; // set the baud rate
dcb.ByteSize = 8; // data size, xmit, and rcv
dcb.Parity = NOPARITY; // no parity bit
dcb.StopBits = ONESTOPBIT; // one stop bit
fSuccess = SetCommState(hCom, &dcb);
if (!fSuccess)
{
// Handle the error.
printf ("SetCommState failed with error %d.\n", GetLastError());
return (3);
}
printf ("Serial port %s successfully reconfigured.\n", pcCommPort);
return (0);
But I'm stuck with how to write the commands to serial port.
Does anybody can help???
Please reply.
A simple example would be to create a character string containing the data you want to send:
uchar StringA50[3] = {'A','5','0'};
uchar StringB85[3] = {'B','8','5'};
To send "A50" you would just issue the command:
status = WriteFile(hCom, // Handle
&StringA50, // Address of Outgoing data
3, // Number of bytes to write
&bytes_written, // Returned number of bytes written
NULL);
The number of bytes actually written (which should be 3) will be returned in bytes_written and the overall status returned in status. If status indicates an error, you can call GetLastError to see the actual error code returned.
If you want to dynamically build the strings you could :<br>
StringA50[0] = 'A';<br>
StringA50[1] = '5';<br>
StringA50[2] = '0';<br>
instead of defining them as constants.
Hope this helps. Sealevel Systems (http://www.sealevel.com) offers C++ and VB code samples on the CD that ships with their products. They offer RS-232/422/485 and Digital I/O products in a variety of interfaces and form factors, including PCI, USB, Ethernet, PC/104, and more. They also offer technical support and a Lifetime Warranty on their products.
I am about to start a project where communication between controller and pc is needed. I will be using C++ builder, I have the Rs232. To be honest iam not sure how and where to start.
What is MSCOMM?
How can i send a command from the form of the builder and be displayed on the LCD of the controller?
Any help will be very very much appreciated.
thanks in advance.
Kane
This is very helpful!!!
At last I could manage to move the module through sending command.
Thanks.
Robert Scott
Real-Time Specialties
Embedded Systems Consulting
Using TransmitComChar, u can send any thing via COM :)
Users of this site are benefiting from open source technologies, including PHP, MySQL and Apache. Be happy.
Fortune
The fortune program is supported, in part, by user contributions and by
a major grant from the National Endowment for the Inanities.







