Results 1 to 6 of 6

Thread: serial port problem in published version

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2007
    Posts
    7

    serial port problem in published version

    I have a VB2005 project I have been working on that uses the built in modem of my laptop to interrogate a piece of equipment. This works fine in debugging mode but if I try to publish it the modem drops off almost immediately.

    Would anyone have any idea why this should be happening and how I might fix it?

  2. #2
    Fanatic Member Andy_P's Avatar
    Join Date
    May 2005
    Location
    Dunstable, England
    Posts
    669

    Re: serial port problem in published version

    Welcome to the forums!

    A few questions...

    What do you mean 'drops off'?

    What happens if you run the project in Debug or Release modes through the IDE?

    Are you running the published app on your laptop or on another PC?

    If it's on another one, is the COM port number the same?


    Oh, and post a bit of code!
    Using Windows XP Home sp3
    Mucking around with C# 2008 Express
    while ( this.deadHorse ) { flog( ); }


  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2007
    Posts
    7

    Re: serial port problem in published version

    Andy

    A little background to this. I am interrogating a piece of equipment which has eight cards with 32 circuits on each and so I have a form with 256 buttons on it and the idea is to give a graphical representation of the state of affairs.

    The modem grabs the line and starts to dial then hangs up. I have since posting this found that I have more problems than I thought. If I connect the serial port directly to the equipment I want to talk to it still has problems but I think that is because it is running faster, I was using readexisting to get the data from the serial port and in debug that worked perfectly every time. I am now trying to sort out a way of using readline but I have yet to pin down what is the end of string character.

    I still don't understand why the modem hangs up though.

    You choose the com port and speed each time so that isn't the problem.

    By the way I am really new to programming like this having only dabbled in VBA previously. I just decided it was time to get away from excel.

    You asked for code so here's a large chunk of it
    vb Code:
    1. Private Sub Command25_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Command25.Click
    2.  
    3.         Dim _Color As Color
    4.         Dim counter1 As Integer
    5.         Dim check As String
    6.         Dim hereagain As Integer = 0
    7.         If Me.Command25.Text = "Stop" Then GoTo here1
    8.         My.Application.DoEvents()
    9.         Me.Command25.Text = "Stop"
    10.         Me.Command25.Update()
    11.         Me.Text = "IP-Pad Display. Written by XXXXXXXXXXXXXXXXXXXXXXXX"
    12.         Me.Update()
    13.         With SerialPort1
    14.             .PortName = ListBox1.SelectedItem
    15.             .BaudRate = ListBox2.SelectedItem
    16.             .Parity = IO.Ports.Parity.None
    17.             .DataBits = 8
    18.             .StopBits = IO.Ports.StopBits.One
    19.             .Handshake = IO.Ports.Handshake.RequestToSend
    20.             .DtrEnable = True
    21.             .RtsEnable = True
    22.         End With
    23.         Dim y As Integer
    24.         SerialPort1.Open()
    25.         If TextBox1.Text = "" Then GoTo here3
    26.         SerialPort1.WriteLine("atdt" & TextBox1.Text & vbCr)
    27.         For x = 1 To 1000000000
    28.             For y = 1 To 10
    29.  
    30.             Next
    31.         Next
    32. here3:
    33.         SerialPort1.WriteLine("/F53 0475 ")
    34. here2:
    35.         For x = 1 To 8
    36.             If hereagain > 0 Then
    37.                 SerialPort1.DtrEnable = True
    38.                 If chk = 0 Then SerialPort1.WriteLine("/F53 0475 ")
    39.             End If
    40.             If chk > 0 Then SerialPort1.WriteLine(vbCr)
    41.             Do Until chk = x
    42.                 My.Application.DoEvents()
    43.             Loop
    44.             For counter1 = 1 To 32
    45.                 check = Mid(padst, counter1, 1)
    46.                 If Mid(padst, counter1, 1) = "1" Then _Color = Color.Lime Else _Color = Color.Red
    47.                 CType(findcontrolbyreflection("Button" & (counter1 + (32 * (chk - 1)))), Button).BackColor = _Color
    48.                 CType(findcontrolbyreflection("Button" & (counter1 + (32 * (chk - 1)))), Button).Update()
    49.             Next
    50.  
    51.         Next x
    52.         x = 0
    53.         chk = 0
    54.         hereagain = 1
    55.         GoTo here2
    56. here1:
    57.         SerialPort1.Close()
    58.         Me.Command25.Text = "Start"
    59.         Me.Command25.Update()
    60.         For chk = 1 To 8
    61.             For counter1 = 1 To 32
    62.                 check = Mid(padst, counter1, 1)
    63.                 _Color = Color.Red
    64.                 CType(findcontrolbyreflection("Button" & (counter1 + (32 * (chk - 1)))), Button).BackColor = _Color
    65.                 CType(findcontrolbyreflection("Button" & (counter1 + (32 * (chk - 1)))), Button).Update()
    66.             Next
    67.         Next
    68.         chk = 0
    69.         Me.Text = "IP-Pad Display"
    70.         Me.Update()
    71.     End Sub
    72.  
    73.     Public Function findcontrolbyreflection(ByVal Name As String) As Control
    74.  
    75.  
    76.         Dim info As System.Reflection.FieldInfo = Me.GetType().GetField("_" & Name, _
    77.         System.Reflection.BindingFlags.NonPublic Or _
    78.         System.Reflection.BindingFlags.Instance Or _
    79.         System.Reflection.BindingFlags.Public Or _
    80.         System.Reflection.BindingFlags.IgnoreCase)
    81.  
    82.         If info Is Nothing Then Return Nothing
    83.         Dim o As Object = info.GetValue(Me)
    84.         Return o
    85.  
    86.     End Function
    87.  
    88.  
    89.  
    90.     Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
    91.         SerialPort1.DtrEnable = True
    92.         My.Application.DoEvents()
    93.         Dim counter As Integer
    94.         Dim carstr As String = ""
    95.         Dim teststr As String = ""
    96.         Dim chan24to31 As String = ""
    97.         Dim chan16to23 As String = ""
    98.         Dim chan8to15 As String = ""
    99.         Dim chan0to7 As String = ""
    100.         Dim revstr As Integer
    101.         Dim check As String
    102.         Dim i As Integer = 0
    103.         Dim gotit As Integer = 0
    104.         Control.CheckForIllegalCrossThreadCalls = False
    105.         teststr = SerialPort1.ReadExisting()
    106.         'This cleans out any spurious characters
    107.         For counter = 1 To Len(teststr) - 1
    108.             If Asc(Mid(teststr, counter, 1)) > 31 Then
    109.                 If Asc(Mid(teststr, counter, 1)) < 125 Then
    110.                     Dim debstr As String = Mid(teststr, counter, 1)
    111.                     carstr = carstr & Mid(teststr, counter, 1)
    112.                     debstr = Asc(Mid(teststr, counter, 1))
    113.                 End If
    114.             End If
    115.         Next
    116.         'I then have to strip the hex codes from the end of the string NB. they always follow a ":"
    117.         counter = 0
    118.         For counter = 1 To Len(carstr) - 1
    119.             check = Mid(carstr, counter, 1)
    120.             If Mid(carstr, counter, 1) = ":" Then
    121.                 chk = chk + 1
    122.                 chan24to31 = Microsoft.VisualBasic.Right(carstr, 2)
    123.                 chan16to23 = Mid(carstr, (Len(carstr) - 4), 2)
    124.                 chan8to15 = Mid(carstr, (Len(carstr) - 7), 2)
    125.                 chan0to7 = Mid(carstr, (Len(carstr) - 10), 2)
    126.  
    127.             End If
    128.         Next
    129.         'then convert to Binary and reverse the string of digits so that I can use right to left presentation
    130.         If chk > 0 Then
    131.             i = Convert.ToInt32(chan24to31, 16)
    132.             Dim ind24to31 As String = Convert.ToString(i, 2)
    133.             If Len(ind24to31) = 1 Then ind24to31 = "0000000" & ind24to31
    134.             If Len(ind24to31) = 2 Then ind24to31 = "000000" & ind24to31
    135.             If Len(ind24to31) = 3 Then ind24to31 = "00000" & ind24to31
    136.             If Len(ind24to31) = 4 Then ind24to31 = "0000" & ind24to31
    137.             If Len(ind24to31) = 5 Then ind24to31 = "000" & ind24to31
    138.             If Len(ind24to31) = 6 Then ind24to31 = "00" & ind24to31
    139.             If Len(ind24to31) = 7 Then ind24to31 = "0" & ind24to31
    140.             Dim revind24to31 As String = ""
    141.             For revstr = 8 To 1 Step -1
    142.                 revind24to31 = revind24to31 + Mid(ind24to31, revstr, 1)
    143.             Next
    144.             i = Convert.ToInt32(chan16to23, 16)
    145.             Dim ind16to23 As String = Convert.ToString(i, 2)
    146.             If Len(ind16to23) = 1 Then ind16to23 = "0000000" & ind16to23
    147.             If Len(ind16to23) = 2 Then ind16to23 = "000000" & ind16to23
    148.             If Len(ind16to23) = 3 Then ind16to23 = "00000" & ind16to23
    149.             If Len(ind16to23) = 4 Then ind16to23 = "0000" & ind16to23
    150.             If Len(ind16to23) = 5 Then ind16to23 = "000" & ind16to23
    151.             If Len(ind16to23) = 6 Then ind16to23 = "00" & ind16to23
    152.             If Len(ind16to23) = 7 Then ind16to23 = "0" & ind16to23
    153.             Dim revind16to23 As String = ""
    154.             For revstr = 8 To 1 Step -1
    155.                 revind16to23 = revind16to23 + Mid(ind16to23, revstr, 1)
    156.             Next
    157.             i = Convert.ToInt32(chan8to15, 16)
    158.             Dim ind8to15 As String = Convert.ToString(i, 2)
    159.             If Len(ind8to15) = 1 Then ind8to15 = "0000000" & ind8to15
    160.             If Len(ind8to15) = 2 Then ind8to15 = "000000" & ind8to15
    161.             If Len(ind8to15) = 3 Then ind8to15 = "00000" & ind8to15
    162.             If Len(ind8to15) = 4 Then ind8to15 = "0000" & ind8to15
    163.             If Len(ind8to15) = 5 Then ind8to15 = "000" & ind8to15
    164.             If Len(ind8to15) = 6 Then ind8to15 = "00" & ind8to15
    165.             If Len(ind8to15) = 7 Then ind8to15 = "0" & ind8to15
    166.             Dim revind8to15 As String = ""
    167.             For revstr = 8 To 1 Step -1
    168.                 revind8to15 = revind8to15 + Mid(ind8to15, revstr, 1)
    169.             Next
    170.             i = Convert.ToInt32(chan0to7, 16)
    171.             Dim ind0to7 As String = Convert.ToString(i, 2)
    172.             If Len(ind0to7) = 1 Then ind0to7 = "0000000" & ind0to7
    173.             If Len(ind0to7) = 2 Then ind0to7 = "000000" & ind0to7
    174.             If Len(ind0to7) = 3 Then ind0to7 = "00000" & ind0to7
    175.             If Len(ind0to7) = 4 Then ind0to7 = "0000" & ind0to7
    176.             If Len(ind0to7) = 5 Then ind0to7 = "000" & ind0to7
    177.             If Len(ind0to7) = 6 Then ind0to7 = "00" & ind0to7
    178.             If Len(ind0to7) = 7 Then ind0to7 = "0" & ind0to7
    179.             Dim revind0to7 As String = ""
    180.             For revstr = 8 To 1 Step -1
    181.                 revind0to7 = revind0to7 + Mid(ind0to7, revstr, 1)
    182.             Next
    183.             'put them together to depict all 32 channels
    184.             padst = revind0to7 & revind8to15 & revind16to23 & revind24to31
    185.  
    186.  
    187.  
    188.  
    189.  
    190.  
    191.  
    192.         End If
    193. here1:
    194.     End Sub

  4. #4
    Fanatic Member Andy_P's Avatar
    Join Date
    May 2005
    Location
    Dunstable, England
    Posts
    669

    Re: serial port problem in published version

    Well first of all, try to stop the use of GoTo, and structure your code in a different way, by using If..Then..Else etc, or by running separate subs.. Using GoTo is not recommended for good programming practice because it rapidly leads to spaghetti code and is harder to debug. It's also harder to read.

    You have a really large For..Next loop in there, presumably waiting for the modem to respond before sending some more data. This is not good because it will hang the user interface while the loop is running until it is finished. It would be a lot better to have verything event driven. Set a timer running perhaps that waits for a set length of time, and run subsequent code from the tick event of the timer. Or, restructure the data received event handler to deal with the response from the "atdt..." transmission. When the correct response has been received set a flag and use this to decide what to do with any subsequent data received.

    There is really no need to keep calling .Update() on your controls.

    Do not do this: [CODE]Control.CheckForIllegalCrossThreadCalls = False[\CODE]
    This will lead to problems down the line. Do a search on the forums for 'serial ports' and 'Invoke'. This thread might be useful, specifically post#13:
    http://www.vbforums.com/showthread.p...al+port+invoke

    It might help to separate more code into different subs and functions, mainly your conversion code in the data received event handler. It will make things easier to read and debug.

    Check out some of the '.NET' ways of doing things, especially with your strings. e.g:
    Code:
    Len(ind24to31)
    can be re-written as this:
    Code:
    ind24to31.Length
    In my personal opinion, the 'Object.Method()' syntax is easier to read.

    OK, there is plenty there so I hope some of it helps. Good luck, it sounds like an interesting project.
    Using Windows XP Home sp3
    Mucking around with C# 2008 Express
    while ( this.deadHorse ) { flog( ); }


  5. #5

    Thread Starter
    New Member
    Join Date
    Jul 2007
    Posts
    7

    Re: serial port problem in published version

    It is a challenge as I said I'm not that experienced in this but this is really only a proof of concept piece and as such it was acceptable for it to be rough and ready. If it is useful it will be passed to the professionals to do properly. The only reason I was asked to do this was I didn't have to account for my time on it. I am a faults person and so get periods when there are no faults and I effectively have nothing to do but things like this.

    I tend to write on the fly so that is why there isn't much structure to it.

    Interesting though that you say I don't need the .update () as things weren't changing till I discovered that one. I had reached the conclusion last night that I should handle the modem connection side of things by using the connect event and I believe I can tidy things up on the read of the serial port by using the ":" to trigger the readline event. even though the data I want follows this event as the next time it triggers my data will be at the beginning of the string. Since I am simply looping through the data over and over it shouldn't make any difference.

    This is quite a learning curve for me so thanks for taking the time and trouble to assist.

  6. #6
    Fanatic Member Andy_P's Avatar
    Join Date
    May 2005
    Location
    Dunstable, England
    Posts
    669

    Re: serial port problem in published version

    I think the reason things were/are not changing until you call .Update() is because the program is running those long loops, and therefore not updating the UI until the loop is finished.

    Once the code becomes event driven, it should not be necessary.

    Quote Originally Posted by R4+Z
    I am a faults person and so get periods when there are no faults and I effectively have nothing to do but things like this.
    I know exactly what you mean.
    Using Windows XP Home sp3
    Mucking around with C# 2008 Express
    while ( this.deadHorse ) { flog( ); }


Posting Permissions

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



Click Here to Expand Forum to Full Width