hi to all,

can anyone help me writing a program that can set a pin of the serial com1 port at 12v+?

it must be simple command line based,for example, program.exe -on or -off.

any pin will work.

thanks in advance :)

Recommended Answers

All 7 Replies

You just cannot control the port so precisely that you can switch a pin on or off as you like.
If you want to use your computer as a switch, you should had some additional electronics behind
the port.
The problem is that any information sent to the port appears during a short period of time.

This windows program just switches the TX pin of the port during a short period
(enough to be detected by a circuit anyway).

#include <windows.h>
#include <iostream>
#include <string>
using namespace std;

void set_com_pin(bool value);

int main(int argc, char *argv)
{
	if(argc != 2)
	{
		cerr<<"bad usage !"<endl;
		return -1;
	}
	
	bool onoff;
	if(string(argv[1])==string("-on"))onoff=true;
	else if(string(argv[1])==string("-off"))onoff=false;
	else 
	{
		cerr<<"bad parameters !"<<endl;
		return -1;
	}
	
	set_com_pin(onoff);
	return 0;	
}

void set_com_pin(bool value)
{
	HANDLE hPort=CreateFile("COM1", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL);
	if(hPort==INVALID_HANDLE_VALUE)
	{
		cerr<<"there is a problem"<<endl;
		return;
	}
	DCB dcb;
	GetCommState(hPort, &dcb);
	dcb.BaudRate=CBR_110; //we slow down the port
	SetCommState(hPort, &dcb);

	//we send the data;

	char data=value?0xFF:0x00;
	DWORD written=0;
	WriteFile(hPort, &data, 1, &written, NULL);
	CloseHandle(hPort);
	if(written!=1)
	{
		cerr<<"there is a problem"<<endl;
	}
}

another solution could be the use of two programs : a daemon, in a separate process, that repeatedely writes on the serial port, an a controller, that would
communicate with the daemon to turn it on/off (your program.exe), using named mutexes.

If I have some more time...

hi crazydieter,
thanks for your code!
well, actually just having the 12v+ signal for a moment can be enough for what im doing,as you guessed im triggering a pic to start a routine and the pic is controlling some pins in the lpt port.

i just tried to compile your code, but, i have almost 0 knowledge about coding and programming.

i have 2 compilers in this machine ,djgpp and mingw.

can you tell me what commands to use for compiling the code?

thanks again :)

No problem !

In fact, there were some errors in my code (i haven't tried to compile and test it yet).

Here is a corrected working version :

#include <windows.h>
#include <iostream>
#include <string>
using namespace std;

void set_com_pin(bool value);

int main(int argc, char **argv)
{
	if(argc != 2)
	{
		cerr<<"bad usage !"<<endl;
		return -1;
	}

	bool onoff;
	if(string(argv[1])==string("-on"))onoff=true;
	else if(string(argv[1])==string("-off"))onoff=false;
	else
	{
		cerr<<"bad parameters !"<<endl;
		return -1;
	}

	set_com_pin(onoff);
	return 0;
}

void set_com_pin(bool value)
{
	HANDLE hPort=CreateFile("COM1", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	if(hPort==INVALID_HANDLE_VALUE)
	{
		cerr<<"there is a problem"<<endl;
		return;
	}
	DCB dcb;
	GetCommState(hPort, &dcb);
	dcb.BaudRate=CBR_110; //we slow down the port
	SetCommState(hPort, &dcb);

	//we send the data;

	char data=value?0x00:0xFF;//in fact, for serial ports, logical zero corresponds to +12v (!)
	DWORD written=0;
	WriteFile(hPort, &data, 1, &written, NULL);
	CloseHandle(hPort);
	if(written!=1)
	{
		cerr<<"there is a problem"<<endl;
	}
}

As it uses windows-specific libraries, you have to use mingw to compile it.

personally, I use dev-cpp.

try :
g++.exe com.cpp -o com.exe. If you have errors, send me a private message with an email adress so i can send you the compiled program.

Dieter

hi crazydieter,

i had a problem compiling the code again:

C:/MINGW/BIN/../lib/gcc-lib/mingw32/3.2.2/../../../libstdc++.a(c++locale.o)(.tex
t+0x3a0): undefined reference to `strtold'

can you please mail me the binary?

jesusdlvr@hotmail.com

thanks!

hi crazydieter,

i had a problem compiling the code again:

C:/MINGW/BIN/../lib/gcc-lib/mingw32/3.2.2/../../../libstdc++.a(c++locale.o)(.tex
t+0x3a0): undefined reference to `strtold'

Maybe use strtod instead of strtold and be sure to #include <cstdlib>.

can you please mail me the binary?

<< moderator edit: removed email address >>

thanks!

Please post questions and replies to the forum.

http://www.daniweb.com/techtalkforums/faq.php?faq=daniweb_faq#faq_rules

Keep it on the site
Please do not post asking for an answer to be sent to you via email. Problems and their responses assist others who read them. Please refrain from responding to people's questions via email for the same reason. Moderators may snip email addresses out of such posts without notice. That being said, please do not email or PM forum staff with your support questions. Also please do not ask support questions in the DaniWeb IRC chat. The chat is meant to offer a more laid back community atmosphere where members can get to know each other, and not as a venue to constantly ask/answer people's computer frustrations.

Hi i need to switch on & switch off my device using dtr pin in db9. i am using max232 ic.
hardware side evrything is fine but i need software coding to control dtr in db-9(-12V TO +12V). iam trying to do in C or C++. CAN YOU HELP ME IN THIS PROJECT ASAP.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.