-1

I have tried the following: (This "whatever" is a 4 digit pin asked to enter after sending a AT+STGR=3,1 to the port)

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);

None of them works, but when using putty and entering the code followed by ctrl+z key everything works perfectly, so can anyone tell me exactly how does putty sends this ctrl+z to the serial port? Or if possible give a solution to this problem in c#? The reply back from the modem every time I try the c# codes given above is:

+CME ERROR: 100

Serial Port Initialization:

            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; 
Adib
  • 359
  • 1
  • 6
  • 16
  • read the response / answer here http://stackoverflow.com/questions/4862684/sending-ctrlc-over-serial-port-in-c – MethodMan Jul 14 '16 at 18:54
  • @MethodMan I have tried (char)26 which is supposed to send the ascii equivalent of sending a ctrl+z. But as I mentioned it didnt work out. Or do you think this is not the way to send it? If so, could you please write the proper way of sending it? – Adib Jul 14 '16 at 18:58
  • Use Write Byte 0x26. – jdweng Jul 14 '16 at 19:03
  • @jdweng could you please kindly write the code in as an answer? – Adib Jul 14 '16 at 19:06
  • try this : this.port.Write(new byte[] { 0x1A }, 0, 1); – jdweng Jul 14 '16 at 19:11
  • @jdweng tried, but no luck :( – Adib Jul 14 '16 at 19:14
  • @jdweng please check number 6 and 7 if that is the way you meant if to be written. – Adib Jul 14 '16 at 19:20
  • Make sure the port is set to 8 bit no parity. this.port = new System.IO.Ports.SerialPort("Com1", 9600, System.IO.Ports.Parity.None, 8, System.IO.Ports.StopBits.One); this.port.Handshake = System.IO.Ports.Handshake.None; – jdweng Jul 14 '16 at 19:21
  • Since Putty works for you, are you positive that the port settings in Putty are also used in your program? @jdweng -- ASCII codes do not require 8-bit char frames. – sawdust Jul 14 '16 at 19:27
  • @jdweng it was set exactly like how you said. – Adib Jul 14 '16 at 19:27
  • @sawdust, ya I am positive about that. – Adib Jul 14 '16 at 19:27
  • It seems there is something different in your serial port configuration. Post the code you're using to initialize and configure your serial port. Otherwise there's nothing we can do to help. – Jim Mischel Jul 14 '16 at 20:46
  • Also, what does "it doesn't work" mean? Are the other characters written, but not the Ctrl+Z? Is something other than Ctrl+Z received in its place? When you do `this.port.WriteLine("whatever\0x1a");`, what is received on the other end? – Jim Mischel Jul 14 '16 at 20:50
  • @JimMischel I have added the port config in the question, and yes the "whatever" part do get passed to the serial port perfectly, it is only the ctrl+z which is making the problem. – Adib Jul 14 '16 at 21:03
  • @MachineLearning, if this was a duplicate then I wouldn't have even posted it as not working. If a space is given between then wouldn't it take that space as a part of that "whatever"? – Adib Jul 15 '16 at 03:37
  • @JimMischel, this is what replied back from the modem "+CME ERROR: 100" – Adib Jul 15 '16 at 03:52
  • Is this of any help to you? http://stackoverflow.com/questions/20111517/gsm-modem-ussd-check-balance-getting-cme-error-100 – Jim Mischel Jul 15 '16 at 03:58
  • @JimMischel, thanks, but unfortunately its not related, I am using stk and at a point I am supposed to enter a pin, I had used ussd like shown there but ussd codes ran perfectly. So, my best guess is that the \0x1A is also being taken as a part of the pin string instead as a hex for ctrl+Z. – Adib Jul 15 '16 at 05:19
  • Try "whatever\u001A" or "whatever" sleep(200) "\u001A" –  Jul 15 '16 at 05:44
  • What verification method are you using? Maybe the code is being sent but your verification method isn't returning accurate results. – jdweng Jul 15 '16 at 09:12

2 Answers2

0

Try "whatever\u001A" or "whatever" Sleep "\u001A".

Also check the whole sequence of commands/responses you're sending/receiving through the connection.

This is an example, to be adapted to your specific case.

//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...
  • problem is this modem don't hold input buffers. So, there is no way to confirm exactly how the inputs are received by the modem. It works up to the point where I send AT+STGR=3,1 to get the ">" where I am supposed to enter the pin and press ctrl+z to confirm. I will try by adding a thread sleep between the pin and the ctrl+Z and let you know. But, I hardly think that will solve anything. – Adib Jul 15 '16 at 13:15
  • Here I'm also suggesting to enter return (WriteLine) after the pin (then sleep and Ctrl+z) –  Jul 15 '16 at 13:33
  • you may need a return `\r` also after the `AT+STGR=3,1` and before the pin –  Jul 15 '16 at 14:15
0

Find a ASCII map like this for the ctrl codes. Looks like for ctrl Z you need an ASCII 0x26. I would define it something like

char CtrlZ = (char)26;
char CR = (char)13;

serialport1.WriteLine(string.Format("whatever{0}{1}",CtrlZ, CR));
Baddack
  • 1,947
  • 1
  • 24
  • 33