Link to home
Start Free TrialLog in
Avatar of andrepires
andrepiresFlag for Brazil

asked on

Help using PC serial port

Hello experts!
I need to create an application using C# and the serial port (D-SUB) of my computer.
The goal is to create a solution that will detect when a car's door is opened/closed and start/stop recording videos using a web cam.
This solution will be used in our company so we can manage the usage of our transportation vehicles (VANS).
Whe the passengers door is opened the web cam should start recording videos. When it's closed, the recording must stop.
I'm creating a cable that will be connected to the door's switch and to a car PC running a .Net C# application.
I searched the internet for a solution and found that I can use the PC serial port to solve the problem.
The problem is that I didn't find any helpful electronic schema explaining how to proceed.
I don't know about the connector pins, voltage, etc.
I found that one (or more) pins of the D-SUB connector has +5VDC available for use.
The idea is taking that 5VDC pin and use it to detect if the door is opene;closed.
Could you please help me to create the switch cable schematics, so I can create the application that will detect when the door is opened/closed?

Thanks for helping!
Avatar of Chris B
Chris B
Flag of Australia image

Don't have details to hand, but the game/joystick port is the easiest to use to detect open/close as it is designed for this (joystick push buttons). I have an old probably vb prog for this somewhere, will look it up.

Chris B
Actually, it's pretty easy to use the serial port for this; simply connect the GND pin (5) of the serial port to one of the input pins when you want that pin to be 'on' (by means of an externally controlled switch); the pins are:
CTS: pin 8
DSR: pin 6
CD: pin 1

Then you can use either the PinChanged event in .Net or continuously poll CtsHolding, DtrHolding or CdHolding properties of the serial port to monitor for changes.

Avatar of andrepires

ASKER

Hello Alex!
What is the difference between CTS, DSR and CD pins?
Burrcm: Is your vb program available? Is it VB.Net?

Thanks!
I will locate it - at work tomorrow and post it for you. Will be vb or basic.

Chris B
ASKER CERTIFIED SOLUTION
Avatar of alex_paven
alex_paven
Flag of Romania image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for helping Alex! That is what I was looking for.
Chris B: Thank you too. It's not necessary to send me the VB code, since Alex provided me with C# code instructions.
Just one note: please select a more appropriate category next time, I now have some points in a category I have absolutely nothing to do with - LDAP :)
Well, I selected C# and Hardware.
Don't know why it is in LDAP.
Can we request EE to change it?
Already did, but it's not that important anyway, don't worry.
Hello Alex.
I created a simple C# program using your tips, but it is not working.
I tested CTS(pin8) and DSR(pin6).
I seams like the pins are different from the standards, don't know...
I used my multimeter to measure the pins voltage in the car PC, using the pin 5 as GND. The resultas are:

Pin               Voltage
1                  +0.14V
2                  +0.14V
3                  -10.95V
4                  -10.95V
6                  +0.14V
7                  -10.95V
8                  +0.14V
9                  +0.14V

What does the table above say to you?

Regards,

Andre
It says to me that I'm an idiot; I gave a rushed response and confused some things.
You need to signal one of the pins with electrical current, not shorting it with the ground pin. The easiest solution would be to connect the DTR (4) pin to, say, CTS (8); however, you should also place a resistor between them, so you don't accidentally burn the circuits. A quick search revealed 6.8k to be the type of resistor needed, but if you can wait until tomorrow I can ask a friend who's doing this kind of stuff for a living for a precise answer.

So, the complete circuit would be: pin 4 ----- switch ---- resistor ----- pin 8; then, all you need to do is make sure pin 4 is powered ( serialport.DtrEnable = true ) before attempting to read the status of pin 8 as I described before. So when the switch is closed, pin 4 will have -11V (in your case) and transmit that to pin 8, which will translate into serialport.CtsHolding being true.
Great!
Please, confirm with your friend which pins I have to use.
I can't afford burning the circuits of the car PC. It's too expensive! :)

Thanks for helping!
Alright, it seems you can leave out the resistor since you would be using the serial port's own power source; the recommended pin pairs to connect are 4 to 6 and 7 to 8, but the important thing is only to connect an output to an input.
Let's assume 7 and 8 (RTS and CTS); you then need to set RtsEnabled when your program starts (after you initialize/open the serial port) and watch for changes on CtsHolding.