6 Replies - 686 Views - Last Post: 07 February 2012 - 08:42 AM Rate Topic: -----

Topic Sponsor:

#1 gm5660  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 12-August 08

Opening Serial Port

Posted 29 January 2012 - 10:08 AM

Hi all. I am trying to make a Christmas light animation via computwer control. I am using VB.Net and have tried several examples I have found, but I get the same error: port is closed. The code I am using is this:

Imports System.IO.Ports
Public Class Form1
    Dim instance As New SerialPort
    Dim textMsg As String
    Private mySerialPort As New SerialPort
    Public Sub Open()
        Dim instance As New SerialPort

        'Setup Proceedure
        With mySerialPort
            .PortName = "COM4"
            .BaudRate = 300
            .DataBits = 8
            .Parity = Parity.None
            .StopBits = StopBits.One
            .Handshake = Handshake.None
        End With

        Try
            instance.Open()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
        Me.Close()
    End Sub

    Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click

        'Writing Data
        textMsg = "00100"
        instance.Write(Text)

    End Sub
End Class




Can anyone tell me what is wrong with this code? I have tried several things and I still get the error message that the port is closed. Any help will be appreciated.

Michael

Is This A Good Question/Topic? 0
  • +

Replies To: Opening Serial Port

#2 Ionut  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 327
  • View blog
  • Posts: 914
  • Joined: 17-July 10

Re: Opening Serial Port

Posted 29 January 2012 - 10:16 AM

Are you sure you have a COM4 installed on your computer? Go to DeviceManager and identify the correct name(may it is COM1)
Also, I noticed that you set up a variable and access Open method on another one.
Dim instance As New SerialPort
'Setup Proceedure
With mySerialPort
....

Try
   instance.Open() 'change here to mySerialPort


Was This Post Helpful? 1
  • +
  • -

#3 gm5660  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 12-August 08

Re: Opening Serial Port

Posted 30 January 2012 - 12:13 AM

The device manager said "COMM4" and the driver is functioning properly. It says there is nothing wrong with this device. My OS is Vista. Could it possibly be a firewall issue?
Was This Post Helpful? 0
  • +
  • -

#4 DimitriV  Icon User is offline

  • It's been so long, without this feeling
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,533
  • Joined: 24-July 11

Re: Opening Serial Port

Posted 30 January 2012 - 12:16 AM

View PostIonut, on 30 January 2012 - 03:16 AM, said:

Are you sure you have a COM4 installed on your computer? Go to DeviceManager and identify the correct name(may it is COM1)
Also, I noticed that you set up a variable and access Open method on another one.
Dim instance As New SerialPort
'Setup Proceedure
With mySerialPort
....

Try
   instance.Open() 'change here to mySerialPort


I'm going with Ionut here. You're setting properties for myserialport then opening Instance instead!
Was This Post Helpful? 1
  • +
  • -

#5 gm5660  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 12-August 08

Re: Opening Serial Port

Posted 05 February 2012 - 09:22 AM

In my device manager, it says it is COM4 in the Ports list. In the USB list it says 'Serial Device". When I click on properties for both, both say, 'Device is working properly' I found some code in an old book used when I was learning VB 2005. It is scratchy and needed to be tweeked, but I got it to eliminate the error code of not opening the COM4 port. It says it has COM4 open. I usd a try/catch to verify the port and nothing was thrown back, so I have to assume it is opne.

If there is a way to verify it is open other than the try/catch program lines, I have no idea. If there is a way, can you tell me what and how to verify the port is open, say through Windows applications or such.

Next problem I see is that I ned to send a byte of info to the port. I don't know if it will send the string in binary or the hex equivalient in binary. If it sends text, then it should send my text string of '00010000' as '00010000', but if it sends it in hex, then converts it into binary, it should go:'00010000 to hex'10', then convert it back into binary'00010000', I should be OK.

I am also wondering if it would be easier to open this port in DOS instead of Windows. I will have to explore the DOS programs and see if I can call a DOS program to open the port.

Any help will be greatly appreciated. Thank you to all who have given great advice to help me achieve my goal.
Was This Post Helpful? 0
  • +
  • -

#6 Ionut  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 327
  • View blog
  • Posts: 914
  • Joined: 17-July 10

Re: Opening Serial Port

Posted 05 February 2012 - 11:14 AM

Quote

I usd a try/catch to verify the port and nothing was thrown back, so I have to assume it is opne.

This is a correct assumption. If you want to test and see if the serial port is open, launch another instance of your application and that instance should crash telling that COM4 is already opened or it is busy.

Quote

Next problem I see is that I ned to send a byte of info to the port. I don't know if it will send the string in binary or the hex equivalient in binary. If it sends text, then it should send my text string of '00010000' as '00010000', but if it sends it in hex, then converts it into binary, it should go:'00010000 to hex'10', then convert it back into binary'00010000', I should be OK.

On serial port what you send it's what you receive. If you send a string, a char, a byte that is what the other application will receive. There is no conversion on the transmission. So, if you send '00010000' from PC1, you will receive'00010000' on PC2.

Quote

I am also wondering if it would be easier to open this port in DOS instead of Windows. I will have to explore the DOS programs and see if I can call a DOS program to open the port.

No, it is not easier. But why would you trouble yourself with this question? Except maybe some device poorly implemented, I don't think someone is using DOS anymore.

This post has been edited by Ionut: 05 February 2012 - 11:15 AM

Was This Post Helpful? 1
  • +
  • -

#7 gm5660  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 12-August 08

Re: Opening Serial Port

Posted 07 February 2012 - 08:42 AM

I'll give the 'second instance' a try and see if it crashes.

I was taught on an IBM XT machine long ago. It was DOS based. Just a crazy idea.

Thanks once again for the help. I'll see if it crashes and let you know if COM4 is ligitimate.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1