Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / C#

Mobile Development – Simple Serial Communication Application

Rate me:
Please Sign up or sign in to vote.
1.00/5 (1 vote)
11 Mar 2011CPOL1 min read 15K   8   4
A simple application to connect to a serial port

Here is a simple application to connect to a serial port. The idea is based on the need of having an application to send demo print files to a virtual comm port connected to a Bluetooth printer.

You can enter and send texts to or send a whole file to the port. Additionally, you are able to send ASCII codes by using the \xAB syntax, where AB is the hex code of the byte you would like to send.

There is nothing special with the code except the hex decoding/encoding and the possibility to send a file.

Here is a short sample how the SendFile is implemented:

C#
private void sendFile(string filename)
{
    try
    {
        updateTxtRcv("Starting sendFile(" + filename + ")");

        System.IO.StreamReader sr = new System.IO.StreamReader(filename);
        int r;
        int blockSize = 4096;
        char[] buf = new char[blockSize];
        //how many blocks to send?
        System.IO.FileInfo fi = new System.IO.FileInfo(filename);
        long lBlocks = (long)(fi.Length / blockSize);
        if (lBlocks == 0)
            lBlocks = 1;
        updateTxtRcv("Need to send " + lBlocks.ToString() + " blocks...");
        long lBlockNr = 1;
        do
        {
            updateTxtRcv("Sending block " + lBlockNr.ToString() + " ...");
            r = sr.Read(buf, 0, blockSize); //r = number of bytes read
            comport.Write(buf, 0, r);
        }
        while (r!=-1 && !sr.EndOfStream);
        sr.Close();
        updateTxtRcv("Finished sendFile(" + filename + ")");
    }
    catch (Exception x)
    {
        updateTxtRcv("sendFile exception:" + x.Message);
        System.Diagnostics.Debug.WriteLine(x.Message);
    }
}

The application persists the chosen settings in the Registry. I had to implement classes to convert the SerialPort Parity, Handshake, Stopbits, etc. settings between typed and integer values. Unfortunately, the enums for these properties are not defined on the base of integer. For example, I had to code this class to enable save/restore of the Handshake property:

C#
public static class myHandshake
{
    public static string[] handshakes = { "None", "XOnXOff", 
           "RequestToSend", "RequestToSendXOnXOff" };
    public static Handshake ToHandshake(int i)
    {
        switch (i)
        {
            case 0: return Handshake.None;
            case 1: return Handshake.XOnXOff;
            case 2: return Handshake.RequestToSend;
            case 3: return Handshake.RequestToSendXOnXOff;
        }
        return Handshake.None;
    }
    public static Handshake ToHandshake(string text)
    {
        if (text.Equals("None", StringComparison.OrdinalIgnoreCase))
            return Handshake.None;
        if (text.Equals("XOnXOff", StringComparison.OrdinalIgnoreCase))
            return Handshake.XOnXOff;
        if (text.Equals("RequestToSend", StringComparison.OrdinalIgnoreCase))
            return Handshake.RequestToSend;
        if (text.Equals("RequestToSendXOnXOff", StringComparison.OrdinalIgnoreCase))
            return Handshake.RequestToSendXOnXOff;
        return Handshake.None;
    }
    public static int ToInt(Handshake hs)
    {
        switch (hs)
        {
            case Handshake.None: return 0;
            case Handshake.XOnXOff: return 1;
            case Handshake.RequestToSend: return 2;
            case Handshake.RequestToSendXOnXOff: return 3;
        }
        return 0;
    }
    public static string ToString(Handshake hs)
    {
        switch (hs)
        {
            case Handshake.None: return "None";
            case Handshake.XOnXOff: return "XOnXOff";
            case Handshake.RequestToSend: return "RequestToSend";
            case Handshake.RequestToSendXOnXOff: return "RequestToSendXOnXOff";
        }
        return "None";
    }
    public static string ToString(int i)
    {
        switch (i)
        {
            case 0: return "None";
            case 1: return "XOnXOff";
            case 2: return "RequestToSend";
            case 3: return "RequestToSendXOnXOff";
        }
        return "None";
    }
}//class myHandshake

This would have been a lot easier if the Handshake enum was based on integer.

By the way, using StopBits.None is not supported: “This value is not supported. Setting the System.IO.Ports.SerialPort.StopBits property to System.IO.Ports.StopBits.None raises an System.ArgumentOutOfRangeException.” (See StopBits enumeration.)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionSerial ports with MS BT Pin
dglnz17-Nov-12 17:20
dglnz17-Nov-12 17:20 
AnswerRe: Serial ports with MS BT Pin
hjgode17-Nov-12 20:05
hjgode17-Nov-12 20:05 
GeneralRe: Serial ports with MS BT Pin
dglnz17-Nov-12 23:02
dglnz17-Nov-12 23:02 
GeneralRe: Serial ports with MS BT Pin
hjgode21-Nov-12 4:50
hjgode21-Nov-12 4:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.