Solved

Read Serial Communication Port

Posted on 2008-10-29
22
892 Views
Last Modified: 2013-12-26
Hello Experts.
I am using VB6.

A program is running, and this Program is writing to the Serial Communication Port.
I need to write a Program- To run on the same machine at the same time - , that will be able to read that port.

Is this possible ?
If Yes, can you please show me how ?

Thanks
0
Comment
Question by:HESUS
  • 11
  • 9
22 Comments
 
LVL 53

Expert Comment

by:Dhaest
Comment Utility
Did you take a look at the following event:

Connection eg:
MSComm1.CommPort = 1
MSComm1.Settings = "9600,n,8,1"
MSComm1.PortOpen = True


Private Sub MSComm1_OnComm()

On Error Resume Next

If MSComm1.CommEvent = comEvReceive Then
msgbox MSComm1.Input
End If

End Sub
0
 

Author Comment

by:HESUS
Comment Utility
Thank you very much.
If another program is already writing to the Comm Port, will my application still be able to view it as well ?
0
 
LVL 53

Expert Comment

by:Dhaest
Comment Utility
I doubt that, because you're one program has already the port blocked for communication. You can try it to see if your program works...
0
 

Author Comment

by:HESUS
Comment Utility
It is not working.
I am looking for a way to read whats in the Comm buffer, even if another application is using the Comm Port.
0
 
LVL 53

Expert Comment

by:Dhaest
Comment Utility
I don't see why you would one program one the pc would send items, and another program on the same computer will receive the items...
0
 

Author Comment

by:HESUS
Comment Utility
Because one Program is not mine, and this program is sending data to the RS232 Port.
I need to read that data for my application.
0
 
LVL 53

Expert Comment

by:Dhaest
Comment Utility
Then you'll have a problem, because the com-port is blocked by your first program until the program closes the connection with your comp-ort.
0
 

Author Comment

by:HESUS
Comment Utility
But isn't there any way that I can read the Comm Port buffer ?
0
 

Author Comment

by:HESUS
Comment Utility
Please Experts, if anybody can assist here...
0
 
LVL 53

Expert Comment

by:Dhaest
Comment Utility
You can't reaed it while the port is in use by the other program. Only one application at the time can use it.
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:HESUS
Comment Utility
I am not trying to be difficult, I am just desperate&
The data is sitting in a buffer somewhere. Is there no way to read this buffer not using an Mscomm ?
0
 

Author Comment

by:HESUS
Comment Utility
Just of interest, I talked to someone that wrote a program to listen to the Comm Port while other program is writing to it. He wrote his program in Delphi.
If Delphi can do it, why VB can not ?
0
 
LVL 53

Expert Comment

by:Dhaest
Comment Utility
>> If Delphi can do it, why VB can not ?

Are you sure that the program is listening/writing to the same com-port on the same pc ?
I also wrote already some communication through the comport, but it always was on different pc's (one pc was sending/receiving, the other also)
0
 

Author Comment

by:HESUS
Comment Utility
I am 100% sure.
One Application is writing to the Comm Port - for an external equipment, and second application is listenning to the same Comm port and writes it to file. Both applications are running on the Same PC.
0
 

Author Comment

by:HESUS
Comment Utility
I am still waiting for a solution in VB.
Please assist me, I am stuck...
0
 
LVL 53

Expert Comment

by:Dhaest
Comment Utility
Maybe a solution, I'm not sure because I haven't tested it
It has to do something with the MSComm1.PortOpen-setting

Communication over Serial Port for VB6 - Visual Basic 6
http://www.programmers-corner.com/sourcecode/111
0
 

Author Comment

by:HESUS
Comment Utility
Thanks Dhaest for not giving up on me.
The url you sent to me will not let me access it. It says that I do not have permission to access the site.
Any suggestions ?

Thanks
0
 
LVL 53

Expert Comment

by:Dhaest
Comment Utility
This is the  complete code
(next time if you have a difficult question, increase your points. There will be more experts looking at the question)
'1 form with

'1 textbox : name=text1, multiline=true, scrollbars=3(both)

'1 textbox : name=text2

'1 command button

'1 mscomm control (Microsoft Comm Control 6.0)

Option Explicit
 

Private Sub Command1_Click()

  With MSComm1

    'make sure the serial port is open

    If .PortOpen = False Then .PortOpen = True

    'send the data (including a tailing carriage return as often needed)

    .Output = Text2.Text & vbCr

  End With 'MSComm1

  With Text2

    'place the focus back to the textbox

    .SetFocus

    'select the current text to be overwritten

    .SelStart = 0

    .SelLength = Len(.Text)

  End With 'Text1

End Sub
 

Private Sub Form_Load()

  With MSComm1

    'make sure the serial port is not open (by this program)

    If .PortOpen Then .PortOpen = False

    'set the active serial port

    .CommPort = 2

    'set the badurate,parity,databits,stopbits for the connection

    .Settings = "9600,N,8,1"

    'set the DRT and RTS flags

    .DTREnable = True

    .RTSEnable = True

    'enable the oncomm event for every reveived character

    .RThreshold = 1

    'disable the oncomm event for send characters

    .SThreshold = 0

    'open the serial port

    .PortOpen = True

  End With 'MSComm1

  With Text1

    'set the properties for the displaying textbox

    .BackColor = vbCyan

    .Locked = True

    .Text = ""

  End With 'Text1

  With Text2

    'set the properties for the 'send' textbox

    .TabIndex = 0

    .Text = ""

  End With 'Text2

  With Command1

    'set the properties for the 'send' command button

    .Caption = "&Send"

    .Default = True

    .TabIndex = 1

  End With 'Command1

End Sub
 

Private Sub Form_Resize()

  Dim sngWidth As Single, sngHeight As Single

  Dim sngDisplayHeight As Single

  Dim sngTxtWidth As Single

  Dim sngCmdWidth As Single, sngCmdHeight As Single

  'calculate the inner size of the form

  sngWidth = ScaleWidth

  sngHeight = ScaleHeight

  With Command1

    'resize and reposition the command button

    sngCmdHeight = .Height

    sngCmdWidth = .Width

    sngDisplayHeight = sngHeight - sngCmdHeight

    sngTxtWidth = sngWidth - sngCmdWidth

    .Move sngTxtWidth, sngDisplayHeight, sngCmdWidth, sngCmdHeight

  End With 'Command1

  'resize and reposition the label

  Text1.Move 0, 0, sngWidth, sngDisplayHeight

  'resize and reposition the textbox

  Text2.Move 0, sngDisplayHeight, sngTxtWidth, sngCmdHeight

End Sub
 

Private Sub MSComm1_OnComm()

  Dim strInput As String

  With MSComm1

    'test for incoming event

    Select Case .CommEvent

      Case comEvReceive

        'display incoming event data to displaying textbox

        strInput = .Input

        Text1.SelText = strInput

    End Select

  End With 'MSComm1

End Sub

Open in new window

0
 

Author Comment

by:HESUS
Comment Utility
Thank you.
The code is not talking to the Comm port if another program is already using it.

I am taking your advise and raising the points.
0
 
LVL 53

Expert Comment

by:Dhaest
Comment Utility
Like I said earlier... I'm afraid that through vb6 there is no solution for your problem. One vb-program will be exclusively locking the com-port. As long as it's locked, it can't be used by other programs !
0
 

Accepted Solution

by:
ee_auto earned 0 total points
Comment Utility
Question PAQ'd, 250 points refunded, and stored in the solution database.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now