Morse Code






4.08/5 (12 votes)
An article on Changing Text to Morse Code in Basic
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.
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.