Serial Communication
Serial Communication
Serial Communication
Serial Communication
Serial Communication
Serial Communication Serial Communication Serial Communication Serial Communication Serial Communication Serial Communication Serial Communication Serial Communication
Serial Communication Serial Communication
Serial Communication
Go Back  Xtreme Visual Basic Talk > > > Serial Communication


Reply
 
Thread Tools Display Modes
  #1  
Old 03-30-2009, 06:57 AM
qtommer qtommer is offline
Freshman
 
Join Date: Feb 2009
Posts: 27
Default Serial Communication


hey there
im actually trying to communicate from my pc to a microcontroller using the MSComm Control.Does anyone know where I can get any GOOD tutorials on this topic for beginners like me?
I only want to send to the microcontroller and NOT receive ..

i have tried to learn the MSComm
using the program on the end of this website and used a loopback thingy where i shorted pins 2 and pins 3 (transmit and receive) on the serial cable using a jumper wire but the program didnt work. am i doing it correctly? (i just shorted pin 2 and 3 using 1 single jumper wire)

apart from the aforementioned website, are there any other tutorials on the web which offer samples or instructions on how to test out serial communication using MSComm?

thanks
Reply With Quote
  #2  
Old 03-30-2009, 12:47 PM
Cerian Knight's Avatar
Cerian KnightSerial Communication Cerian Knight is offline
Polymath (in disciplina)

Super Moderator
* Expert *
 
Join Date: May 2004
Location: Michigan
Posts: 4,199
Default

Shorting 2-3 should work. I noticed that the example tutorial code you referenced did not set MSComm1.RThreshold = 1 so that the On_Comm Event code they listed will NOT execute... tsk, tsk, as this is necessary to test your loopback. Give that a try first (in Form_Load), then follow this through till you understand it:

If you have to read data (for loopback), using MSComm On_Comm is easy once you understand the idea that all received data does not come in simultaneously. The On_Comm event may be generated only once for all data recieved OR once for each byte of data recieved OR any combination thereof. Normally you concatenate a Static (or Form/Module level) variable to buffer the incoming data (instead of a TextBox) and parse it each time new data is added to see if it contains a complete packet. Once you act on a complete packet, you can remove it from your Static buffer.

There are lots of code examples on this site using MSComm, as well.
__________________
I got all the answers wrong on the GLAT, apparently even #9 (where I put a period in the middle of the box and labeled it 'singularity ripe for rapid inflation').

Last edited by Cerian Knight; 03-30-2009 at 01:07 PM.
Reply With Quote
  #3  
Old 04-01-2009, 06:51 AM
qtommer qtommer is offline
Freshman
 
Join Date: Feb 2009
Posts: 27
Default

Thank you Cerian Knight for your insightful advice. I did exactly as u told me and my program is working fine and is as follows:

Private Sub Form_Load()
MSComm1.CommPort = 1
MSComm1.Settings = "9600,N,8,1"
MSComm1.PortOpen = True
MSComm1.InputLen = 0
MSComm1.RThreshold = 1
End Sub

Private Sub Command1_Click()
Dim stringsent As String
Dim stringrcv As String


stringsent = Text1.Text
MSComm1.Output = stringsent

stringrcv = MSComm1.Input
Label3.Caption = stringrcv


End Sub



Id like to know how i could convert my textstring to hexcode form to be sent to my microcontroller.

i read from somewhere that its something like this.?

stringsent = Text1.Text
MSComm1.Output = Chr( "$" & stringsent)


the information on that web wasnt really clear..


if it helps, im actually doing a LED Matrix Display and im using VB as the GUI for the user to input text and the microcontroller will output it on the Display Board.

Thanks So Much
Reply With Quote
  #4  
Old 04-01-2009, 11:33 AM
qtommer qtommer is offline
Freshman
 
Join Date: Feb 2009
Posts: 27
Default

i was doing some research on the net to get the functions on how to convert to hexcode and i got this..would this be correct? (The part i added is in red)

Private Sub Form_Load()
MSComm1.CommPort = 1
MSComm1.Settings = "9600,N,8,1"
MSComm1.PortOpen = True
MSComm1.InputLen = 0
MSComm1.RThreshold = 1
End Sub

Private Sub Command1_Click()
Dim stringtyped As String

Dim stringsent As String


stringtyped = Text1.Text


Dim loop as Integer
Dim stringlength as Integer

stringtyped=Text1.Text

stringtyped=Trim(stringtyped)
stringlength= len(stringtyped)

For loop = 1 to stringlength
stringsent= stringsent & "&H" & Hex(Asc(mid(stringtyped,loop,1)))

MSComm1.Output=stringsent




End Sub


would this be correct?
will i send out the string input by the user to the serial port in hexcodes?
if not could you kindly rectify where i went wrong?
(i want to send the string as a group of hexcodes to the microcontroller)

thanks again for your invaluable time
Reply With Quote
  #5  
Old 04-01-2009, 12:57 PM
Cerian Knight's Avatar
Cerian KnightSerial Communication Cerian Knight is offline
Polymath (in disciplina)

Super Moderator
* Expert *
 
Join Date: May 2004
Location: Michigan
Posts: 4,199
Default

You don't actually send Hex code, but the documentation is usually in hex.
For example, if the documentation says to send 1Bh + 41h, then you send decimal 27 and 65 as an ASCII string which is easy: 'MSComm1.Output = Chr$(27) & "A"'.
__________________
I got all the answers wrong on the GLAT, apparently even #9 (where I put a period in the middle of the box and labeled it 'singularity ripe for rapid inflation').
Reply With Quote
  #6  
Old 04-03-2009, 06:30 AM
qtommer qtommer is offline
Freshman
 
Join Date: Feb 2009
Posts: 27
Default

i see.
if i want to communicate with my microcontroller, do i have to compile my VBcode as a HEXCODE? If yes, how can i do so? If that is not the way then what method should i employ?
thanks
Reply With Quote
  #7  
Old 04-03-2009, 10:12 AM
tpaksu's Avatar
tpaksu tpaksu is offline
Centurion
 
Join Date: Dec 2008
Location: Istanbul
Posts: 173
Default

No you don't need that.

mscomm1.output = chr(35)

is the same with

mscomm1.output = "5"

by the way, you can watch what's happening on the serial cable by using a sniffer like Free Serial Port Monitor which i use and you can find it here:

http://www.hhdsoftware.com/sermon.html

It helps a lot.

---
Note : The software is not written by me and I'm not advertising it.I'm just using it and offering this to help someone who needs this.
__________________
Never send a human to do a machine's job. (Agent Smith :P)
Reply With Quote
  #8  
Old 04-03-2009, 10:20 AM
tpaksu's Avatar
tpaksu tpaksu is offline
Centurion
 
Join Date: Dec 2008
Location: Istanbul
Posts: 173
Default

here are some advices,

- You should use the MsComm1_OnComm event and MsComm1.Input to catch the data coming from mCU and if you want you can create your own buffer with a code like this (RecvDataBuffer is a collection which i use, you can use different types like string buffering too) :

Code:
Private Sub MSComm1_OnComm()
    Select Case MSComm1.CommEvent
        Case comEvReceive
            mydata = Asc(MSComm1.Input)
            RecvDataBuffer.Add mydata
    End Select
End Sub
- You should use InputLength property set to "1" if you want your code to catch the inputs byte by byte.

- First of all, You HAVE TO open your port with MSComm.PortOpen like this:

Code:
If MSComm1.PortOpen = False Then
      MSComm1.commPort = 1    'Select COM1 for data transmission
      MSComm1.PortOpen = True  'Open the port.
End If
- And the last thing about this subject, you need to use MsComm1.output when you need to send anything over the serial line.

You can use:

MsComm1.Output = "tpaksu" ' sends "tpaksu" over the line
MsComm1.Output = 5 ' sends chr(35) (character "5") over the line
MsComm1.Output = chr(35) ' same with the above line

These are the basics of Serial Communication with VB6.

Hope it helps.
__________________
Never send a human to do a machine's job. (Agent Smith :P)
Reply With Quote
  #9  
Old 04-03-2009, 11:26 PM
qtommer qtommer is offline
Freshman
 
Join Date: Feb 2009
Posts: 27
Default

Thank you so much tpaksu=)
its really a great help to me
i'll be giving it a go with my microcontroller soon.

oh and thanks for the software recommendation too!!!
Reply With Quote
  #10  
Old 04-04-2009, 04:47 PM
tpaksu's Avatar
tpaksu tpaksu is offline
Centurion
 
Join Date: Dec 2008
Location: Istanbul
Posts: 173
Default

your welcome.
__________________
Never send a human to do a machine's job. (Agent Smith :P)
Reply With Quote
  #11  
Old 04-06-2009, 05:57 AM
qtommer qtommer is offline
Freshman
 
Join Date: Feb 2009
Posts: 27
Default

Quote:
Private Sub Form_Load()
MSComm1.CommPort = 1
MSComm1.Settings = "9600,N,8,1"
MSComm1.PortOpen = True
MSComm1.InputLen = 0
MSComm1.RThreshold = 1
End Sub

Private Sub Command1_Click()
Dim stringsent As String
Dim stringrcv As String


stringsent = Text1.Text
MSComm1.Output = stringsent

stringrcv = MSComm1.Input
Label3.Caption = stringrcv


End Sub

I have successfully been able to send out strings through my loop-backed serial port using the above method but the above coding only supports the user keying in data through one text box only..

I would like now to send out strings from multiple text boxes to my serial port. THIS STRINGS ARE TO BE SENT TO AN ATMEL MICROCONTROLLER

The coding that i came up with is as follows:


Private Sub Form_Load()
MSComm1.CommPort = 1
MSComm1.PortOpen = True
MSComm1.Settings = "9600,N,8,1"
MSComm1.RThreshold = 1


End Sub



Private Sub Send1_Click()
Dim string1 As String
string1 = Text1.Text
MSComm1.Output = string1
End Sub

Private Sub Send2_Click()
Dim string2 As String
string2 = Text2.Text
MSComm1.Output = string2
End Sub

Private Sub Send3_Click()
Dim string3 As String
string3 = Text3.Text
MSComm1.Output = string3
End Sub

Private Sub Send4_Click()
Dim string4 As String
string4 = Text4.Text
MSComm1.Output = string4
End Sub


Is the method that i used correct? If not how can i achieve sending out strings from multiple textboxes to the microcontroller..

Thanks
Reply With Quote
  #12  
Old 04-06-2009, 07:57 AM
qtommer qtommer is offline
Freshman
 
Join Date: Feb 2009
Posts: 27
Default

one more thing that i forgot to mention
From what i understand, microcontrollers receive data in a serial stream of hexcodes.

i want to know what method i should employ in my program so that the strings that are sent to the serial port from the text boxes can be isolated back at the microcontroller:

Example(user enters into VB program):
Text Box 1: Hello how are you?
Text Box 2: It depends on the weather
Text Box 3: Oh really?
Text Box 4: It also depends on the weather

I want to code these strings to the serial port connected to my microcontroller in such a way that so that the MCU knows that
"Hello How are you" is a separate string from " It depends on the weather" and so on..

What method should i employ on my VB part?

thanks
Reply With Quote
  #13  
Old 04-06-2009, 09:20 AM
mkaras's Avatar
mkarasSerial Communication mkaras is offline
Ultimate Contributor

Retired Leader
* Expert *
 
Join Date: Mar 2004
Location: Beaverton, OR
Posts: 1,874
Default

This is an common issue and it turns out that there are characters defined which can be deployed specifically for this purpose. Append the text string constant vbCr to the end of each string before you send it out. Like this:

string = TextBox.Text
string = string & vbCr
MSComm1.Output = string

Now when the microcontroller is getting bytes in through its UART (which have been sent from the PC) it will get the bytes of the string and then finally it will see a byte arrive that has a value of 0x0D. This will indicate the end of the string and corresponds to the vbCr value.

Some times folks will elect to delimit data using the vbCrLf string. In such case the microcontroller will be seeing two bytes come between strings that have values 0x0D followed by 0x0A.

- mkaras
Reply With Quote
  #14  
Old 04-06-2009, 10:18 AM
qtommer qtommer is offline
Freshman
 
Join Date: Feb 2009
Posts: 27
Default

thanks mkaras =)

so if i want to send out strings using four text boxes would the code be like this?

Private Sub Send1_Click()
Dim string1 As String
string1 = Text1.Text
string1=string1 & vbCr
MSComm1.Output = string1
End Sub

Private Sub Send2_Click()
Dim string2 As String
string2 = Text2.Text
string2=string2 & vbCr
MSComm1.Output = string2
End Sub

Private Sub Send3_Click()
Dim string3 As String
string3 = Text3.Text
string3=string3 & vbCr
MSComm1.Output = string3
End Sub

Private Sub Send4_Click()
Dim string4 As String
string4 = Text4.Text
string4=string4 & vbCr
MSComm1.Output = string4
End Sub
Reply With Quote
  #15  
Old 04-06-2009, 10:30 AM
tpaksu's Avatar
tpaksu tpaksu is offline
Centurion
 
Join Date: Dec 2008
Location: Istanbul
Posts: 173
Default

0x00 may be a choice too because its the original string delimiter in mcu's.
__________________
Never send a human to do a machine's job. (Agent Smith :P)
Reply With Quote
  #16  
Old 04-06-2009, 10:35 PM
mkaras's Avatar
mkarasSerial Communication mkaras is offline
Ultimate Contributor

Retired Leader
* Expert *
 
Join Date: Mar 2004
Location: Beaverton, OR
Posts: 1,874
Default

qtommer:

You have the idea. It is possible to be a small bit more concise in the coding to do this. I know you followed my example (which I did as I showed to make it fully clear to you).

You could write it as:
Code:
Private Sub Send1_Click()
    Dim string1 As String
    string1 = Text1.Text & vbCr
    MSComm1.Output = string1
End Sub
And you could also just as easily see this work too:
Code:
Private Sub Send1_Click()
    MSComm1.Output = Text1.Text & vbCr
End Sub
Just to follow up with what tpaksu said about using 0x00 as the delimiter in between the strings. I am not so sure that I agree that using the zero byte value is the "original" string delimiter in MCUs. There have been many many varied schemes dreamed up for delimiting strings. I myself in some 35 years of working with this kind of thing have probably encountered several dozen schemes. One thing that can be said for the zero (0x00) byte value is that it is the standard string terminator used by C language implementations, whether that be on an MCU or other computer being a mainframe, mini computer or microprocessor system. Bottom line is that if a delimiter (or separator) character is to be used you have to make sure that the strings themselves do not have the delimiter included inside the string. If they did the other end that has to break the strings apart would get confused as to where the string divisions actually were located. Other than that the delimiter could be any byte you wanted.

The delimiter could also be devised along any one of the following...

- Use the first character as a marker and accept text following this as the string content up till you see that first character marker again. An example could be "This is a string delimited by double quotes" .

- You could decide to never have strings longer than 999 bytes and then take the string length and convert it to a three digit readable character count and pre-pend it to the text you send. An example could be: 038I'm sending a thirty eight byte string .

- You could determine from making a study of the text that you are sending that all of the character code values being sent turn out to be standard ASCII codes that lie in values between 0 and 127. This means that all the byte values you would send would only be 7 bits. You could delimit the text by taking advantage of this and mark the final character in the string by setting its upper bit (ie. add 128 to the last byte in the string). In this latter case you would certainly have to ensure that you have the MSComm control to support a UART format of 8-bit characters and not 7.

In all of these cases the receiving end simply has to comprehend the scheme in use and decode / deblock the incoming serial data stream in the appropriate manner.

- mkaras
Reply With Quote
  #17  
Old 04-08-2009, 12:42 AM
qtommer qtommer is offline
Freshman
 
Join Date: Feb 2009
Posts: 27
Default

thanks mkaras for the methods on delimiting=). I will try them out as soon as I get my microcontroller back. In the mean time, is there a method where I can test my delimiting on my pc by using a loopback plug?

im having a problem with my program now. im sending and receiving strings using a loopback method using the following code:

Private Sub Form_Load()
MSComm1.CommPort = 1
MSComm1.Settings = "9600,N,8,1"
MSComm1.PortOpen = True
MSComm1.InputLen = 0

MSComm1.RThreshold = 1


End Sub

Private Sub Command1_Click()
Dim stringsent As String
Dim stringrcv As String

stringsent = Text1.Text
MSComm1.Output = stringsent & vbCr

stringrcv = MSComm1.Input
Text2.Text = stringrcv


End Sub


The problem is if I enter a string less than 14 bytes I have to press the command button TWICE only to receive input from the serial port in my textbox. When i press it once,no input is received (textbox is blank).When i press it twice, then only the whole string appears (only for strings 14 bytes and below)

however,if i enter a string 15 bytes and above, I can send and receive it back with one click of the button. However, i will receive the input from the port in a wierd manner :
Let's say i send out the string:
qtommerislearning (16bytes)
When i press the command button once,i will receive only the first 8 bytes:
qtommeri (8bytes)
when i press the command button for the second time, i will receive
slearning|qtommeri (the remaining of the string prepended to the first 8 bytes)


I understand that the | character denotes the vbCr character but would like to know how come my string is all jumbled up.
Why cant i receive a 16 byte string with its characters in order straight away on a click of the button.
Is that a normal occurence when im communicating on the same pc using a loopback?
or is it some buffer settings i have to adjust.?

thank you so much for your invaluable time..

*wow 35 years in this industry..am i ever so privileged for your help* =)
Reply With Quote
  #18  
Old 04-08-2009, 03:27 PM
tpaksu's Avatar
tpaksu tpaksu is offline
Centurion
 
Join Date: Dec 2008
Location: Istanbul
Posts: 173
Default

yes mkaras is right. The 0x00 is the standart for the C implementations. Thanks for mentioning this.

and qtommer, try this:

Code:
dim strData as string
strData = Text1.Text
for i=1 to len(strData)
mscomm1.output = mid$(strData,i,1)
next i
mscomm1.output = vbCr
and tell us what happened.
__________________
Never send a human to do a machine's job. (Agent Smith :P)
Reply With Quote
  #19  
Old 04-09-2009, 12:49 AM
parpat14 parpat14 is offline
Newcomer
 
Join Date: Apr 2009
Posts: 1
Exclamation Want to receive data

Hey friends,

i am working on project in which i want to receive data from Microcontroller....i had tried many codes but none of them run....so please send me code from which i can receive data on PC in VB6.0......please send me as soon as you can as my project submission is on 15th Feb....

my email id is:- parpat14@gmail.com
Reply With Quote
  #20  
Old 04-09-2009, 06:36 AM
qtommer qtommer is offline
Freshman
 
Join Date: Feb 2009
Posts: 27
Default

the same problem occurs:
The form in my program lets the user enter a message into a textbox followed by the desired time of display on two other textboxes (hour and minute respectively)

All this comes under 1 slot and the program consists of a total of 4 slots.
(12 textboxes all together)

All these strings are what i want to send to my microcontroller but for now im using a loopback plug and displaying the received data from the serial port into a label (Label12)

The code that ive adjusted accordingly is as follows:

Private Sub cmdTransmit_Click()
Dim string1, string2, string3, string4, string5 As String
Dim hour1, hour2, hour3, hour4 As String
Dim min1, min2, min3, min4 As String
Dim stringrcv As String
Dim i As Integer


hour1 = txtHr1.Text & vbCr
hour2 = txtHr2.Text & vbCr
hour3 = txtHr3.Text & vbCr
hour4 = txtHr4.Text & vbCr

min1 = txtMin1.Text & vbCr
min2 = txtMin2.Text & vbCr
min3 = txtMin3.Text & vbCr
min4 = txtMin4.Text & vbCr


'ive adjusted the following accordingly

string1 = txtMsg1.Text
For i = 1 To Len(string1)
MSComm1.Output = Mid$(string1, i, 1)
Next i
MSComm1.Output = vbCr & hour1 & min1

string2 = txtMsg2.Text
For i = 1 To Len(string2)
MSComm1.Output = Mid$(string2, i, 1)
Next i
MSComm1.Output = vbCr & hour2 & min2

string3 = txtMsg3.Text
For i = 1 To Len(string3)
MSComm1.Output = Mid$(string3, i, 1)
Next i
MSComm1.Output = vbCr & hour3 & min3

string4 = txtMsg4.Text
For i = 1 To Len(string4)
MSComm1.Output = Mid$(string4, i, 1)
Next i
MSComm1.Output = vbCr & hour4 & min4


stringrcv = MSComm1.Input
Label12.Caption = stringrcv

End Sub



This is what happens when i enter the following input,

string1=qtommerqtommer ;hour1=12 ; min1=12
string2=hellohellohello ; hour2=12 ;min2=12
string3=qwertyqwerty ;hour3=12 ;min3=12
string4=keyboardkeyboard ; hour4=12 ; min4 =12

ON THE FIRST CLICK OF THE COMMAND BUTTON TO TRANSMIT DATA:
LABEL 1 DISPLAYS THE FOLLOWING

qtommerqtommer
12
12
hellohellohello
12
12
qwertyqwerty
12
12
keyboardke



ON THE SECOND CLICK OF THE COMMAND BUTTON, ONCE AGAIN THE REMAINING PART OF THE STRINGS ARE PREPENDED TO THE BEGINNING AND ARE THE FINAL OUTCOME IS:

yboard
12
12

qtommerqtommer
12
12
hellohellohello
12
12
qwertyqwerty
12
12
keyboardke


Is this a normal occurrence? it seems that on one click, the buffer can only transmit a maximum amount of bytes and requires a second click to transmit the remaining bytes.
is it anything to do with the buffer settings?


thanks so much..
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Serial Communication
Serial Communication
Serial Communication Serial Communication
Serial Communication
Serial Communication
Serial Communication Serial Communication Serial Communication Serial Communication Serial Communication Serial Communication Serial Communication
Serial Communication
Serial Communication
 
Serial Communication
Serial Communication
 
-->