none
C# Hyper Terminal Emulation / Serial Port Adapter read and write Issues RRS feed

  • Question

  • Hi,

    I am building a sort of Hyper Terminal Adapter (Serial port interface) to work with modems / air cards for sniffing.

    I am stuck when i attempt to read the information returned from the serial connection. I can't seem to get it to return anything, although it appears it is sending the command through the port. I saw some other implementations which set and wait for an event, however that seems to be more for an interactive application (an unwanted complexity i would like to avoid) as opposed to my intended use of sending one or two commands and passing the values to a string variable in the parent application.

    Here is the Adapter:

    1using System; 
    2using System.IO.Ports; 
    3using System.Threading; 
    4using System.Windows.Forms; // temp use during debug 
    5 
    6public class HyperTerminalAdapter { 
    7 
    8    SerialPort oSerialPort = new SerialPort(); 
    9 
    10    // Allow the user to set the appropriate properties. 
    11    public int BaudRate = 9600; 
    12    public int DataBits = 8; 
    13    public int ReadTimeout = 500; 
    14    public int WriteTimeout = 500; 
    15    public string PortName = "COM4"
    16    public string Handshake = ""
    17    public string Name = "user"
    18    public string DataReceived = ""
    19    public string sParity = "none"
    20    public int iStopBits = 1; 
    21     
    22 
    23 
    24    public HyperTerminalAdapter() { 
    25        this.Configure(); 
    26    } 
    27 
    28    public void Configure() { 
    29        oSerialPort.PortName = this.PortName; 
    30        oSerialPort.BaudRate = this.BaudRate; 
    31        oSerialPort.DataBits = this.DataBits; 
    32        oSerialPort.ReadTimeout = this.ReadTimeout; 
    33        oSerialPort.WriteTimeout = this.WriteTimeout; 
    34 
    35        oSerialPort.Handshake = System.IO.Ports.Handshake.None; 
    36         
    37        if(this.sParity == "even"){ 
    38            oSerialPort.Parity = Parity.Even; 
    39        }else if(this.sParity == "odd"){ 
    40            oSerialPort.Parity = Parity.Odd; 
    41        }else if(this.sParity == "mark"){ 
    42            oSerialPort.Parity = Parity.Mark; 
    43        }else if(this.sParity == "space"){ 
    44            oSerialPort.Parity = Parity.Space; 
    45        } else { 
    46            oSerialPort.Parity = Parity.None; 
    47        } 
    48 
    49        if(this.iStopBits == 0){ 
    50            oSerialPort.StopBits = StopBits.None; 
    51        }else if(this.iStopBits == 1.5){ 
    52            oSerialPort.StopBits = StopBits.OnePointFive; 
    53        }else if(this.iStopBits == 2){ 
    54            oSerialPort.StopBits = StopBits.Two; 
    55        } else { 
    56            oSerialPort.StopBits = StopBits.One; 
    57        } 
    58 
    59        MessageBox.Show("Configured"); 
    60    } 
    61 
    62    public void Connect() { 
    63        try { 
    64            if (!oSerialPort.IsOpen) { 
    65                oSerialPort.Open(); 
    66                MessageBox.Show("Connected"); 
    67            } // else already open 
    68        } catch (Exception e1) {  
    69                MessageBox.Show("Error: Connection is in use or is not available: \n\n" + e1);  
    70        } 
    71    } 
    72 
    73    public void Disconnect() { 
    74        try
    75            if (oSerialPort.IsOpen) { 
    76                oSerialPort.Close(); 
    77                MessageBox.Show("Disconnected"); 
    78            } //else not open 
    79        } catch { } 
    80    } 
    81 
    82    public void Write(string sData /* string data to write to the port */ ) { 
    83        if (oSerialPort.IsOpen) { 
    84            try { 
    85                oSerialPort.WriteLine(sData); 
    86                MessageBox.Show("Command Issued: " + sData); 
    87            } catch { } 
    88        } 
    89    } 
    90 
    91    public string Read() { 
    92        try { 
    93            this.DataReceived = oSerialPort.ReadLine().ToString(); 
    94            MessageBox.Show(this.DataReceived); 
    95            return (this.DataReceived); 
    96        } catch { 
    97            return ""
    98        } 
    99    } 
    100 
    101// exit class 
    102 



    Here is a simplified snipplet of the intended usage:

    1
    2   HyperTerminalAdapter oHyperTerminalAdapter = new HyperTerminalAdapter(); 
    3   oHyperTerminalAdapter.Connect(); 
    4   oHyperTerminalAdapter.Write("AT");
       string x = oHyperTerminalAdapter.Read();
    5   oHyperTerminalAdapter.Disconnect(); 
    6   MessageBox.Show("Result of the command was: " + x);






    Thursday, July 3, 2008 1:40 AM

Answers

  • I checked to make sure i entered a return code, I actually found the answer to this late last night.. it's been quite some time since I worked with drivers and forgot they tend to be a little slow at responding (especially modems).

    I just modified the default time outs to 2 seconds (2000) and added a thread.sleep(500) before it starts to read in the data for good measure. It works flawlessly if you can live with the fact of stripping return codes out of resulting read input (one quick regexp fixed that).

    Thanks again for everyones help on this oddity.
    Thoughtful QA, Properly Scoped Architecture, Following Best Practices: Pay Now or Pay 100 x Later.
    • Marked as answer by Avitar.Net Thursday, July 3, 2008 4:39 PM
    Thursday, July 3, 2008 4:39 PM

All replies

  •  however that seems to be more for an interactive application (an unwanted complexity i would like to avoid)

    I think Hyperterminal is an interactive API by default. How do you expect it to function if there is no feedback from the other end? If you don't care about any response, then simply declare your application a success as soon as you have debugged it:)


    Also if you are trying to use serial ports to communicate between two applications on the same machine or even across LAN then I should say it is a rather exotic idea. There are much simpler ways.


    Nobugz said:
    http://forums.msdn.microsoft.com/en-US/netfxbcl/thread/43d97669-1d92-46a0-9a08-f2709f40174d/

    PortChat said:
    http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.aspx


    AlexB
    Thursday, July 3, 2008 2:18 AM
  • I should clarify:

    The usage is planned for one command to be pushed by an automated application on the same box and then possibly received by the same application since I am just sniffing a single setting on internal modems and then storing the result. I was considering pushing the change with one app and then re-auditing the units at a later date in favor of expiditing the change on a large network --- although I agree it is arguable that all applications have user interaction (even if it is at an abstract level) this is about as far from it you can get.

    The two sources you mentioned are ones I have already Googled that make good use of an event based architecture for a continuous user interaction as opposed to a 20kb network agent doing a batch command.

    I am just looking for a way to retrieve the one line of output provided from the serial port without having to mess with listener events which in my opinion should only be used for on-going interactive user applications as opposed to batch operations. If someone could point me to an example of this or some insight about the objects construction that forces event based output I would appreciate it.
    Thursday, July 3, 2008 3:01 AM
  • WriteLine appends the string represented by the NewLine property which by default is a linefeed character (0x0a). Perhaps you need a carriage return (0x0d) or both instead. This applies for both the ReadLine and the WriteLine methods.

    Can you verify that the device is actually receiving the command and that it is actually responding?

    You can do this with a hardware sniffer, even on the same computer. Connect the RX line of another serial port in the same or another computer to the line you want to sniff (the TX line of the port to watch to sniff outgoing messages or the RX line to sniff incomming messages). The result can be checked with Hyperterminal connected to the sniff port. Or even better, something like RealTerm since it enables you to check the result in hex mode.

    Good luck.


    /Ruben RJJournal
    Thursday, July 3, 2008 5:50 AM
  • I checked to make sure i entered a return code, I actually found the answer to this late last night.. it's been quite some time since I worked with drivers and forgot they tend to be a little slow at responding (especially modems).

    I just modified the default time outs to 2 seconds (2000) and added a thread.sleep(500) before it starts to read in the data for good measure. It works flawlessly if you can live with the fact of stripping return codes out of resulting read input (one quick regexp fixed that).

    Thanks again for everyones help on this oddity.
    Thoughtful QA, Properly Scoped Architecture, Following Best Practices: Pay Now or Pay 100 x Later.
    • Marked as answer by Avitar.Net Thursday, July 3, 2008 4:39 PM
    Thursday, July 3, 2008 4:39 PM
  • Hi,

    I'm new in programming...

    I think I have the same problem. I tried to modified time outs to 2 seconds. It worked...but,after I made some improvements in my program (outside HyperterminalAdapter program), it doesn't work again. once i compiled the program, there was some errors. but, i think it's not about the program. it said about microsoft explorer. i forced to continue. then, after that, i run the program, it doesn't work anymore. Is the problem comming from my computer environment? I checked my serial port using Hyperterminal program in Communication, it works. But, in my program, it desn't. I checked my program, in the read methods, it alsways jumps to catch directly. Oh ya, I also tried the connection without connect serial port sometimes. It gives some effects?

    Any advise/comments will be appreciated.

    thanks

    Friday, April 16, 2010 11:57 AM
  • Hi Avitar, I am new to programming in C#... Can you send the complete coding of this... Thanks in advance

    • Proposed as answer by tuffcustomer Wednesday, February 2, 2011 4:35 PM
    Tuesday, January 11, 2011 5:25 AM
  • Hi Avitar.Net,

    Would you be able to share the code..please..cn_mohan2yahoo.com or cn_mohan2hotmail.com

    Thanks in advance.


    mona
    Thursday, March 24, 2011 2:19 AM
  • Hello Avitar.Net,

      I am working on the similar kind of project. I have to automate the commands sent and store the results that show up in hyper terminal using .net.

      can you please share your code with me at raghava205@hotmail.com it will be of big help for me.

    Thanks in advance.

    raghava

    Tuesday, September 13, 2011 1:21 PM
  • Hi, its enable to work with hyperterminal and visual basic c# simultaneously.

    please, help us,we need your prompt reply.

    Saturday, March 3, 2012 10:18 AM
  • hi avitar..i want you to send me the whole code to this email: helal_assi94@hotmail.com ..thanks in advance

    Sunday, March 4, 2012 12:30 PM
  • Hi there,

    Would you mind sending me the completed coding..

    my e-mail is siewling_leong@hotmail.com

    Really Thank you to send me in advance.

    smile

    Koalabearbear

    Thursday, March 28, 2013 6:06 AM
  • Do you mind sharing your codes? I'm actually new in this C# and hyperterminal and would be pleased if you could share your good work. Thank you, if you don't mind, my email is car-min@live.com. Really appreciate it if you could help.

    Wednesday, November 20, 2013 4:11 AM
  • Hi there,

    Can you send me the completed code

    my e-mail is alex-max22@hotmail.com

    Thanks, mate

    Thursday, April 23, 2015 9:09 AM
  • hi

    I m working similar programming and i m new for implement this kind of programming so please send me a full code of this project at wilmanpatel@live.com

    thanks a lot.


    Wilman


    • Edited by wilman12 Wednesday, May 20, 2015 12:08 PM
    Wednesday, May 20, 2015 12:07 PM
  • I am new to Serial port and hyper terminal emulation.

    Appreciate if you could send me the complete code to cbeeshow@gmail.com.

    regards

    Carmel


    Carmel Beeshow MCSD

    Monday, April 4, 2016 8:39 AM