Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Serial port communications 1

Status
Not open for further replies.

KornGeek

Programmer
Aug 1, 2002
1,961
US
I am rewriting an old QBasic program using VB .Net. In the old program, we accessed the com port treating it like a file. The code looked something like this:
(please forgive the approximations. I don't have my code in front of me)

Open "COM1:9600,E,7,1" For Random As #1

This opened the port for random access as file #1. I am trying to do something similar and I'm having all kinds of problems. Using code like this:

FileOpen (filenum, "COM1:9600,E,7,1", Random)

I know that's not the exact syntax, but it's similar.

Doing this, I get an error with Number 75 and Description (similar to) "FileStream will not open Win32 devices like disk partitions and tape drives."

I'm open to other methods of doing this, but I need to read data from and write data to a serial port. I saw some posts referencing (again, something like) MsiComm, but I don't know what that is or how to use it and I was unable to find any answers in my help files or on Tek-Tips.

Please offer what help you can. I'll be very grateful.
 
Here is some code that may get you going. Add a reference to MSCommLib (Project|Add Reference| select the "COM" tab). Add a txtSend, txtReceive, and some buttons btnOpenPort, btnClosePort, btnSendData, btnReceiveData

Imports MSCommLib

Public Class CommControl
Inherits System.Windows.Forms.Form

Dim MyComm As New MSCommClass()

Private Sub btnOpenPort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenPort.Click
subOpenComm()
End Sub

Private Sub subOpenComm()

With MyComm
' Which comm port?
.CommPort = 1

' 9600 baud, no parity, 8 data bits, and 1 stop bit
.Settings = "9600,N,8,1"

' Open the port
.PortOpen = True

End With

End Sub

Private Sub btnSendData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSendData.Click
MyComm.Output = Me.txtSend.Text & ControlChars.CrLf

End Sub


Private Sub btnReceiveData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReceiveData.Click
Me.txtReceive.Text = MyComm.Input

End Sub

Private Sub btnClosePort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClosePort.Click
MyComm.PortOpen = False

End Sub
End Class


 
Thanks for the feedback. I'll give this a try and let you know how it works out.
 
Richs,
I went to the Add Reference / Com tab, but I don't see MSCommLib listed there. I've searched my hard drive for *mscomm* and didn't turn up anything. I'm currently trying to locate a download from Microsoft's site, but I'm not having much luck. Do you have any suggestions?
 
It should show in the list as:

Microsoft Comm Control 6.0
Version 1.1

For reference, on my box the location is:

C:\WINNT\System32\MSCOMM32.OCX

The file date is 6/24/1998 so if you don't have it you can probably unpack it from a windows CD-ROM.

(BTW, I keep thinking that there must be a better .NET way to do this but I have yet to find one.)
 
I can't seem to get this to work. I located a MSCOMM32.OCX on another computer that I copied to my computer and registered using regsvr. It was version 1.1, with file date 6/24/1998. When I go to run the code, I get an error message stating "Class is not licensed for use."

I am using VB .Net Standard Edition. Is there something about this version that makes doing serial communications impossible? I was under the impression (thanks to Microsoft's website) that this was a fully functional version, but it doesn't seem to support basic functionality like this. Am I just missing something, or have they severely crippled this language to force me to spend 10 times as much?
 
Thanks for the info. For this project, because time was more important than UI, I reverted back to QBasic. I'm researching oher info on this, and will post any new findings.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top