By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,899 Members | 1,066 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,899 IT Pros & Developers. It's quick & easy.

How to send CTRL-S & CTRL-X over serial port

P: n/a
Hello -

I'm trying to wite these control characters to the serial port w/o
success.

I've tried sp.Write((Keys.ControlKey & Keys.X).ToString());
sp.Write((Keys.Control & Keys.X).ToString());

char[] temp = { Convert.ToChar(Keys.Control), Convert.ToChar(Keys.X)
};
sp.Write(temp, 0, 2);

and other screwy variations.

Any suggestions how to do this?

Thanks, Tom
Sep 27 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Sat, 27 Sep 2008 11:00:54 -0700, Blip <bl**@krumpli.comwrote:
Hello -

I'm trying to wite these control characters to the serial port w/o
success.

I've tried sp.Write((Keys.ControlKey & Keys.X).ToString());
sp.Write((Keys.Control & Keys.X).ToString());
What's "sp"? Is that an instance of the SerialPort class?

The bottom line here is that you are trying to mix the user-input data
structures associated with keyboard input with the ASCII used to represent
that input.

The first non-zero byte values for ASCII are reserved for control input.
By convention, the letters are assigned in sequence, with Control-A being
1, Control-B being 2, etc. Control-S would be 19. You can probably
figure out Control-X on your own. :)

Just send the correct byte value. You aren't going to get anywhere trying
to use values from the Keys enumeration, as that's tied to the keyboard,
not ASCII.

Pete
Sep 27 '08 #2

P: n/a
"Blip" <bl**@krumpli.comwrote in message
news:3a********************************@4ax.com...
I'm trying to wite these control characters to the serial port w/o
success.

I've tried sp.Write((Keys.ControlKey & Keys.X).ToString());
sp.Write((Keys.Control & Keys.X).ToString());

char[] temp = { Convert.ToChar(Keys.Control), Convert.ToChar(Keys.X)
};
sp.Write(temp, 0, 2);

and other screwy variations.

Any suggestions how to do this?

You can use the numeric value of the control character: Ctrl-A is ASCII
1, Ctrl-B is 2, Ctrl-C is 3, and so on.

SerialPort sp = ...;
....
char ctrlS = (char)('S'-'A'+1);
char[] charsToWrite = new char[]{ctrlS};
sp.Write(charsToWrite, 0, 1);
Sep 27 '08 #3

P: n/a
MC
To make the same point Pete did, a little more explicitly:

Serial ports don't transmit keys. They transmit bytes, which are normally
the ASCII representations of characters.

"Keys.ControlKey & Keys.X" has to do with sensing what is going on on a PC
keyboard. But serial ports do not transmit the whole state of the keyboard.
For instance, the F keys and the arrows are entirely outside the ASCII
system.

An ASCII chart will tell you that, for instance, Ctrl-A is 1, Ctrl-B is 2,
'A' is 65, and so on.

Sep 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.