Alex Gordon
Alex Gordon

Reputation: 60751

How to send ctrl+z

How do I convert ctrl+z to a string?

I am sending this as an AT COMMAND to an attached device to this computer.

Basically, I just to put some chars in a string and ctrl+z in that string as well.

Upvotes: 7

Views: 32770

Answers (5)

gimel
gimel

Reputation: 86372

When sending characters to a device, translation from the internal string representation is needed. This is known as Encoding - an encoder translates the string into a byte array.

Consulting the Unicode Character Name Index, we find the SUBSTITUTE - 0x001A character in the C0 Controls and Basic Latin (ASCII Punctuation) section. To add a CTRL-Z to an internal C# string, add a unicode character escape sequence (\u001a) code.

String ctrlz = "\u001a";
String atcmd = "AT C5\u001a";

Any encoding used for translation before output to the device (for example output using StringWriter), will translate this to ASCII Ctrl-Z.

Upvotes: 1

sbk
sbk

Reputation: 9508

It's clear from other responses that Ctrl+Z has ASCII code 26; in general Ctrl+[letter] combinations have ASCII code equal to 1+[letter]-'A' i.e. Ctrl+A has ASCII code 1 (\x01 or \u0001), Ctrl+B has ASCII code 2, etc.

Upvotes: 2

Pavel Radzivilovsky
Pavel Radzivilovsky

Reputation: 19114

byte[] buffer = new byte[1];
buffer[0] = 26; // ^Z
modemPort.Write(buffer, offset:0, count:1);

Upvotes: 3

Pranay Rana
Pranay Rana

Reputation: 176906

Try following will work for you

serialPort1.Write("Test message from coded program" + (char)26);

also try may work for you

serialPort1.Write("Test message from coded program");
   SendKeys.Send("^(z)");

also check : http://www.dreamincode.net/forums/topic/48708-sending-ctrl-z-through-serial/

Upvotes: 3

R Samuel Klatchko
R Samuel Klatchko

Reputation: 76541

You can embed any Unicode character with the \u escape:

"this ends with ctrl-z \u001A"

Upvotes: 9

Related Questions