Anyone know whats wrong with this code


Closed Thread
Results 1 to 11 of 11
  1. #1
    Join Date
    Dec 2004
    Location
    Scarborough UK
    Posts
    77

    Angry Anyone know whats wrong with this code

    Hi All

    This is a direct cut/paste of the code snipet that comes with VB Express edition for serial comms, it doesnt work, it locks the prog up and can only be stopped with ctrl/alt/del.
    After some investigation i found that its the RX part thats wrong, the TX part sends the byte "S" out the serial port.
    All i want to do is send "S" out the port to my PIC and my PIC sends a string of chars back (which i want to display in a text box) this should not be too hard but im starting to bang my head on the wall, know what i mean ?.
    Any help wil be apreciated.

    '--------------------------------------------------------------------------

    Using comPort As SerialPort = My.Computer.Ports.OpenSerialPort("COM1", 9600)
    comPort.DtrEnable = True
    comPort.Write("S")

    ' All data transfer code goes here.

    End Using

    'All the above works
    '------------------------------------------------------------------------
    'All below does not work

    Dim buffer As New StringBuilder()
    Using comPort As SerialPort = My.Computer.Ports.OpenSerialPort("COM1")
    Do
    Dim line As String = comPort.ReadLine()
    If line Is Nothing Then
    Exit Do
    Else
    buffer.AppendLine(line)
    End If
    Loop
    End Using
    '--------------------------------------------------------------------------
    Reading the datasheet & understanding it are two different things.

  2. #2
    Join Date
    Feb 2003
    Posts
    432

    Default

    I dont use VB Express (Im still on VB6) so wasnt sure what comport.Readline() did. Obviously it reads data from the comport but the "Readline" bit was what I wondered about.

    A quick google pointed me at this page which although about c# does use the same methods

    http://www.codeproject.com/csharp/Se...munication.asp

    This bit is interesting.....
    ReadLine(): Reads up to the NewLine value in the input buffer. If a New Line is not found before timeout, this method returns a null value.

    Does the PIC return a string terminating with CR or LF ?

    In VB6 there is a command "Do Events" which allows the program to do other things as well, maybe your program is reading nothing from the comport and getting stuck in an infinate loop.
    Keith

    www.diyha.co.uk
    www.kat5.tv

  3. #3
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898

    Default

    I don't know nothing about VBExpress, but i feel it's like VB6, so you should be able to use COMevents instead of a loop... which is certainely the killer situation.

    If VBExpress don't have any ComEvents, you should be able to insert a Timer and check the serial com in a Timer interrupt.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  4. #4
    Join Date
    Dec 2004
    Location
    Scarborough UK
    Posts
    77

    Unhappy

    Hi
    Thanks for your reply

    After MUCH searching on google and microsoft website and diffrent forums it is becoming plain that many people are having this problem, i searched this forum and found people with the same problem, BUT I can find no REAL answer that works, only suggestions.

    Even the code on the MS website does not seem to work?????.

    Some of the examples i have found that do work are so complex that i will never figure out how to do it, all i need is 3 or 4 line simple code snip that realy works.

    Does anyone have a real code snip that realy works ???
    Reading the datasheet & understanding it are two different things.

  5. #5
    Join Date
    Feb 2003
    Posts
    432

    Default

    Quote Originally Posted by Bonxy View Post
    After MUCH searching on google and microsoft website and diffrent forums it is becoming plain that many people are having this problem, i searched this forum and found people with the same problem, BUT I can find no REAL answer that works, only suggestions.
    As I said earlier, I have no experience of VB Express.

    I tried VB.net when it first came out and couldnt get any of my existing VB programs to convert to dotNet as they all contained COM ports.

    I had the same lack of success with ASP.net as well. I had written many web based applications in good old ASP but someone higher up at work decided that our latest project should be written in ASP.net as a "learning opportunity" despite having a tight deadline.

    I knocked up a rough and ready but fully functional demo of the application using ASP in about half a day and after the functionality and screen layout was approved I set about writing the "proper" application in ASP.net. As it was a learning curve it obviously took longer but most of the functionallity was there within a couple of weeks. One bit however was impossible to get working in ASP.net despite about 10 people in the team all trying to find out ways of achieving it.

    I dont know why they have to keep changing things when there was nothing wrong with the previous way of doing it.

    If it aint broke..... dont try to fix it!!!

    Keith .... who is probably 2-3 years away from having a PC running Vista.
    Keith

    www.diyha.co.uk
    www.kat5.tv

  6. #6
    Join Date
    Dec 2004
    Location
    Scarborough UK
    Posts
    77

    Unhappy

    Hi

    I agree, good old vb5 worked for me, but it doesnt work now with winxp.
    I've bee trying for 3 days now to get a simple comms in/out prog to work with vb express but its realy doing my head in now.
    Someone must know how to do a simple comms prog with this express version, personaly i find the syntax quite insane to understand (but thats just me).
    Reading the datasheet & understanding it are two different things.

  7. #7
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    947

    Default

    Bonxy

    First of all, I must clarify, I haven't used VB express till now. But, on the face of it, you need to do something differently.

    Code:
    '--------------------------------------------------------------------------
    
    Using comPort As SerialPort = My.Computer.Ports.OpenSerialPort("COM1", 9600)
    comPort.DtrEnable = True
    comPort.Write("S")
    
    ' All data transfer code goes here.
    
    End Using
    
    'All the above works
    '------------------------------------------------------------------------
    'All below does not work
    
    Dim buffer As New StringBuilder()
    Using comPort As SerialPort = My.Computer.Ports.OpenSerialPort("COM1")
    Do
    Dim line As String = comPort.ReadLine()
    If line Is Nothing Then
    Exit Do
    Else
    buffer.AppendLine(line)
    End If
    Loop
    End Using
    '--------------------------------------------------------------------------
    The above code of yours needs to be modified so that the using block encompasses the entire code. Something like this

    Code:
    '--------------------------------------------------------------------------
    
    Using comPort As SerialPort = My.Computer.Ports.OpenSerialPort("COM1", 9600)
    comPort.DtrEnable = True
    comPort.Write("S")
    
    ' All data transfer code goes here.
    
    'All the above works
    '------------------------------------------------------------------------
    'All below does not work
    
    Dim buffer As New StringBuilder()
    Do
    Dim line As String = comPort.ReadLine()
    If line Is Nothing Then
    Exit Do
    Else
    buffer.AppendLine(line)
    End If
    Loop
    End Using
    '--------------------------------------------------------------------------
    I suspect that the OpenSerialPort method destroys whatever data is held in the receiver buffer uptil that time. So, you should try and see if you can open the serial port at the start of your program and close it at the end of your program. Rest of the time, just use it without the using/endusing clauses.

    Hope this helps

    Jerson

  8. #8
    Join Date
    Dec 2004
    Location
    Scarborough UK
    Posts
    77

    Unhappy

    Hi Jerson

    I tried the mod u suggest but it does not work, it locks the prog up as before.
    I was looking on the MS website and i think i found a clue, I may be placing the code in the wrong place ?.
    The MS example i found uses a command line interface with the code snips place in a code module.
    What i am doing is simply placing a button on a form and putting the code snips in the buttonclick sub.

    The worst thing is that MS doesnt provide a simple working example and i cant find anyone that KNOWS how to do this one simple thing, its driving me bloody nuts.
    Reading the datasheet & understanding it are two different things.

  9. #9
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    947

    Default

    Bonxy

    try the usual way. Like I said, at application startup, do the openserialport method call(without the using keyword). Then when you are about to close the app, close serial port(dont know if theres a method for this). I wouldnt mind giving it a shot, but it will take a couple of days since I am very tied up right now with other assignments.
    Perhaps you could ZIP attach the app for me to look at.

    Jerson

  10. #10
    Join Date
    Dec 2004
    Location
    Scarborough UK
    Posts
    77

    Unhappy

    Jerson

    Thanks for your offer but i have to get this thing working today, i am going to try to find a book that explains it.

    If anyone knows how to do serial comms in VB Express please help.
    Reading the datasheet & understanding it are two different things.

  11. #11
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898

    Default

    Probably harder for sure, but the windows API call method should work.

    How to in VBExpress? i wish i could help better.... i don't know.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

Similar Threads

  1. Nokia COLOR LCD PicBasicPro 2.50a example code
    By skimask in forum Code Examples
    Replies: 49
    Last Post: - 28th September 2011, 01:43
  2. PIC BASIC TLC5940 code
    By eletrokat in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 8th May 2010, 21:01
  3. Manchester coding/decoding Code help
    By financecatalyst in forum Code Examples
    Replies: 0
    Last Post: - 25th August 2009, 19:05
  4. Re-Writing IF-THEN-AND-ENDIF code?
    By jessey in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 18th August 2006, 17:23
  5. Its not always the code that is wrong !!!!
    By keithdoxey in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 19th April 2006, 23:42

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

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