Click here to Skip to main content
14,430,522 members

Mobile Development – Simple Serial Communication Application

Rate this:
1.00 (1 vote)
Please Sign up or sign in to vote.
1.00 (1 vote)
11 Mar 2011CPOL
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.

Image 1 Image 2 Image 3

Here is a short sample how the SendFile is implemented:

private void sendFile(string filename)
        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;
            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);
        updateTxtRcv("Finished sendFile(" + filename + ")");
    catch (Exception x)
        updateTxtRcv("sendFile exception:" + 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:

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


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


About the Author

Germany Germany
No Biography provided

Comments and Discussions

QuestionSerial ports with MS BT Pin
dglnz17-Nov-12 18:20
Memberdglnz17-Nov-12 18:20 
AnswerRe: Serial ports with MS BT Pin
hjgode17-Nov-12 21:05
Memberhjgode17-Nov-12 21:05 
Hello Dave

you can get the same results using MS BT stack. You can either use BT socket communication directly or by creating a virtual COM port.

There are other articles and posts describing the use of RFCOMM. You can go the (hard) way of MS API or use wrappers like InTheHand (

GeneralRe: Serial ports with MS BT Pin
dglnz18-Nov-12 0:02
Memberdglnz18-Nov-12 0:02 
GeneralRe: Serial ports with MS BT Pin
hjgode21-Nov-12 5:50
Memberhjgode21-Nov-12 5: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.

Technical Blog
Posted 11 Mar 2011

Tagged as


8 bookmarked