• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3550
  • Last Modified:

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!
0
andrepires
Asked:
andrepires
  • 6
  • 5
  • 2
1 Solution
 
burrcmCommented:
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
0
 
alex_pavenCommented:
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.

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

Thanks!
0
Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

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

Chris B
0
 
alex_pavenCommented:
There's no actual difference except the names given by the standard. The pins in question were used by modems for flow control, so they had to be given a certain denomination, but you can use them for anything as long as you don't use a modem or similar piece of equipment with very strict requirements.

CTS = Clear To Send (signal to start sending data)
DSR = Data Set Ready (signal to prepare for receiving data)
CD = Carrier Detect (equipment is present)

You could also use RI (Ring Indicator), pin 9, but in my experience it's a bit unreliable, i.e. not all serial port chips trigger it correctly. It should work with a standard wired pc serial port though.

The only real significance of the names is for you to know which properties of the SerialPort class to look at in your program. So if you use, for instance, CTS (8) pin of serial port 1 to detect if a door is open or closed, you have:

SerialPort s = new SerialPort("COM1");
if (s.CtsHolding) {
  // door is open
} else {
  // door is closed
}

or alternatively,
s.PinChanged += new PinChangedEventHandler(myFunction);

void myFunction(object sender, SerialPinChangedEventArgs e)
    {
      if (e.EventType == SerialPinChange.CtsChanged)
      {
        if ((sender as SerialPort).CtsHolding)
        {
          // door open
        }
        else
        {
          // door closed
        }
      }
    }
0
 
andrepiresAuthor Commented:
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.
0
 
alex_pavenCommented:
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 :)
0
 
andrepiresAuthor Commented:
Well, I selected C# and Hardware.
Don't know why it is in LDAP.
Can we request EE to change it?
0
 
alex_pavenCommented:
Already did, but it's not that important anyway, don't worry.
0
 
andrepiresAuthor Commented:
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
0
 
alex_pavenCommented:
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.
0
 
andrepiresAuthor Commented:
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!
0
 
alex_pavenCommented:
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.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 6
  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now