Click here to Skip to main content
Click here to Skip to main content

Mobile Development – Simple serial communication application

, 11 Mar 2011
Rate this:
Please Sign up or sign in to vote.
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 [...]

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:

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:

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

Source code and binary located at code.google.com

License

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

Share

About the Author

hjgode

Germany Germany
No Biography provided

Comments and Discussions

 
QuestionSerial ports with MS BT Pinmemberdglnz17-Nov-12 17:20 
AnswerRe: Serial ports with MS BT Pinmemberhjgode17-Nov-12 20:05 
GeneralRe: Serial ports with MS BT Pinmemberdglnz17-Nov-12 23:02 
GeneralRe: Serial ports with MS BT Pinmemberhjgode21-Nov-12 4:50 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 11 Mar 2011
Article Copyright 2011 by hjgode
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid