Click to See Complete Forum and Search --> : Serial Port Device


Dave Coleman
September 7th, 1999, 01:25 AM
I'm trying to use the serial port to light a lamp. I need to write code that will send the signal to the device. I'm not sure which pin to use as a power source for the lamp, or at least the trigger source; and how to send a signal to that specific pin via C++. It's a 12V lamp, but 2 5V's would work. I've identified a 10.92V lead (Pin 3, Transmit/TxD), that changes to 10.32V when I send it a character. Do different characters send different voltages? Any help on this subject would be greatly appreciated. Thanks

DC

Klaus Bucher
September 7th, 1999, 04:37 AM
Hello DC

Here is a simple codefragment to blink a Lamp over the COM Port. The Lamp should connected on pin 3 TxD.


void WriteSign(void * ComDev)
{
DWORD dwWritten;
char Buffer[] = {" "};

// The Buffer length gives the duration,the Lamp is on (or off)

while(1)
{
Sleep(1000);
if (!WriteFile(ComDev,Buffer,strlen(Buffer), &dwWritten, NULL))
{
// Unable to write data to comport
return;
}
}
}

void StartupCommPort(void)
{
HANDLE ComDev;
DCB dcb = {0};

dcb.DCBlength = sizeof(dcb);

ComDev = CreateFile( "COM2",
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);

if (ComDev == INVALID_HANDLE_VALUE)
{
// Unable to CreateComDevice
return;
}

COMMTIMEOUTS OrgTime;

if (!GetCommTimeouts( ComDev, &OrgTime) )
{
// Unable to Create ComDevice
return;
}


/* Serielles Livesign einblenden */
if (!GetCommState(ComDev, &dcb))
{
// Unable to get CommState
return;
}


//
// update DCB rate, byte size, parity, and stop bits size
//
dcb.BaudRate = 9600;
dcb.ByteSize = 8;
dcb.Parity = 0;
dcb.StopBits = 0;

//
// update event flags
//

dcb.EvtChar = '\0';

//
// update flow control settings
//
dcb.fDtrControl = 1;
dcb.fRtsControl = 1;

dcb.fOutxCtsFlow = 0;
dcb.fOutxDsrFlow = 0;
dcb.fDsrSensitivity = 0;
dcb.fOutX = 0;
dcb.fInX = 0;
dcb.fTXContinueOnXoff = 0;
dcb.XonChar = 17;
dcb.XoffChar = 19;
dcb.XonLim = 0;
dcb.XoffLim = 0;

//
// DCB settings not in the user's control
//
dcb.fParity = TRUE;

//
// set new state
//
if (!SetCommState(ComDev, &dcb))
{
// Unable to set CommState
return;
}


//
// set new timeouts
//
COMMTIMEOUTS NewTime;
if (!SetCommTimeouts(ComDev, &NewTime))
{
// Unable to set ComTimeout
return;
}

if (!EscapeCommFunction(ComDev, SETDTR))
{
// Unable to EscapeCommFunction
return;
}

/* Start a thread who blink the Lamp */
HANDLE ThisThread = CreateThread(NULL,0,
(LPTHREAD_START_ROUTINE )WriteSign,
ComDev,0, NULL);
SetThreadPriority(ThisThread,THREAD_PRIORITY_BELOW_NORMAL);

}

I hope this help you

Dave Coleman
September 7th, 1999, 10:41 AM
Thanks. I've written very similar code, but this code clears up a lot for me. I appreciate it!

DC

September 7th, 1999, 10:51 AM
I would use a parallel port to do stuff like this. Serial port is for communications. Besides trying to lit a bulb may not work properly. The port may not be able to provide enough power.

In case Parallel port may not have enough power for you, you can use external voltage source and drive a relay using your parallel port. A word of caution, playing with your ports and external voltage supply can damage your port or your PC if you dont hook them up properly. I would suggest that you buy some additional parallel port which fits on your PC slot and experiment on that. In case you do burn it, you can replace it.

Dave Coleman
September 7th, 1999, 11:13 AM
What you suggested was actually my plan, using the serial port. The parallel port on the machines is taken, so serial is my only option. I've got a couple options. I could use one 12V lamp, two 5V lamp's, or, if the serial port doesn't give enough juice, a 12V lamp and a capacitor, or an external power source. I've already purchased a couple of serial and parallel cards for testing purposes, and the external power source as well, but I'm trying to keep it simple. The part that confuses me is the software end. When I send a signal, the voltage appears to drop, not rise. Can I send different signals from the serial port? I'm going to try the code from Klaus, but any suggestions would be welcome.

DC

ImranRajwani
September 7th, 1999, 03:11 PM
Here is the suggestion again. Use Parallel ports. If you dont have free, buy one and add it. Serial ports are used to transmit data. You wont be able to mantain a steady state using TX line. These are all pulses. In Parallel port, data is latched. Further you have control lines available too. Bottom line is, serial is not designed for what you have in mind.

Kelly
September 8th, 1999, 12:28 PM
In order to really learn how this works, I would recommend looking into getting ahold of, or even building from a schematic, a serial port break-out box. A break-out box is a small device which normally has LED's indicating the active status of each line on the port, and often has contact points for attaching test instrumentation or performing custom wiring for things like NULL modem, etc. Information on these things is plastered all over the place, so you shouldn't have any problem finding it on the Net. Moreover, you could probably find one in your local technical computing shop pre-made and ready for use.

Dave Coleman
September 8th, 1999, 02:25 PM
Thanks. I have completed the device. I think I'll make that box you mentioned just for the practice. I really appreciate everyones help!

DC

Dave Coleman
September 10th, 1999, 03:24 PM
Another issue has arisen in this little project. I used the 3-pin (TxD) as a power source to trigger the device, and grounded the 12V 3-pin (which is the only power in the device) to the 5-Pin ground. Is this going to short my serial port, or any of the circuitry the serial port connects to?

Thanks!

DC

June 2nd, 2000, 11:51 AM
I think your ~12v to ~11v 'drop' is not actually a drop on pin3/tx (I know this is real old but figured I'd post anyways) but is either a 12v->5v or 12v->0v high/low pulse rather and you are just seeing it as a drop because it happens so fast and your probably looking at it w/ a dvm... I bet if you tried hold on lower value if you have a good dvm etc. you'd see 0v or 5v.. something other than ~11v ... that just doesn't make sense to me

kris