Library tutorials & articles

Com Ports and the MSComm Control

Introduction

One subject that seems to be discussed very little on the MSDN and on other VB sites is the COM port, and how to use it in Visual Basic. Therefore, in this tutorial, we will discuss detecting the available COM ports, and using the Microsoft COMM control to communicate with COM ports.

Comments

  1. 10 Jul 2006 at 13:19

    Hi. I need to comunicate with a money colector using VB 6.0 Pro. I had installed the MsComm control.

    How can i send this comands to the  money collector and how i can read the answers ?

            254 - Simple Poll
    18 0.00002576 WRITE Length 5: 02 00 01 FE FF  
    19 0.00851212 READ Length 5: 02 00 01 FE FF  
    20 0.00621133 READ Length 4: 01 00 02 00  
    21 0.00000270 READ Length 1: FD



      
            246 - Request Manufacturer ID
    23 0.00001986 WRITE Length 5: 02 00 01 F6 07  
    24 0.00854877 READ Length 5: 02 00 01 F6 07  
    25 0.00935437 READ Length 4: 01 03 02 00  
    26 0.00000235 READ Length 4: 4E 52 49 11




     
            244 - Request Product Code
    28 0.00002023 WRITE Length 5: 02 00 01 F4 09  
    29 0.00852501 READ Length 5: 02 00 01 F4 09  
    30 0.00935432 READ Length 4: 01 03 02 00  
    31 0.00000234 READ Length 4: 47 31 33 4F  





            242 - Request Serial No
    33 0.00002094 WRITE Length 5: 02 00 01 F2 0B  
    34 0.00853151 READ Length 5: 02 00 01 F2 0B  
    35 0.00935416 READ Length 4: 01 03 02 00  
    36 0.00000241 READ Length 4: BF 42 5F 9A  






    Thanks... Raxe

  2. 09 Feb 2006 at 14:51

    First of all, I have to thank you for giving out this lovely tutorial:) This is a great help for folks like who has no idea in using the Ms comm Control, Thank you very much and keep up the good work. :cool:  

  3. 12 Dec 2005 at 08:48

    Dear Friend Virag,
    I have done lots of R&D in this matter, and what i found is, it is best option to go with third party controls & components to work with these type of apps. So search web u will have options for such controls

  4. 11 Dec 2005 at 10:30

    dear friend,


    i m looking for new tips for controling hardware using visual basic code.
    i also want to build interactive voice response system by which i can control my computer from anywhere. just like listninig songs from my computer.



    please help me to get this result.



    waiting for positive reposnce


    from


    virag jani


  5. 15 Oct 2005 at 12:18

    Is it possible to develop simple IVR Apps with ms comm control in which step of wave file prompts guide the caller and he/she can choose option from phone buttons. By detecting caller choice programmer should able to take action

  6. 24 Sep 2005 at 07:14

    HI EVERYBODY
    GUYS
    I NEED SMS CODE USING VB,BY WHICH WE CAN SEND SMS IN ALL THE MOBILE
    SO KINDLY GIVE ME CODE AND ALSO ALL THE INFORMATION HOW TO USE IT
    THANKS
    THIS IS URGENT
    ASHISH

  7. 21 Sep 2005 at 07:36

    You can use Wavecom modems - www.wavecom.com.  Phones are generally less stable and can hang.

  8. 21 Sep 2005 at 07:13

    Hi everybody,
    I am also working on SMS application via GSM modem, can any one tell which GSM modem will be better for this purpose, why can't we use mobile phones?


    Thanks & regards
    lsf_kan

  9. 29 Aug 2005 at 18:42

    You can try this easy to use SMS Gateway  sender freeware, VisualGSM Lite, that provides HTTP and Commandline interface:


    http://www.sms-gateway-software.com


    Rgds,
    Joshua

  10. 04 Aug 2005 at 17:12

    Hi!


    Thanks for the hint for opening COM ports higher than 9. It works fine with the CreateFile("\.\COMx", ...).


    Bye!
    Vero

  11. 25 Jul 2005 at 11:05

    i am new user of mscomm control.If you send me material fo making  "mobile nokia 3315 using F-Bus protocol project".I will be thank full if you help me
    e-mail:asif_kh7@yahoo.com

  12. 12 Jul 2005 at 10:59

    Hi all i am trying to communicate ny mobile nokia 3315 using F-Bus protocol when i try in VB i am gettin some data in responce with the mscomm but when i tried the same with VC++ i am not able to read the input of the data sent from control
    if any one of u has any information regarding this please help
    thanks in advance
    bye: confused:

  13. 07 Jul 2005 at 09:50

    > I have heard that MSCOMM can only handle upto 16 ports only.
    > Is this really true?


    Yes, MSCOMM only handles port numbers from 1 to 16. I have an issue with this as my laptop's docking station mapped the serial port in at number 27 (COM27!! I managed to re-map it using XPpro's Device Manager to number 12, which was the lowest one that it said was unused.


    There must be a new mechanism/method of supporting COM ports in XP which completely abstracts the application from the hardware (OSs like Win95/98 provide a software layer, but you had the sense that you were addressing a physical port).


    In my case, I think the answer is to find out how XP maps physical serial ports to the logical ports presented to the application layer, especially to legacy apps that only know about COM1 & COM2 (and perhaps COM3 & COM4), and so how to re-establish what I would consider 'normal' COM port numbers.

  14. 17 May 2005 at 04:11

    I have heard that MSCOMM can only handle upto 16 ports only.
    Is this really true?

  15. 11 May 2005 at 17:54

    I am trying to send the "Page Down" key to the output buffer. Any ideas on how?


    Thanks,


    g

  16. 11 Feb 2005 at 02:37

    Hi


    I have been trying to work with MSComm control. I am able to recieve messages but not send any. I am working on transmitting messages to a video modulator. The code works in VC++ though it is a different library. It does not work out in VB 6.0. The problem is not with the hardware. I got  comEventFrame and comEventRxOver errors. My settings are proper. Can you help with the same?


    Regards,
    SR

  17. 25 Nov 2004 at 05:31

    How can I send  a print job, directed to LPT1 , to a Network port programatically without pinning the network path to LPT1 at the command promt.(Win2k/XP)

  18. 25 Nov 2004 at 05:24

    How can i move a print job from one queue to another queue using windows API functions,structures and contants.I can receive the job info but not the job's data(Contents) itself.

  19. 04 Oct 2004 at 17:37

    change
    hCOM = CreateFile(".\COM" & COMNum & "", 0, FILESHAREREAD + FILESHAREWRITE, sec, OPENEXISTING, FILEATTRIBUTENORMAL, 0)
    to
    hCOM = CreateFile("\.\COM" & COMNum & "", 0, FILE
    SHAREREAD + FILESHAREWRITE, sec, OPENEXISTING, FILEATTRIBUTENORMAL, 0)


  20. 19 Sep 2004 at 00:07

    Instead of Clng(0), you can use 0&.

  21. 13 Aug 2004 at 04:06

    if u got the answer please send to me also vpbashir@eim.ae

  22. 28 May 2004 at 17:58

    I think u need to clear the buffer, before your commport load the second data in . it will be a very hard time when you first time do the commport connection. try your best

  23. 03 May 2004 at 03:57

    To send and receive SMS, Dinkum Technology Pte Ltd (Singapore) provides API. The features includes Send and Receive SMS, Maintain Phone Books, Voice Call, Data Call, Alarm and lot more.


    http://www.dinkum.com.sg/products/hardware-wmapi.asp


    WM-API is easy to use. It needs only 3 lines of code to send SMS from a Visual Basic application. It also supports Java, Visual C++, Delphi and .NET.


    Dinkum is also distributors for Wavecom modems. You can purchase Wavecom Fastrack Modems, Quik Modem Modules, Integra Modem Modules from Dinkum Technology Pte Ltd. Please visit www.dinkum.com.sg for more details.


    You can also send your questions on sending receiveing SMS and questions related to GSM modems to technical@dinkum.com.sg

  24. 29 Apr 2004 at 23:39

    James-


    Perhaps you should mention that your code as posted, for checking the COM ports, doesn't work under Windows 98, ME, or CE.


    In order for it to work under thoses OSes, you must make the following changes:


    1) Change the 4th parameter in the declaration of CreateFile() from [courier new]lpSecurityAttributes As SECURITY_ATTRIBUTES[/courier new]  to [courier new]lpSecurityAttributes As Any[/courier new]


    2) When you call the CreateFile() function, change its 4th parameter to [courier new]ByVal CLng(0)[/courier new] . Just passing plain old 0 (zero) won't work.



    The code as posted works under NT, 2000, and I would think later versions (XP), but I haven't tested it.



    Cheers,



    -Andrew

  25. 23 Apr 2004 at 02:53

    Thanks for prjMSComm, very useful. However I found and fixed a problem with the COMAvailable Function.
    The problem appears to be that Createfile has a problem with the concatenation of ("COM" & COMNum  & ""...etc) in the original function
    I ran into this problem using an add-in 8 port com card and could not find COM 10 with the code snippet that I put into my project. Even though the project
    actually requires only up to COM 9, I would not rest until I solved this. I did not find any relevant documentation about this condition researching the Createfile API function and ended up finding the solution on a Delphi posting!  Serial I/O (RS232) Using Delphi (RS232)



    Serial I/O (RS232) Using Delphi (RS232)


    I have modified your "COMAvailable" function to use a helper function called "GetAlias" which simply parses the proper string to feed to the
    "CreateFile" function.


    I hope this is helpful... c.caduff@racalate.com








    '// This detects if a COM ports is available.
    '// Used by the ListComPorts() procedure
    '// Returns TRUE if the COM exists, FALSE if the COM does not exist
    Public Function COMAvailable(COMNum As Integer) As Boolean
       Dim hCOM As Long
       Dim ret As Long
       Dim sec As SECURITYATTRIBUTES
       'New variable to hold results of GetAlias
       Dim commHandle As String
       
       'Original
       'try to open the COM port
    '    hCOM = CreateFile("COM" & COMNum & "", 0, FILE
    SHAREREAD + FILESHAREWRITE, sec, OPENEXISTING, FILEATTRIBUTENORMAL, 0)
       
       'Modified
       'get alias which works on all Com ports
       commHandle = GetAlias(COMNum)
       'revised call uses commHandle returned from "GetAlias" function
       hCOM = CreateFile(commHandle & "", 0, FILESHAREREAD + FILESHAREWRITE, sec, OPENEXISTING, FILEATTRIBUTE_NORMAL, 0)
       If hCOM = -1 Then
           COMAvailable = False
       Else
           COMAvailable = True
           'close the COM port
           ret = CloseHandle(hCOM)
       End If
    End Function
    'Patch function to allow CreateFile to get handle for Com Port higher than Com 9
    Public Function GetAlias(nIndex As Integer) As String


    Dim port As Integer
    Dim result As String
       
       port = nIndex
       
       Select Case port
               Case 1:   result = "\.\COM1"     'Port 1
               Case 2:   result = "\.\COM2"     'Port 2
               Case 3:   result = "\.\COM3"     'Port 3
               Case 4:   result = "\.\COM4"     'Port 4
               Case 5:   result = "\.\COM5"     'Port 5
               Case 6:   result = "\.\COM6"     'Port 6
               Case 7:   result = "\.\COM7"     'Port 7
               Case 8:   result = "\.\COM8"     'Port 8
               Case 9:   result = "\.\COM9"     'Port 9
               Case 10:  result = "\.\COM10"    'Port 10
               Case 11:  result = "\.\COM11"    'Port 11
               Case 12:  result = "\.\COM12"    'Port 12
               Case 13:  result = "\.\COM13"    'Port 13
               Case 14:  result = "\.\COM14"    'Port 14
               Case 15:  result = "\.\COM15"    'Port 15
               Case 16:  result = "\.\COM16"    'Port 16
               
               
       End Select
       
       GetAlias = result


    End Function

  26. 21 Apr 2004 at 06:47

    Hello, I need some hlep here, Dose MSCommm work with USB? I have Itegno300 with USB , can I deal with it via MSCOMM?
    thank you for your prompt response!

  27. 20 Apr 2004 at 10:52

    A friend of mine gave me his machine to fix after he butchered it. He's run a 98 setup CD again and it gets as far as requesting the product registration. I don't want to enter a product key, but revert the machine back to it's original state. What files do I need to edit or remove to stop the machine booting into the setup screen every time and boot normally as it did before?


    A simple solution would be to enter the product key and let it run but I don't want to bugger up all the applications that are currently installed and start re-installing all the stuff all over again. Any help would be appreciated :-)

  28. 08 Apr 2004 at 08:17

    Hi All,
    I have just started using MSComm for serial comunication.
    I have heard that we need to close port after we send data once..... and we again need to open port if we want to send data again.
    Actually I am using this for communication with a device.
    For the first message that I send, I do get an acknowledge.... but when I send the second message(command) I don't get an acknowledge.
    Also how do we know whether data has actually been written on the port or not?
    Can someone please help me on this.


    Thanks all.

  29. 02 Apr 2004 at 06:26

    Please help me to get the SMS message to be stored in Database

  30. 18 Mar 2004 at 00:15

    hi
    can you help me for get caller id from modem.
    i use vc++ compiler and work with com port by
    CreatFile function.
    and use at command
    i used following command but it not worked correctly
    and i cant get caller id.


    at+fclass=8
    at+vcid=1


    thanks


  31. 18 Mar 2004 at 00:13

    Quote:
    [1]Posted by virdi_ds on 29 Jan 2003 08:33 AM[/1]
    Hi
    I want a program which can dial the number, if somebody pick-up the number, his phone no as well as name should be entered in my computer database. I have a list of all phone number along with their name. I suceeded in getting the phone number with honour name and also i am able to dial through my computer. The problem is when somebody pick-ups the phone, I am unaware of that and even if somebody press some button say 5, i am not able to trace the tone or data. I am using an external modem and it is configured properly. My source program is:


    Private Sub cmdDial_Click()
     On Error Resume Next
     Dim str As String, dummy
     str = InputBox("Enter the phone no", "Dialer", 27)
     MSComm1.CommPort = 2
     MSComm1.PortOpen = True
     MSComm1.Output = "ATDT" + str + vbCr
     Do
       dummt = DoEvents()
             Text1.Text = Text1 + MSComm1.Input    'StrConv(MSComm1.Input, vbUnicode)
            Text2.Text = Text2 + MSComm1.Output  'StrConv(MSComm1.Output, vbUnicode)
     Loop
    End Sub


    Private Sub Form_Load()
     MSComm1.DTREnable = True
     MSComm1.InBufferSize = 100
     MSComm1.InputLen = 0
     MSComm1.InputMode = comInputModeText
     MSComm1.NullDiscard = True
     MSComm1.OutBufferSize = 100
     MSComm1.RThreshold = 1
     MSComm1.RTSEnable = True
     MSComm1.Settings = "9600,N,8,1"
     MSComm1.SThreshold = 1
     MSComm1.Handshaking = comRTSXOnXOff ' GetSetting(App.Title, "Properties", "Handshaking", "") 'frmTerminal.MSComm1.Handshaking
    End Sub


    Private Sub MSComm1_OnComm()
     On Error Resume Next
     Dim i As Integer, j
     For i = 0 To 1255
       For j = 0 To 1255
       Next
     Next
     
      Select Case MSComm1.CommEvent
         Case comEventBreak   ' A Break was received.
         Case comEventFrame   ' Framing Error
         Case comEventOverrun   ' Data Lost.
         Case comEventRxOver   ' Receive buffer overflow.
         Case comEventRxParity   ' Parity Error.
         Case comEventTxFull   ' Transmit buffer full.
         Case comEventDCB   ' Unexpected error retrieving DCB]
         Case comEvCD   ' Change in the CD line.
           Text1.Text = test1.Text & "1"
         Case comEvCTS   ' Change in the CTS line.
           Text1.Text = Text1.Text & "2"
         Case comEvDSR   ' Change in the DSR line.
           Text1.Text = Text1.Text & "3"
         Case comEvRing
           Text1.Text = Text1.Text & " Ring" & vbCrLf
         Case comEvReceive   ' Received RThreshold # of
           Text1.Text = Text1.Text & StrConv(MSComm1.Input, vbUnicode) & vbCrLf
         Case comEvSend   ' There are SThreshold number of
           Text1.Text = Text1.Text & " 5 " & StrConv(MSComm1.Input, vbUnicode) & vbCrLf               ' characters in the transmit
         Case comEvEOF   ' An EOF charater was found in
           Text1.Text = Text1.Text & "6"              ' the input stream
      End Select
    End Sub


    Please help me how to solve this problem.

  32. 28 Feb 2004 at 01:24

    Pls help me how to recoeve message.Give the sample code.

  33. 28 Feb 2004 at 01:11

    The com ports are not listed thru VB but are available in the device manager of My Computer.  How to go about?

  34. 18 Feb 2004 at 14:51

    Its me again. Found the solution of my problem.


    Add one more \ in front of ".\COM"  in line hCOM = CreateFile("\.\COM" ....


    i.e. Originally the line was
    hCOM = CreateFile(".\COM" ....


    make it
    hCOM = CreateFile("\.\COM" ....


    to work for comports above 9.

  35. 17 Feb 2004 at 03:20

    I am facing the same problem also! And the curious thing is that after unsuccesfully reading from the modem... all the data is thrown out when i connect with hyperterminal.

  36. 11 Feb 2004 at 11:14

    I used the "list available com ports" function and it worked great until I used a PCMCIA socket card that assigned the com port number 10 to the serial port. I couldnt get it to list. I went into device manager and changed it back to number 7 and it showed up fine.


    Any suggestions?


    Thanks
    Pranav

  37. 09 Feb 2004 at 00:12

    Thanks for this great tutorial.
    I would like to know How to use the MSComm control inside a Activex DLL, in other words, How can I use the control without obtain and Object Variable Definition error from the server application.


    Regards,
    Marcelo.

  38. 27 Jan 2004 at 10:02

    Great tutorial - got some serial weighing-scales implemented into our system in under an hour.

  39. 13 Jan 2004 at 12:09

    Can someone please tell me how to give the output command to a modem in C#.  I have tried everything and I think the problem is trying to duplicated VB.NET's vbCR constant in C#.  Here is the command I am trying:


               string Buffer = "ATDT 916016644844 \n";
               Object oBuffer = (object)Buffer;
               axNETComm1.PortOpen = true;
               axNETComm1.set_Output(ref oBuffer);


    Can anyone please give me some suggestions?????


    Thanks,
    Kendal

  40. 08 Jan 2004 at 11:22

    For whatever reason none of my comm ports are registering as available on either one of my computers (both w98se).  When I try to run the program (the one with the screen shot on pg3)
    I get:
    Run-time error '380'
    Invalid Property value


    as far as I can tell this happens at the end of ListComPorts()
    ...
    cboComm.ListIndex = 0 <----- Since nothing ever populated the cboBox, index is -1 and the assignment fails.


    Has anybody else run into this problem?...I've tried adding ports through windows add hardware but no luck.  Thanks for any help you can provide


    --jeremy

  41. 01 Dec 2003 at 08:20

    hi thanks for your reply


    please help me how to clear the buffer in the modem.
    i am using AT+CMGR -> for receiving and At+CMGS -> for sending and AT+CMGD-> for deleting the message in the modem.


    is there any command available to clear the buffer?


    Thanks
    Regards
    Karthik

  42. 01 Dec 2003 at 05:35

    "hi
    already i am using AT+CMGF=1 ( i.e in Text mode option ).
    i am able to send and receive using the itegno modem. i only issue is that while receiving the message , i am getting the received message plus the previously sent message.


    this happens only when too many messages are sent and received continuously.
    please help me to solve this problem.
    Thanking you
    Karthik"


    Are you sending message to your own number? That's why you see all the messages ? Did you clear the buffer ?
    Please write e-mail directly to developers@itegno.com for more help

  43. 01 Dec 2003 at 02:23

    hi
    already i am using AT+CMGF=1 ( i.e in Text mode option ).
    i am able to send and receive using the itegno modem. i only issue is that while receiving the message , i am getting the received message plus the previously sent message.


    this happens only when too many messages are sent and received continuously.
    please help me to solve this problem.
    Thanking you
    Karthik

  44. 01 Dec 2003 at 02:14

    check if


    AT+CMGF?
    iF RETURN IS ZERO, PLEASE set at+cmgf=1 and at&w to save

  45. 01 Dec 2003 at 02:11

    Karthik,


    check if


    AT+CMGF?
    iF RETURN IS ZERO, PLEASE set at+cmgf=1 and at&w to save

  46. 12 Nov 2003 at 12:31

    Hey,


    Is this Karthik Nahender?


    If so, send me email at dia.abavar@terra.com.br


    If not, oh well.......




  47. 06 Nov 2003 at 05:40

    how to receive the telephone call through modem using Vb. actually it does not established the connection with telephone

  48. 15 Sep 2003 at 02:59

    Hi


    I am Karthik and I am involved in developing a SMS Application using GSM Itegno modem. I am


    facing some problems in reading the SMS message from modem. I am using C# language and


    windows 2000 Professional operating System.


    Problem




    While reading a particular message, I am getting the message received along with the message I have sent previously from the modem. Please help to solve this issue.


    Thanks you in advance



    Regards
    C.S.Karthikeyan

  49. 10 Sep 2003 at 12:55

    I have had a look at the code and run the program in the screen shot however when you get a response from the modem ie. No Carrier it is spread over two lines ie NO CAR
                                                                             RIER


    Why is this and what is the Fix.......



    Thanks in advance .......Richard

  50. 15 Aug 2003 at 04:05

    Does anyone know how to access COM ports above 9?
    I am currently using the RS232.vb module in VB.NET.


    Thanks,


    - Steve

  51. 12 Jul 2003 at 04:02

    Receiving no data when key is pressed after the phone is connected
    pls help me its very very urgent

  52. 10 Jul 2003 at 17:59

    Forgot one thing: When passing the zero, use the syntax "ByVal 0&". This seems to pass a null pointer.

  53. 10 Jul 2003 at 17:50

    The problem (at least in Win 95) is Security Attributes. Change the Alias at the top of the function so that instead of taking his user defined SECURITY_ATTRIBUTES data structure, it takes a Long. Then, pass a zero (aka NULL) to indicate default security attributes. This seems to fix it.

  54. 01 Jul 2003 at 14:37

    Quote:
    [1]Posted by kalp on 19 Mar 2003 04:41 AM[/1]
    i want to develope a online billing system


    first i have to dial a telephone number by press the number on the telephone
    how the dial number print in vb text box

  55. 09 May 2003 at 07:00

    I also would be very interested in a solution for a W98 PC.



    My work around is to use the GetAvailablePorts function of the program which gets the COM port. Then just strip out the others

  56. 11 Apr 2003 at 08:00

    i need to know how to trace the numbers being pressed on the phone after a call is connected(MSComm)?.

  57. 07 Apr 2003 at 02:52

    mm the explanation is ok.


    but u didn't explain if there is something goes wrong, how we would solve theproblem ?


    i got NO CARRIER response from my Itegno modem ?


    so any idea ?

  58. 19 Mar 2003 at 04:41

    i want to develope a online billing system


    first i have to dial a telephone number by press the number on the telephone
    how the dial number print in vb text box

  59. 06 Mar 2003 at 15:34

    hi, when I comm with my motor via com1, it show receive break and frame error. why is that? |I have set the "setting" 9600,7,1

  60. 17 Feb 2003 at 01:14

    hi virdi_ds,


     trapping the callerid through the modem is the project i'm currently doing.
    i got the caller id provision from the telephone company and have checked it.
    but my conexant hcf data fax modem is not helping me in capturing the number.
    may i know which modem ur using?
    have u got idea of how a telephone conversation can be recorded?
    please help
     john

  61. 16 Feb 2003 at 19:04


         Hi, Ezmy!


         I have just seen your message, and I have a problem with the communication with MSComm like your problem. I'm doing a communication between PC and one sort of multimeters, and it is possible for me to read the data from them but not to send command in order to control them.


         If you would like, you could send to me your program and your question, and we will investigate what is the trouble.


         Let's meeting by e-mail.


         Chenel.                                       carloslis13@hotmail.com

  62. 08 Feb 2003 at 22:46

    You should try using a laser scanner with rs232 and connecting it to an odbc data base. Thats a trip. Control is rock solid though.

  63. 29 Jan 2003 at 08:33

    Hi
    I want a program which can dial the number, if somebody pick-up the number, his phone no as well as name should be entered in my computer database. I have a list of all phone number along with their name. I suceeded in getting the phone number with honour name and also i am able to dial through my computer. The problem is when somebody pick-ups the phone, I am unaware of that and even if somebody press some button say 5, i am not able to trace the tone or data. I am using an external modem and it is configured properly. My source program is:


    Private Sub cmdDial_Click()
     On Error Resume Next
     Dim str As String, dummy
     str = InputBox("Enter the phone no", "Dialer", 27)
     MSComm1.CommPort = 2
     MSComm1.PortOpen = True
     MSComm1.Output = "ATDT" + str + vbCr
     Do
       dummt = DoEvents()
             Text1.Text = Text1 + MSComm1.Input    'StrConv(MSComm1.Input, vbUnicode)
            Text2.Text = Text2 + MSComm1.Output  'StrConv(MSComm1.Output, vbUnicode)
     Loop
    End Sub


    Private Sub Form_Load()
     MSComm1.DTREnable = True
     MSComm1.InBufferSize = 100
     MSComm1.InputLen = 0
     MSComm1.InputMode = comInputModeText
     MSComm1.NullDiscard = True
     MSComm1.OutBufferSize = 100
     MSComm1.RThreshold = 1
     MSComm1.RTSEnable = True
     MSComm1.Settings = "9600,N,8,1"
     MSComm1.SThreshold = 1
     MSComm1.Handshaking = comRTSXOnXOff ' GetSetting(App.Title, "Properties", "Handshaking", "") 'frmTerminal.MSComm1.Handshaking
    End Sub


    Private Sub MSComm1_OnComm()
     On Error Resume Next
     Dim i As Integer, j
     For i = 0 To 1255
       For j = 0 To 1255
       Next
     Next
     
      Select Case MSComm1.CommEvent
         Case comEventBreak   ' A Break was received.
         Case comEventFrame   ' Framing Error
         Case comEventOverrun   ' Data Lost.
         Case comEventRxOver   ' Receive buffer overflow.
         Case comEventRxParity   ' Parity Error.
         Case comEventTxFull   ' Transmit buffer full.
         Case comEventDCB   ' Unexpected error retrieving DCB]
         Case comEvCD   ' Change in the CD line.
           Text1.Text = test1.Text & "1"
         Case comEvCTS   ' Change in the CTS line.
           Text1.Text = Text1.Text & "2"
         Case comEvDSR   ' Change in the DSR line.
           Text1.Text = Text1.Text & "3"
         Case comEvRing
           Text1.Text = Text1.Text & " Ring" & vbCrLf
         Case comEvReceive   ' Received RThreshold # of
           Text1.Text = Text1.Text & StrConv(MSComm1.Input, vbUnicode) & vbCrLf
         Case comEvSend   ' There are SThreshold number of
           Text1.Text = Text1.Text & " 5 " & StrConv(MSComm1.Input, vbUnicode) & vbCrLf               ' characters in the transmit
         Case comEvEOF   ' An EOF charater was found in
           Text1.Text = Text1.Text & "6"              ' the input stream
      End Select
    End Sub


    Please help me how to solve this problem.

  64. 25 Jan 2003 at 08:44

    I have connected my phone to modem and modem to my computer. what i need is, when somebody pick-up his phone as dial by me, i should be notified. I know how to dial others phone, but don't know how to receive the voice or data. kindly suggest.

  65. 16 Dec 2002 at 14:07

    I have a issue with MSComm, I will be receiving messages without any problems and all of a sudden, information will stop just coming in.  Do you know any reason for this.

  66. 13 Nov 2002 at 21:00

    I already completed a program to communicate with 8052(microcontroller).  when i sent a byte to get 8052 attention it seems that  i havent received any acknowledgement from 8052.  Until now i cannot figured out how to solve this problem.  Is there anything wrong with the mscomm that the 8052 is not responding or it is the hardware problem?

  67. 13 Nov 2002 at 20:59

    I already completed a program to communicate with 8052(microcontroller).  when i sent a byte to get 8052 attention it seems that the i havent received any acknowledgement from 8052.  Until now i cannot figured out how to solve this problem.  Is there anything wrong with the mscomm that the 8052 is not responding or it is the hardware problem?

  68. 26 Oct 2002 at 20:24

    Dim Communications As MSCommLib.MSComm
    Set Communications = CreateObject("MSCommLib.MSComm")


    This is if you want to instantiate the MSComm control as an object, so go to Project menu in your VB IDE and down to references and make sure you add the MSCommLib reference.


    If you just want to use it as a control, then dont need to instantiate it as an object.


    sal

  69. 25 Oct 2002 at 19:49

    Hi
    I'm programming one monitor, sniffer, in VB for RS 232- that works nice when its set to ModeText,
    but when I tryed for comInputModeBinary, the serial port assumes is a array,


    Could you kindly help me?


    Have a look at the code.


    Private Sub Command1_Click()
    Dim buffer0, buffer As String
    Dim dados As String
    Dim TheMsg() As Byte, sTemp$


    Text1.Text = ""
    If Not (MSComm1.PortOpen) Then
      MSComm1.PortOpen = True
      End If
       MSComm1.InputMode = 1 ' Set InputMode to read binary data
            ' If MSComm1.InBufferCount > 0 Then
            ' buffer0 = MSComm1.Input
               While w <= 204809
                  DoEvents
                  w = w + 1
                  sTemp = MSComm1.Input
                  TheMsg = sTemp
                   If UBound(TheMsg, 1) > -1 Then
                       'Dim p&, i&
                       'p& = LBound(TheMsg)
                       Text1 = Text1 & UBound(TheMsg, 1)
                   End If
                  'Text1 = StrConv(MSComm1.Input, vbFromUnicode)
                  'dados = (Mid(MSComm1.Input, w, 1))     'convert each binay value to ASCII
                  'If dados <> "" Or dados <> Null Then
                   'buffer = Asc(Mid(MSComm1.Input, w, 1)) 'convert each binay value to ASCII
                  'buffer0 = Hex(Mid(buffer, w, 1))   'convert each binay value to ASCII
                  'Text1.Text = Text1.Text + buffer + Chr$(32)
                  'End If
                Wend
              '  Text1.Text = Text1 + buffer
            '  End If
         w = 0
    End Sub

  70. 25 Oct 2002 at 19:20

    I'm programming one monitor, sniffer, in VB for RS 232- that works nice when its set to ModeText,
    but when I tryed for comInputModeBinary, the serial port assumes is a array,


    Could you kindly help me?


    Thanks
    ->The Code:


    Private Sub Command1_Click()
    Dim buffer0, buffer As String
    Dim dados As String
    Dim TheMsg() As Byte, sTemp$


    Text1.Text = ""
    If Not (MSComm1.PortOpen) Then
      MSComm1.PortOpen = True
      End If
       MSComm1.InputMode = 1 ' Set InputMode to read binary data
            ' If MSComm1.InBufferCount > 0 Then
            ' buffer0 = MSComm1.Input
               While w <= 204809
                  DoEvents
                  w = w + 1
                  sTemp = MSComm1.Input
                  TheMsg = sTemp
                   If UBound(TheMsg, 1) > -1 Then
                       'Dim p&, i&
                       'p& = LBound(TheMsg)
                       Text1 = Text1 & UBound(TheMsg, 1)
                   End If
                  'Text1 = StrConv(MSComm1.Input, vbFromUnicode)
                  'dados = (Mid(MSComm1.Input, w, 1))    
                  'If dados <> "" Or dados <> Null Then
                   'buffer = Asc(Mid(MSComm1.Input, w, 1)) 'convert each binay value to ASCII
                  'buffer0 = Hex(Mid(buffer, w, 1))   'convert each binay value to ASCII
                  'Text1.Text = Text1.Text + buffer + Chr$(32)
                  'End If
                Wend
              '  Text1.Text = Text1 + buffer
            '  End If
         w = 0
    End Sub


  71. 04 Oct 2002 at 20:30

    As I can know when they answered I telephone using the MSComm  


    my code is :




    Private Sub InicializarModem()


    With msModem
            .CommPort = 3
            .Handshaking = comRTSXOnXOff
            .RThreshold = 1
            .RTSEnable = True
            .Settings = "9600,n,8,1"
            .SThreshold = 1
            .EOFEnable = True
            .InputMode = comInputModeText
            .InputLen = 0
            .PortOpen = True
            .Output = "ATZ"
            .PortOpen = False
    End With


    end sub



    Private Sub CallNumber(Byval sOutput as String)
       With msModem
            .PortOpen = True
            .Output = "ATDT" & sOutput & vbCrLf
       End With
    end sub


    Private Sub msModem_OnComm()
    'Occurs when there's a change in the modem signal
    Dim InBuff As String


    Select Case msModem.CommEvent
       'Case comEventBreak : A Break was received from the line : not required
       'The next three cases are timeout values, no longer detected in Win32 systems
       'Case comEventCDTO
       'Case comEventCTSTO
       'Case comEventDSRTO
       'Case comEventFrame
       
       Case comEventOverrun ' Data Lost during transfer
           MsgBox "Some Data was lost during transfer."
       Case comEventRxOver  ' Receive buffer overflow.
           'taken care in another sub in form2
       Case comEventRxParity   ' Parity Error.
           MsgBox "Parity Error !"
       Case comEventTxFull  ' Transmit buffer full.
           'taken care in another module in form2
       Case comEvCD   ' Change in the CD line.
       'Used to detect connection state :
       'Though it's taken care by using ATX4 modem command, it's required
       'for AT non compatible modems
           If msModem.CDHolding = True Then
               'connected
               Connected = True
           Else
               'Disconnected
               Connected = False
           End If
         Case comEvReceive 'Received RThreshold no. of chars.
            'Use strconv to convert the binary data got from modem
            'InBuff = StrConv(msModem.Input, vbUnicode)
            InBuff = msModem.Input
            Call ScanCom(InBuff)
         Case comEvSend 'There are SThreshold number of characters in the
                        'transmit buffer.(this should never occur as there's form2, but, in case)
            Do While msModem.OutBufferCount > 0
               DoEvents
            Loop


      End Select
     
    End Sub



    Private Sub ScanCom(ByVal instring As String)
    ' This routine will work on data (instring) received from the Com Port.
    Dim X As Integer 'position being read from instring
    Dim Y As String 'data in position being read (X)


    If Connected = False Then
    ' Not connected...so data may be modem responce.
    ' Work on data (instring) received between vbcrlf's:
       For X = 1 To (Len(instring) + 1)
           Y = Mid$(instring, X, 1)
           If Y = Chr$(13) Then
               Y = ""
               '"MessageHandler" is where to do the processing on
               ' the packet after it is fully parsed here:
               Call MessageHandler(UseString)
               UseString = ""
           End If
           UseString = UseString & Y
       Next X


    Else
       'Connected=true. So parse data.
       'Data begins with <modem> , ends with <xodem> + chr$(13)
       For X = 1 To (Len(instring) + 1)
           Y = Mid$(instring, X, 1)
           If Y = Chr$(13) Then
               If Len(UseString) > 11 And Right(UseString, 7) = "<xodem>" Then
                   Y = ""
                   Call MessageHandler(UseString)
                   'Process the data
                   UseString = ""
               End If
           End If
           UseString = UseString & Y
       Next X
    End If
    End Sub


    Private Sub MessageHandler(InBuff As String)
               
    If IsVacio(InBuff) Then Exit Sub
    If InStr(1, InBuff, "=") <> 0 Then Exit Sub
    If Len(InBuff) <= 1 Then Exit Sub


    InBuff = Right(InBuff, Len(InBuff) - 1)


    'Modem responses


    'Occurs when a command sent to modem is accepted
    If InStr(InBuff, "OK") Then


       InBuff = ""
       Exit Sub
    End If


    'if command "ATX4" has been sent to modem,
    'inbuff will be "CONNECT #####" where ##### = speed
    'connected to remote comp
    If InStr(InBuff, "CONNECT") Then
       Connected = True
       lblLlamada(3) = "CONECTADO"
       strHoraInicioLlamada = Format(Time, "HH:MMS")
       InBuff = ""
       Exit Sub
    End If
     
     
    If InStr(InBuff, "CARRIER") Then
       
       If MsgBox("No se recibio respuesta del telefono marcado, desea capturar alguna observación ?", vbYesNo + vbQuestion, "Automarcado") = vbYes Then
           tmpObs = InputBox("Capture aqui las observaciones de la llamada", "Automarcado", " ")
           If tmpObs <> "" Then
               txtObservacionesLlamada = "" & UCase(tmpObs)
           End If
       End If
       
       InBuff = ""
       Call ActualizaEstatusLlamada(NO_CONTESTO)
       txtObservacionesLlamada = ""
       Call ColgarTelefono
       Call Actualiza
       ' Se mueve al siguiente registro
       If TDBG1.EOF Then
           TDBG1.MoveFirst
       Else
           TDBG1.MoveNext
       End If
       
       Exit Sub
       
    End If
     
    If InStr(InBuff, "ERROR") Then
       InBuff = ""
       MsgBox "Ocurrio un error al marcar al número " & TDBG1.Columns("TelCasa"), vbCritical
       Exit Sub
    End If
     

  72. 27 Sep 2002 at 03:30

    Halo,


    I can fire the commevent but not always...that's make my prg not consistant.. any help

  73. 27 Sep 2002 at 03:27

    Hi,


    I faced the same prob with you. can some one offer help!!


    by the way, I'm doing the sms thing I'm can send out the msg but can't receive the msg.  why ah?

  74. 18 Sep 2002 at 09:28

    Hey Kevin,


    If you don't mind sharing, what was your workaround.


    Terry

  75. 15 Sep 2002 at 16:35

    no real answer, I have used alternate means to read the com port that works  (sort of)


    kevin

  76. 15 Sep 2002 at 00:35

    Kevin,


    Have you found a solution to this yet?  I am having the same problem (error 424).


    I would appreciate any insight you may have.


    Terry

  77. 12 Sep 2002 at 05:40

    How do i create a marquee script for an email signature....ive seen it on someones email they sent me and it looks so cool, i am on a works network p.c so i dont know if i am able to do it or not, please supply the code and instructions if poss


    kind regards


    chris

  78. 30 Aug 2002 at 17:25



    i have a copy (from legit source) of mscomm ocx and have added to available controls, but vb 6 does not recognise mscomm (says it need an object declaration) - the downloaded code from here causes error - any thoughts?


    kevin

  79. 25 Aug 2002 at 16:36

    Hi,
        Please help me regarding the usage of the MSComm control.Iam able to recieve messages but not able to transmit messages.I can clearly hear the other parties voice but they r not able to hear me.

  80. 06 Aug 2002 at 12:40

    Dear All,


    I got some problem when I try to capture data using the onComm1.commEvent


         Case comEvReceive        
           ' receive log here
           inBuffer = inBuffer & MSComm1.Input
           Debug.Print MSComm1.Input


    I have to put the break point infront of the MsComm1_OnComm() inorder to have it fire.


    my configure of MsComm1 is:
    Private Sub Form_Load()
       With MSComm1
             .CommPort = 1
             .Handshaking = comRTSXOnXOff
             .RThreshold = 1
             .RTSEnable = True
             .Settings = "9600,n,8,1"
             .SThreshold = 1
             .PortOpen = True
             .EOFEnable = True
             .InputMode = comInputModeText
             .InputLen = 0
       End With
    End Sub


    According to the posting above it should be working fine.
    But for my case it was not .


    Help,
    Many Thanks

  81. 13 Jul 2002 at 12:50

    Hi Ashsoliman,


    I'm having that problem too, actually with Win Me, but I suspect it doesn't work with any non-XP/2000/NT PCs.


    I have based a port detection part of a project I'm working on around this method, and it worked fine until I came to install the final program on a Win Me system.


    Have you found a solution yet ??


    I'll let you know if I find one.


    Cheers
    Mark

  82. 13 Jul 2002 at 12:42

    OK guys, don't all rush to help ... sussed it anyway!!


    Anyone experiencing the same problem, use comInputModeBinary reception mode and receive the entire buffter into a byte array, then get the size of the array using UBound (+1 to include the first byte stored in the zero element) and write your decoding routine within a for next loop to work through each received byte. (Did that make any sense?? )




    Mark H

  83. 03 Jul 2002 at 07:08

    how can i get the numbers pressed on the telephone using mscomm control, using the same telephone line with modem and telephone?

  84. 21 Jun 2002 at 22:48

    Can i transfer a file to my friend with whom i am talking on a phone by holding on the connection using the mscomm control?
    (this refers to use of voiceview at-commands). please help me out.

  85. 21 Jun 2002 at 15:12

    I tried using your comport example but it didn't work on windows 98.  is there any supporting DLL's or components I need to attach to my project.

  86. 20 Jun 2002 at 09:05

    Hi,


    Hopefully there's a simple solution to this one, but I'm pulling my hair out right now, coz I've tried everyway I can think of ....



    Please forgive me if I'm overlooking the obvious, but though I'm an experienced programmer, I am very new to VB!


    I am trying to write an application to emulate a thermal printer to allow printing 'through' a PC. The received data includes nulls/zeros, mainly as part of command properties, which I need the app to recognise.


    I have tried to receive the data as text (comInputModeText) but as null is an empty string, that doesn't work! I have tried receiving the data numerically (comInputModeBinary) but as soon as I try to run the app with 'NullDiscard' as false it hangs, seemingly receiving infinate nulls/zeros, long after the input data has ceased. The program does not hang if I set 'NullDiscard' to true, but of course then I don't get my nulls!!


    Please Help!! ... anyone!!


    Cheers,


    Mark H

  87. 04 Jun 2002 at 03:56

    Hello,


    How can receive the numbers pressed by anyone while I am talking with him/her via telephone. I think this is the way PABX works.


    My compiler is showing syntax error with the following code-
    txtOutput.Text = txtOutput.Text & StrConv(MSComm1.Input, vbUnicode)


    Please help...


    Anup

  88. 30 May 2002 at 01:08

    Can we use mscomm control to read synchronous binary data. If yes, please tell me how?


    Thank you.

  89. 04 Apr 2002 at 10:02

    As long as PortOpen is TRUE, the OnComm Event will fire if there is incomming data.

  90. 26 Mar 2002 at 05:05

    hi..there...


    I would like to ask you the following questions.


    1. If I open my Com port, does it mean I am always listening to incoming data from that com port?
    2. If it does not keep listening, how do I do the polling in my program so that it will keep listening to incoming data from that com port?


    Hope you would like to answer my questions....


    Thank You very much


    Nelly

  91. 01 Jan 1999 at 00:00

    This thread is for discussions of Com Ports and the MSComm Control.

Leave a comment

Sign in or Join us (it's free).