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

Morse Code

By , 26 Apr 2007
 

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
Member
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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionMorse Codemembern0nuf1 Jul '12 - 17:12 
Hi Dennis.
 
Do you have this program in C#? I am thinking of writing a CW (morse code) practice program where the user can use a word list with the ability to set the char speed, space speed, and speed of the space character between words. You can email n0nuf2@gmail.com if you have any more information.
 
Thanks,
-Scott
GeneralWorking on making a trainer for CW.memberveltrox6 Jul '10 - 16:14 
I'm having some programming issues which I see you've already worked out.
 
I'm making a step by step trainer and see that many of the issues I'm having you've managed to work around with better results.
 
Can you please email me your source code?
 
Also, for some reason when I have letters generated and played with tone, I can't type the 'answers' to the key in the textbox until the code finishes playing through, which sort of defeats the purpose. Is there any way I can keep the ability to type in text as the morse is being audibly played?
 
Thank you!
 
- Jordan
Veltrox@gmail.com
Generalconsole.beep = lagmemberjoeostrander24 May '10 - 10:43 
I wrote a similar one... but had to abandon the Console.Beep() method. I was excited to see this project, but sad to see it too has the same issue as mine.
 
I found when I recorded the waveform and analyzed it, that the timing after the dah's was actually more than 1 dit in length (kind of a big deal for actual morse code). I tried several methods to get around this... using separate threads, failed attempts at directx. No matter what I did, I couldn't guarantee the exact timing.
 
Ultimately I'm stuck with using wav files embedded as resources in the project. The timing works well... but it sure whould be nice to use something else.
 
If anyone knows why console.beep does this... or a way around it... I'd love to hear why!
GeneralWindows x64 Editionsmemberhswear318 Mar '10 - 17:52 
Just in case anyone tries this code on Windows XP or Vista 64-Bit editions, you should know that it won't work. The Beep API function is not supported. Frown | :(
Herbert N Swearengen III

GeneralMods for a pocket pcmemberitake_sal23 Oct '07 - 7:46 
Mods for a pocket pc
 
Hello,
 
first, it's a great code.
 
Now I'm trying to adapt for my PocketPC, but it doesn't run.
 
I think due DLL library... Does someone know which DLL must I use?
 
Thanks a lot in advance. -itake-
 
-itake-
GeneralSource CodememberXChronos25 Apr '07 - 7:24 
Please share the source code. It's a great work.
GeneralRe: Source CodememberDennis 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.

GeneralSourcememberMichael J. Collins24 Apr '07 - 7:51 
I started something like this and never got back to it. I'd love to have to "full blown" app and source. I can't find your email address though.

 
Michael J. Collins
Web Application Programmer

GeneralWell Done!memberpatrickdonohue20 Apr '07 - 10:03 
Is the source code available by chance?Smile | :)
 
p.donohue

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 26 Apr 2007
Article Copyright 2007 by Dennis W R
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid