advertisement
from the Automation List department...
Sending commands through serial port
Programming languages. topic
Posted by serial port on 19 July, 2005 - 8:07 pm

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.



Posted by wk on 20 July, 2005 - 8:03 pm

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.



Posted by Kane on 24 July, 2005 - 1:14 pm
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


Posted by Anonymous on 1 August, 2005 - 10:51 pm
Thank you.
This is very helpful!!!

At last I could manage to move the module through sending command.


Posted by Anonymous on 28 November, 2006 - 11:15 pm
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.


Posted by TimC on 20 July, 2005 - 8:33 pm
int WriteFile( dword Handle, byte *buffer, dword len, dword *BytesWritten, dword Overlapped )


Posted by Robert Scott on 20 July, 2005 - 8:40 pm
Use WriteFile() to actually to the write to the serial port.

Robert Scott
Real-Time Specialties
Embedded Systems Consulting


Posted by Anonymous on 17 June, 2006 - 12:14 pm
Hi u !
Using TransmitComChar, u can send any thing via COM :)

Your use of this site is subject to the terms and conditions set forth under Legal Notices and the Privacy Policy. Please read those terms and conditions carefully. Subject to the rights expressly reserved to others under Legal Notices, the content of this site and the compilation thereof is © 1999-2010 Nerds in Control, LLC. All rights reserved.

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.
Advertise here
advertisements
Servo, steppers, analog, digital & web HMI - Fully Integrated!
Time to incorporate data handling, web HMI and motion in one system!
MatrikonOPC has over 500 OPC interfaces and out of the box software. Find an OPC Server for your devices here.
Servo, stepping motor control, analog & web HMI in one system!
Control.com is the largest Automation community on the web. Learn how to advertise here now...
our advertisers
Help keep our servers running...
Patronize our advertisers!
Visit our Post Archive