7

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.

Werner Henze
  • 16,404
  • 12
  • 44
  • 69
Alex Gordon
  • 57,446
  • 287
  • 670
  • 1,062

5 Answers5

9

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

"this ends with ctrl-z \u001A"
R Samuel Klatchko
  • 74,869
  • 16
  • 134
  • 187
3
byte[] buffer = new byte[1];
buffer[0] = 26; // ^Z
modemPort.Write(buffer, offset:0, count:1);
bluish
  • 26,356
  • 27
  • 122
  • 180
Pavel Radzivilovsky
  • 18,794
  • 5
  • 57
  • 67
3

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/

Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
2

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.

Werner Henze
  • 16,404
  • 12
  • 44
  • 69
sbk
  • 9,212
  • 4
  • 32
  • 40
1

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.

Werner Henze
  • 16,404
  • 12
  • 44
  • 69
gimel
  • 83,368
  • 10
  • 76
  • 104