Sending commands through serial port

S

Thread Starter

serial port

<p>Hi, everyone.

<p>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++.

<p>I want to send commands like these, "A50","B85".
then the device move.

<p>I try to compile&run that open port and set the parameter the followings:
<pre>
#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);
</pre>

<p>But I'm stuck with how to write the commands to serial port.
Does anybody can help???
Please reply.
 
<p>A simple example would be to create a character string containing the data you want to send:
<pre>
uchar StringA50[3] = {'A','5','0'};
uchar StringB85[3] = {'B','8','5'};
</pre>
<p>To send "A50" you would just issue the command:
<pre>
status = WriteFile(hCom, // Handle
&StringA50, // Address of Outgoing data
3, // Number of bytes to write
&bytes_written, // Returned number of bytes written
NULL);
</pre>
<p>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.
<p>
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.

<p>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.
 
R

Robert Scott

Use WriteFile() to actually to the write to the serial port.

Robert Scott
Real-Time Specialties
Embedded Systems Consulting
 
Hello there,
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
 
Thank you.
This is very helpful!!!

At last I could manage to move the module through sending command.
 
I am writing a similar application and have a question. I need to send a command Rx\r in which x varies everytime I send the command. x could have integer values between -25,000 to + 25,000. Can you suggest a method to use a single WriteFile function to send multiple commands like above.

Thanks.
 
Top