dcsimg
Results 1 to 9 of 9

Thread: RS232 - COM1, very noobie stuff

  1. #1

    Thread Starter
    Member intellilogic's Avatar
    Join Date
    Oct 2010
    Location
    Tyson's Corner, VA
    Posts
    39

    Question RS232 - COM1, very noobie stuff

    So I am trying to get info from a scale using an RS232 cable into a VB2010 program.

    But before we even start talking about that, I don't know ANYTHING about serial ports. I need to learn how to get the data from the scale without programming first... I am using Win7. I do have a sample program (w/ source) I downloaded(http://www.codeworks.it/net/VBNetRs232.htm) It runs without any errors, then I have a big USER error, what do I push? How do I tinker to figure out the right settings on this interface? ...or any interface.

    These are the sittings for the scale: http://www.dymo.com/media/UserGuides...2_Protocol.pdf

    I am considering returning my scale and getting something with maybe USB on it, any ideas? I need to be able to weight things up 100lbs, I am shipping car parts.

  2. #2
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Pointless Forest 38.517,-92.023
    Posts
    9,245

    Re: RS232 - COM1, very noobie stuff

    If you search this forum for serialport you will find many examples.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  3. #3
    Member
    Join Date
    Aug 2010
    Posts
    54

    Re: RS232 - COM1, very noobie stuff

    How strange! I was just coming to post about linking to a weighing indicator. I am looking for the same thing, although our string is slightly different. ie

    9600, 8 bits, no parity, 1 stop bits.

    If we hyperterminal to the indicator, we get a weight response back from pressing CTRL + G.

    If you find anything I would be very interested, and vice versa

  4. #4
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: RS232 - COM1, very noobie stuff

    A USB scale won't be any different. It's just using USB to emulate... you guessed it, a serial port. When you install the USB driver, it'll make a virtual serial port in your computer; which is no different programming for and talking to than a 9-pin RS232 port.

    Ok, FIRST, you need something to talk directly to the COM port just so you know you're talking to the scale. Such a program is called a Terminal program or Terminal Emulator. An excellent free one is called "PuTTY". Google it and download it. If you got Windows XP or earlier, you can use HyperTerm which comes with it.

    Put PuTTY in "Serial" mode and pick the COM port number (typically COM1) your scale is physically plugged in to and the Speed to "2400" (which is what the Freightmaster document tells you it is) and next click under "Connection->Serial" in the tree to the left. Change Stop bits to "2" and start it up. Paul is correct with his settings, as 9600 Baud, 8 data bits, No parity, 1 stop bit is very typical for scales. The Freightmaster seems to be an exception.

    You should be talking to your scale/scale indicator now. If you look in your handbook that came with the thing, you'll see commands for doing various things. On my Rice Lake indicator for example, to zero it, I type "KZERO" and hit enter. I appears looking over the link you provided, the Freightmaster might not TAKE any commands, but will constantly stream it's weight data to you (see below).

    If your scale/indicator is in any type of "streaming" mode (again, look in the manual for how to get it there), you'll probably see a column of weight values as well endlessly pouring down your screen. This is handy since you can use these modes to build a constantly streaming weight indicator control for your program.

    That's the basics, post your questions and I'd be happy to answer them. I've done a lot of scale programming in VB.NET.
    Last edited by Jenner; Oct 28th, 2010 at 08:10 AM.
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  5. #5
    Member
    Join Date
    Aug 2010
    Posts
    54

    Re: RS232 - COM1, very noobie stuff

    Jenner, I have used the hyperterminal type example from

    http://home.comcast.net/~hardandsoft...opSerialIO.htm

    I can successfully query my indicator by using the keyboard shortcut CTRL + G. I have stepped through the code, and serialport.Output(e.KeyChar) = ""c. But how would I do that in code?

  6. #6
    Member
    Join Date
    Aug 2010
    Posts
    54

    Re: RS232 - COM1, very noobie stuff

    btw there was a square symbol in the middle of those two quotes

  7. #7
    Fanatic Member
    Join Date
    Sep 2009
    Location
    Lakewood, Colorado
    Posts
    621

    Re: RS232 - COM1, very noobie stuff

    I have several scale examples in my book, Visual Basic Programmer's Guide to Serial Communications 4. It seems that every scale manufaturer on earth invents his/her own embedded protocol.

    From the protocol that you posted, there seems to be nothing required to start the data transfer. Just open the port at 2400 bps. Then, start receiving and parsing data (the DataReceived event is OK for this, but if you want to display data, you would then need to call a Delegate routing to interact with the UI).

    I'd start here. This is a code example that I include in my book for the scale with which you are working. This example is not complete, in that it does not include decoding for the 3 bit-mapped fields XXX, which you will want to incorporate

    Code:
    Imports System.Text
    Public Class Form1
        Private Buffer() As Byte
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            With SerialPort1
                .PortName = "Com3"   'or whatever portname should be used for your system
                .BaudRate = 2400     'the other settings can stay at default
                .RtsEnable = True
                .Open()
            End With
        End Sub
    
        Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
            'this code contains no error handling
            Static BufferLength As Integer
            With SerialPort1
                Dim BytesToRead As Integer = .BytesToRead
                Dim DataPart(BytesToRead) As Byte
                .Read(DataPart, 0, BytesToRead)
                ReDim Preserve Buffer(BufferLength + BytesToRead - 1)
                Array.Copy(DataPart, 0, Buffer, BufferLength, BytesToRead)
                Dim PacketStart As Integer = -1
                Dim PacketEnd As Integer
                Dim CompletePacket As Boolean
                For I As Integer = 0 To Buffer.Length - 1
                    If Buffer(I) = 2 Then PacketStart = I
                    If Buffer(I) = 13 Then PacketEnd = I
                    If PacketStart >= 0 AndAlso PacketEnd > 0 Then
                        CompletePacket = True
                        Exit For
                    End If
                Next
                If CompletePacket = True Then
                    Dim FullPacket(9) As Byte
                    Array.Copy(Buffer, PacketStart, FullPacket, 0, 10)
                    Array.Copy(Buffer, PacketEnd + 1, Buffer, 0, Buffer.Length - PacketEnd - 1)
                    BufferLength = Buffer.Length - PacketEnd - 1
                    ReDim Preserve Buffer(BufferLength - 1)
                    ParseFullPacket(FullPacket)
                Else
                    BufferLength = Buffer.Length
                End If
            End With
        End Sub
    
        Private Function ParseFullPacket(ByVal Packet() As Byte) As Boolean
            Dim FirstByte As Byte = Packet(0)
            Dim SecondByte As Byte = Packet(1)
            Dim ThirdByte As Byte = Packet(2)
            Dim AsciiEncoding As New ASCIIEncoding
            Dim Reading As String = AsciiEncoding.GetString(Packet, 3, 6)
            'Reading is the actual ASCII text of the reading
            'FirstByte, SecondByte, and ThirdByte are bit-mapped, and may be decoded
            'to extract more information from the packet, if needed: code to be added later
            Me.BeginInvoke(New DisplayData(AddressOf Display), Reading)
            Return True
        End Function
    
        Private Delegate Sub DisplayData(ByRef Buffer As String)
        Private Sub Display(ByRef Buffer As String)
            TextBox1.Text = Buffer
        End Sub
    
    End Class
    Now, you might ask, "Why is this code so complicated?" The reason is because of the bit-mapped fields, XXX, which are binary, not text (the remainder of the weight packet is ASCII text). To reliably decode these values, all of the receive data should be treated as binary, and the text portion extracted separately.

    BTW, there is more than one way to code this protocol, so this isn't "the only way." However, it should get you started.

    Dick
    Richard Grier, Consultant, Hard & Software
    Microsoft MVP (Visual Basic)

  8. #8

    Thread Starter
    Member intellilogic's Avatar
    Join Date
    Oct 2010
    Location
    Tyson's Corner, VA
    Posts
    39

    Re: RS232 - COM1, very noobie stuff

    Manual says nothing about Serial connection, I had to find this document from digging on the internet.
    I tried to connect with putty on COM1 and COM2

    Cursors stays solid, can't type, I don't think I am connecting. Should I check settings in BIOS?

  9. #9
    Fanatic Member
    Join Date
    Sep 2009
    Location
    Lakewood, Colorado
    Posts
    621

    Re: RS232 - COM1, very noobie stuff

    I don't understand what you have posted. Do you mean that you are trying to use the TTY application that you have posted about, and it is failing? I cannot help you there, though you should change a couple of things. Flow control should be set to none, and Stop bits set to 1. Naturally, Com1 may or may not be correct, so you must select the port that you are using.

    If you are talkjng about my code, then you need to put in a Debug.Print statement in the DataReceived event, to see if you are receiving data. Also, you must open the correct port -- are you doing so?

    More information is needed.

    Dick
    Richard Grier, Consultant, Hard & Software
    Microsoft MVP (Visual Basic)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width