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

Morse Code

, 26 Apr 2007
Rate this:
Please Sign up or sign in to vote.
An article on Changing Text to Morse Code in Basic

Screenshot - Morse_Code1.jpg

Introduction

Morse Code: it consists of a combination of dots and dashes in standardized sequences. I set out to write a application in VB.NET that would read Morse Code and translate it into Text.

Background

The project turned out a lot more work than I thought it would be. I ended up with a project which was different than the one I had originally intended. This new project will help you learn Morse Code and can be used as a beacon.

The biggest problem I had was using a sound control in VB.NET. Although there is DirectX sound for VB.NET, I considered it far too complex. So I ended up with the beep function.

There are two important things that it has to do. The beep has to be able to change frequency and duration. The first thing that you have to do is enable the beep. Using just Beep() results in no adjustment in duration or frequency. This line of code just gets us started. In the same sub we start the timer for the clock in a textbox to be displayed in the toolbar. We also set the variable freq from the Trackbar1.Value.

Public Declare Function Beep Lib "kernel32" Alias "Beep" (
    ByVal dwFreq As Integer, ByVal dwDuration As Integer) As Integer

    Sub Main()
     
        Timer1.Start()
        freq = TrackBar1.Value

    End Sub

Morse code is all about timing. The timing is set like this.

Screenshot - Morse_Code2.jpg

Points of Interest

After the user has typed the string in Textbox1 it's time to change it over to Morse code. In order to do this we have to take one character at a time. We also have to find out the pitch and the words per minute. The word "PAIRS" is example which we use to figure out five words per minute. Each element is equal to a dot, and a dash is three dots. So by using the formula we come up with a rounded average for words per min. The user sets the numericupdown.value to the words per minute.

freq = TrackBar1.Value
        dit = 60000 / (NumericUpDown1.Value * 50)
        dah = dit * 3

Let's take a look at handling the string from the textbox. What I did was run it through a For Next Loop so I could check one character at a time.

OK, here comes an interesting part. In the formula we came up with, a timing element in thousandths of a second. There is a total of 50 elements or dots in the word "pairs". So we multiply the value set by the user and then divide. Let's say one of the characters was C; in Morse code that would be: dash dot dash dot. In between the dots and dashes is a time element. That's where we want to have the program stop and do nothing. We need to hear the difference between the dots and dashes. So how do we do that? Here's the answer.

'Char "C"
    ElseIf key = "C" Then
        Beep(freq, dah)
        Thread.Sleep(dit)
        Beep(freq, dit)
        Thread.Sleep(dit)
        Beep(freq, dah)
        Thread.Sleep(dit)
        Beep(freq, dit)
        Thread.Sleep(dit * 3)

So we tell the thread to go to sleep. How long the length of the dot or Dit is as you see above.

As you can see from the code above, a character can have up to ten lines of code or more. I am doing this in VB.NET so speed is not on my side. That's where we break out with BackGroundWorker. This is where we pick at the string from Textbox1. Without using the BackgroundWorker The program would come to a stop.

Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, 
    ByVal e As System.ComponentModel.DoWorkEventArgs) Handles 
    BackgroundWorker1.DoWork
    'Check the string in textbox2 one char at a time and change to Morse code
        len = Microsoft.VisualBasic.Len(batch)
        'Check Batch for sending and send
'len is the length of the string
        For A = 1 To len
            Me.BackgroundWorker1.ReportProgress(A)
            key = Microsoft.VisualBasic.Mid(batch, A, 1)
            'Put string together for textbox3 "qso"
            qso = qso + key
            'Char " "
            If key = " " Then
                Thread.Sleep(dit * 7)
            End If 

In the demo you have two textboxes. In the first one, type your text and then set your pitch and words per minute. If anyone would like the full blown out program drop me an e-mail and I will gladly send it to you with full code to play with. This was a fun project and that's the kind we like.

Coming up

I am presently working on a keyer to work of off the sound card DB9 or USB Ports. Maybe all three.

License

This article, along with any associated source code and files, is licensed under The Common Development and Distribution License (CDDL)

About the Author

Dennis W R

United States United States
You might say that I am a throw back from the old days. I started out with the Vic 20. THE WHAT!! Ya that’s right the Vic 20 and the C64 the Timex Plus 64 and so on. So to be able to work with vb basic 2005 express. Is like a kid in a candy shop. When it stops being fun and more like work I hope I have a paycheck. Don't look for any wallpaper here. Just the school of hard knocks
 
Dennis

Comments and Discussions

 
QuestionMorse Code Pinmembern0nuf1-Jul-12 17:12 
GeneralWorking on making a trainer for CW. Pinmemberveltrox6-Jul-10 16:14 
Generalconsole.beep = lag Pinmemberjoeostrander24-May-10 10:43 
GeneralWindows x64 Editions Pinmemberhswear318-Mar-10 17:52 
GeneralMods for a pocket pc Pinmemberitake_sal23-Oct-07 7:46 
GeneralSource Code PinmemberXChronos25-Apr-07 7:24 
GeneralRe: Source Code PinmemberDennis W R25-Apr-07 10:46 
I sent you the sorce code i will update the article
 
That's my View and I'm Stuck with it.

GeneralSource PinmemberMichael J. Collins24-Apr-07 7:51 
GeneralWell Done! Pinmemberpatrickdonohue20-Apr-07 10:03 

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
Web04 | 2.8.140709.1 | Last Updated 26 Apr 2007
Article Copyright 2007 by Dennis W R
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid