Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 504
  • Last Modified:

New Serial Port object in .net 2.0

There's a new serial port object (system.io.ports.serialport) in .NET 2.0 for VS 2005

It seems to be that this is similar to the original vb6 mscomm control

I can't figure out how to send a command to my serial port.

in VB6 my mscomm control sends correctly to my serial device, but trying the same functionality with this new object using mySerialPort.write OR mySerialPort.writeLine doesn't work.   I made the setting such as baud and parity and handshake, etc the ssame..

Anyone familiar with this control?

Shahzad
0
shahzadq
Asked:
shahzadq
  • 7
  • 5
1 Solution
 
Bob LearnedCommented:
1) What kind of device are you using?
2) Is the port open?
3) Can you show me an example of the code you are using to send data?

Bob
0
 
shahzadqAuthor Commented:
It's a serial port device that is plugged into a corporate phone system called AXXESS.  It accept most regular MODEM commands...  That's how I've been running it in VB6, just sending "ATA" or "ATDT" or whatever straight to the com port.

Yeah, I've opened it.


Here's a couple of different code options I've tried...

   Private Sub btnHuntGroupToggle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHuntGroupToggle.Click
        portAxxess.Open()
        portAxxess.WriteLine("ATDT 818013601062")
        portAxxess.Close()
    End Sub

OR

   Private Sub btnHuntGroupToggle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHuntGroupToggle.Click
        portAxxess = My.Computer.Ports.OpenSerialPort("COM1", 4800)
        MsgBox(portAxxess.IsOpen)
        Using portAxxess
            portAxxess.WriteLine("ATDT 818013601062,,049")
        End Using
   End sub

Also tried it wih --
           portAxxess = My.Computer.Ports.OpenSerialPort("COM1", 4800)


*shrug*...   I dunno

Shahzadq
0
 
Bob LearnedCommented:
1) Did you get the stop bits, data bits, parity settings too?  

2) Does the device require a carriage return to signal the end of a send operation?

3) How did you accomplish this with the M$Comm control in VB6?  

4) With the VB6 version, did you specify the device settings on the control or in code?

Bob
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
shahzadqAuthor Commented:
In my VB6 program, I have

DTREnable - True
HAndshaking - 2 - comRTS
Inbuffersize - 1
InputLen - 1
InputMode 0 -comInputModeText
ParityReplace - ?
RThreshold - 1
RTSEnable - True
Settings 4800,n,8,1
SThreshold - 1

The .Net control has slightly different settings, so it's hard for me to match them up exactly

shahzadq
0
 
shahzadqAuthor Commented:
Also, OutBufferSize = 0




The default for PArityReplace in .net is 63, whereas in VB6 it's "?"

That's kind of odd I think
0
 
shahzadqAuthor Commented:
Just double checked adding a chr(13) to the end of the string sent...  no luck...

It doesn't give any errors, but doesn't do anything either.  When stepping through in debugging, it just walks right over it.






shahzadq
0
 
Bob LearnedCommented:
1) The SerialPort has a few constructors.  Here is the one that I find useful:

    http://msdn2.microsoft.com/en-us/library/z8d1ykfs.aspx

    Private portAxxess As New SerialPort("COM1", 4800, Parity.None, 8, StopBits.One)

2) Set the handshaking:

     portAxxess.Handshake = Handshake.RequestToSend

3) Set RTS enable:

    portAxxess.RtsEnable = True

4) Set DTR enable:
    portAxxess.DtrEnable = True

5) Receive threshold:

    portAxxess.ReceivedBytesThreshold = 1
    portAxxess.ReadBufferSize = 1

6) Write buffer size:

    portAxxess.WriteBufferSize = 1

7) ParityReplace only applies to invalid bytes in the data stream

8) You might want to think about the Encoding property, and if you need it

Bob
0
 
shahzadqAuthor Commented:
Hmmm...  ok will see how that works.  I figured I had these all setup statically in the visual properites section, but I'll code it all in, also try that new constructor...

shahzadq
0
 
Bob LearnedCommented:
I am in favor of setting properties in code.  Then, you can recreate the control, without losing any property values.

Bob
0
 
shahzadqAuthor Commented:
Should I delete the static object first?  Or does that not matter?



shahzad
0
 
Bob LearnedCommented:
If you are creating the port and setting properties in code, then you don't need the instance on the form.

Bob
0
 
shahzadqAuthor Commented:
Holy Smokes!!!

It works!

Woo hoo!!

:)


Shahzad
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now