cwteoh
asked on
Monitoring Serial Port / Receiving data from Serial Port
This could be my first vb6 programming developing an application communicate with hardware. I have a controller generating 1 - 2 line(s) of data from a machine send to a ordinary pc through a common serial port (example: com port 1). I am developing this application to collect the data every time the cotroller send through the serial port. Any idea what I am telling you experts worldwide?? Any idea how to communicate with this serial port.
ASKER
hi vinnyd79,
refer to your first message, the example from microsoft website. is that mean the application will keep monitoring the comm port using do.. loop feature?
For second message, I am not really understand the language, it will better you explain how it work in that application.
By the way, do we need to configure which pin receive data and send data?
refer to your first message, the example from microsoft website. is that mean the application will keep monitoring the comm port using do.. loop feature?
For second message, I am not really understand the language, it will better you explain how it work in that application.
By the way, do we need to configure which pin receive data and send data?
Sure, here is the lowdown:
First you need to make certain that the terminal is configured for the correct Baud Rate, Number Of Stop Bits, Number Of Data Bits and Parity Bits. With this not set right, Baud rate mismatch, data mismatch or parity error can occur. You can set these parameters in the Form_Load event
Private Sub Form_Load()
Dim Data As Long
MSComm1.CommPort = 1 'select your com port
MSComm1.Settings = "9600,n,8,1" 'this must be configured to your specifications
'9600 is the baud rate, n is no parity bits, 8 data bits and 1 stop bit
MSComm1.PortOpen = True 'opens the port
End Sub
'You can read the data on a mouse click;
Private Sub readData_Click()
data = MSComm1.input
End Sub
'Or use the OnComm evet;
Private Sub MSComm1_OnComm()
Dim buffer As Variant
Select Case MSComm1.CommEvent
Case comEvReceive
buffer = MSComm1.Input
Text1.Text = Text1.Text & buffer 'this will append the incoming data to the text1 text box
End Select
End Sub
'If you require the handshaking signals, you can use a null moden cable to simulate.
Wiring:
http://www.loop-back.com/null-mod.html
http://www.nullmodem.com/NullModem.htm
Diagrams:
http://www.beckwithelectric.com/relays/m3420/commapp/images/ca_fig05.gif
http://goforit.unk.edu/datacomm/images/dc_00oe.gif
http://www.qsl.net/wb3afl/graphics/nullmdm.gif
To ensure it's done correctly, you can buy pre-made null modem cables, as well as connectors for around $1.50. Check this site for info
http://www.national-tech.com/catalog/nullmodemcables.htm
http://www.trianglecables.com/nulmodad.html
You will need to use the correct pin for transmitting and receiving as well, so here is a pinout of a DB9 com port connector:
http://www.aggsoft.com/rs232-pinout-cable/pinout-and-signal.htm
If you want to lear more about the serial port communication, or RS232, check this out:
http://www.arcelect.com/rs232.htm
Bingie
First you need to make certain that the terminal is configured for the correct Baud Rate, Number Of Stop Bits, Number Of Data Bits and Parity Bits. With this not set right, Baud rate mismatch, data mismatch or parity error can occur. You can set these parameters in the Form_Load event
Private Sub Form_Load()
Dim Data As Long
MSComm1.CommPort = 1 'select your com port
MSComm1.Settings = "9600,n,8,1" 'this must be configured to your specifications
'9600 is the baud rate, n is no parity bits, 8 data bits and 1 stop bit
MSComm1.PortOpen = True 'opens the port
End Sub
'You can read the data on a mouse click;
Private Sub readData_Click()
data = MSComm1.input
End Sub
'Or use the OnComm evet;
Private Sub MSComm1_OnComm()
Dim buffer As Variant
Select Case MSComm1.CommEvent
Case comEvReceive
buffer = MSComm1.Input
Text1.Text = Text1.Text & buffer 'this will append the incoming data to the text1 text box
End Select
End Sub
'If you require the handshaking signals, you can use a null moden cable to simulate.
Wiring:
http://www.loop-back.com/null-mod.html
http://www.nullmodem.com/NullModem.htm
Diagrams:
http://www.beckwithelectric.com/relays/m3420/commapp/images/ca_fig05.gif
http://goforit.unk.edu/datacomm/images/dc_00oe.gif
http://www.qsl.net/wb3afl/graphics/nullmdm.gif
To ensure it's done correctly, you can buy pre-made null modem cables, as well as connectors for around $1.50. Check this site for info
http://www.national-tech.com/catalog/nullmodemcables.htm
http://www.trianglecables.com/nulmodad.html
You will need to use the correct pin for transmitting and receiving as well, so here is a pinout of a DB9 com port connector:
http://www.aggsoft.com/rs232-pinout-cable/pinout-and-signal.htm
If you want to lear more about the serial port communication, or RS232, check this out:
http://www.arcelect.com/rs232.htm
Bingie
Just a small point about Bingie's code.
The .onComm event does not fire until there are .RThreshold characters in the control's buffer. If this is left to default a 0, then it doesn't fire at all.
You'll need to have something like:
MSComm1.RThreshold = 1
The .onComm event does not fire until there are .RThreshold characters in the control's buffer. If this is left to default a 0, then it doesn't fire at all.
You'll need to have something like:
MSComm1.RThreshold = 1
ASKER
Hi GrahamSkan,
You mean I have to apply MSComm1.RThreshold = 1 at form_load() ? or inside MSComm1_OnComm() ?
You mean I have to apply MSComm1.RThreshold = 1 at form_load() ? or inside MSComm1_OnComm() ?
At form load is fine, or you can set it at design time.
ASKER
any one familiar with folowing script? Will it works?
Private Sub Form_Load ()
' Buffer to hold input string
Dim Instring As String
' Use COM1.
MSComm1.CommPort = 1
' 9600 baud, no parity, 8 data, and 1 stop bit.
MSComm1.Settings = "9600,N,8,1"
' Tell the control to read entire buffer when Input
' is used.
MSComm1.InputLen = 0
' Open the port.
MSComm1.PortOpen = True
' Send the attention command to the modem.
MSComm1.Output = "ATV1Q0" & Chr$(13) ' Ensure that
' the modem responds with "OK".
' Wait for data to come back to the serial port.
Do
DoEvents
Buffer$ = Buffer$ & MSComm1.Input
Loop Until InStr(Buffer$, "OK" & vbCRLF)
' Read the "OK" response data in the serial port.
' Close the serial port.
MSComm1.PortOpen = False
End Sub
Private Sub Form_Load ()
' Buffer to hold input string
Dim Instring As String
' Use COM1.
MSComm1.CommPort = 1
' 9600 baud, no parity, 8 data, and 1 stop bit.
MSComm1.Settings = "9600,N,8,1"
' Tell the control to read entire buffer when Input
' is used.
MSComm1.InputLen = 0
' Open the port.
MSComm1.PortOpen = True
' Send the attention command to the modem.
MSComm1.Output = "ATV1Q0" & Chr$(13) ' Ensure that
' the modem responds with "OK".
' Wait for data to come back to the serial port.
Do
DoEvents
Buffer$ = Buffer$ & MSComm1.Input
Loop Until InStr(Buffer$, "OK" & vbCRLF)
' Read the "OK" response data in the serial port.
' Close the serial port.
MSComm1.PortOpen = False
End Sub
Yes, it's from the MSDN. It works if you have a Hayes-compatible modem on Comm1.
It doesn't use the OnComm event, but loops until it gets the answer expected.
It doesn't use the OnComm event, but loops until it gets the answer expected.
follow this link and you will find the project that shows what you are trying to achieve.
http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=40594&lngWId=1
hope this helps
Mitzs
http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=40594&lngWId=1
hope this helps
Mitzs
ASKER
Why my Win XP keep telling me the open is already open... and I have nothing connected in Com port 1
A port being open means that something on your PC has opened it (your program, hyperterminal Dial-up connection?).
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
eh..i have problem using MSComm1. the program says that need to declare as a variable. is it something i have to declared before using MSComm1????
ander99,
If you have Option Explicit in the Declarations are of your module as many of us would recommend, then you will get a compile error until you declare (Dim, Public) all your variables (and get the spelling correct).
However, I suggest that you post your own question either here in VB Controls, or in the Visual Basic Area. You will have to give more details.
If you have Option Explicit in the Declarations are of your module as many of us would recommend, then you will get a compile error until you declare (Dim, Public) all your variables (and get the spelling correct).
However, I suggest that you post your own question either here in VB Controls, or in the Visual Basic Area. You will have to give more details.
Private Sub Form_Load()
Dim Data As Long
MSComm1.CommPort = 1 'select your com port
MSComm1.Settings = "9600,n,8,1" 'this must be configured to your specifications
'9600 is the baud rate, n is no parity bits, 8 data bits and 1 stop bit
MSComm1.PortOpen = True 'opens the port
End Sub
note the "MSComm1" ? that is the problem. must i declare the MSComm1 as (Dim, Public) or is MSComm1 a reserve word??? because the program says that the MSComm1 has to be declared as a variable. is there any coding i need to declare first before using MSComm1??
Dim Data As Long
MSComm1.CommPort = 1 'select your com port
MSComm1.Settings = "9600,n,8,1" 'this must be configured to your specifications
'9600 is the baud rate, n is no parity bits, 8 data bits and 1 stop bit
MSComm1.PortOpen = True 'opens the port
End Sub
note the "MSComm1" ? that is the problem. must i declare the MSComm1 as (Dim, Public) or is MSComm1 a reserve word??? because the program says that the MSComm1 has to be declared as a variable. is there any coding i need to declare first before using MSComm1??
It is not a reserved word. MSComm1 is the default name for the first MSComm control added to a Form. If the name is unknown, VB suggests that it is an undeclared variable. Actually you and I know that it is a missing or misnamed control. If you don't have an MSComm control already, put one on your form. If there is one there, make sure that its name matches the name used in the code. You don't have to Dim the name as well, in fact if you do you will get a different error.
THKS for the comment but how do i add the control??? ^^"
Can pls guide me??? thks.
Can pls guide me??? thks.
You obviously need some beginner's help with VB. Here are some free on-line tutorials. suggest that you work through at least one of them before continuing with your project.
http://www.developerfusion.com/show/30/
http://www.hitmill.com/programming/vb/beginner.html
http://www.vbtutor.net/
If you still have problems after that, then I suggest (again) that you ask your own question. I will not comment any more to this question.
http://www.developerfusion.com/show/30/
http://www.hitmill.com/programming/vb/beginner.html
http://www.vbtutor.net/
If you still have problems after that, then I suggest (again) that you ask your own question. I will not comment any more to this question.
eh I just found out the problems, i think is because i am using visual basic.net. does that means that the coding above is not suitable???
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/comm98/html/vbobjcomm.asp