Click here to Skip to main content
16,004,761 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am using c++ visual studio 2008 express, writing a program to control a robot.

I have a serialport on the main form 'serialPort1' and wish to talk to it from the cpp file 'robot 300610.cpp'.

Could someone please help me with the syntax?

I can use the serialport inside 'Form1.h' but not from elsewhere.

I only need to write a string to the port.

I have googled this and tried the methods offered without success and as I'm new to C++, having done all my programming in assembler up to this point, I do not know all the syntax or even what everything is called in C++.

Thanks,
Dave Everett
Posted
Comments
SnowHow 2-Aug-10 8:27am    
What error are you getting? What are you using for serial communication? Have you tried separating the communications portion of the code from the rest?

What do you mean by the sentence "I can use the serialport inside 'Form1.h' but not from elsewhere."? I assume you are using managed C++ (aka C++/CLI) to build your program but you actually want to use pure C++. Take a look at some of these articles[^] for some suggestions as to how you may do it.
 
Share this answer
 
Thanks Richard.

I mean't I can use the serial port inside the form code:
this->serialPort1->Write("!SCVER?");
results in that string being sent out the serial port to the device, but that line is not usable in my main cpp file.
It's in the main cpp file that I need to control the port.

The program is CLR, I changed the setting to Pure C++ but that generates errors during build when some of the OpenCV files are compiled.

Unfortunately the article link in your reply goes to a blank search window.

Dave Everett
 
Share this answer
 
Comments
Richard MacCutchan 2-Aug-10 4:07am    
Sorry about the link, just put "serial port" in the search box and you will see a lot of articles. As to moving the code from your form to your main module this is a simple matter of instantiating your serialport1 class in your main module instead of in your form.
WantToLearn1 25-Jan-23 9:16am    
Any example of how to instantiate serialPort1 to main.cpp from myForm.h?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900