如何使用c#将ctrl + z发送到串口

时间:2016-07-14 18:49:22

标签: c# serial-port gsm

我尝试过以下方法: (这"无论"是一个4位数的引脚,在向端口发送AT + STGR = 3,1后要求输入)

1. this.port.WriteLine("whatever\0x1A");

2. this.port.WriteLine("whatever"+ char.ConvertFromUtf32(26));

3. this.port.WriteLine("whatever\u0001");

4. this.port.WriteLine("whatever"+(char)26);

5. this.port.WriteLine("whatever");
   SendKeys.Send("^(z)");

6. this.port.WriteLine("whatever");
   this.port.Write(new byte[] { 0x1A }, 0, 1);

7. this.port.WriteLine("whatever");
   this.port.Write(new byte[] { 0x26}, 0, 1);

它们都不起作用,但是当使用putty并输入代码后跟ctrl + z键时,一切都运行正常,所以有人能告诉我putty究竟是如何将此ctrl + z发送到串口的吗?或者如果可能的话,在c#中解决这个问题? 每次尝试上面给出的c#代码时,调制解调器的回复是:

+CME ERROR: 100

串口初始化:

            port.PortName = "COM3";
            port.BaudRate = 115200;
            port.DataBits = 8;
            port.StopBits = StopBits.One;
            port.Parity = Parity.None;
            port.ReadTimeout = 300;
            port.WriteTimeout = 300;
            port.Encoding = Encoding.GetEncoding("iso-8859-1");
            port.DataReceived += new SerialDataReceivedEventHandler(this.port_DataReceived);
            port.Open();
            port.DtrEnable = true;
            port.RtsEnable = true; 

2 个答案:

答案 0 :(得分:1)

为ctrl代码找到像this这样的ASCII地图。看起来像ctrl Z你需要一个ASCII 0x26。我会定义类似

的东西
char CtrlZ = (char)26;
char CR = (char)13;

serialport1.WriteLine(string.Format("whatever{0}{1}",CtrlZ, CR));

答案 1 :(得分:0)

尝试"whatever\u001A""whatever" Sleep "\u001A"

同时检查您通过连接发送/接收的整个命令/响应序列。

这是一个例子,可以根据您的具体情况进行调整。

//TODO initialize the serialConnection and open it
//TODO specific commands...
serialConnection.WriteLine("AT+CPIN=\"1234\""); // replace according to your real situation
var response = serialConnection.ReadExisting();
Console.WriteLine("pin resp: " + response);
Thread.Sleep(200);
//TODO other specific commands?
serialConnection.Write("whatever");
//Thread.Sleep(200); uncomment this if it helps
serialConnection.Write(new byte[]{26}, 0, 1); // or Write("\u001A")
response = serialConnection.ReadExisting();
Console.WriteLine("ctrl-z resp: {0}", response);
//closing stuff, etc...