PDA

View Full Version : Anyone know whats wrong with this code



Bonxy
- 8th March 2007, 16:57
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
'--------------------------------------------------------------------------

keithdoxey
- 8th March 2007, 19:23
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/SerialCommunication.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.

mister_e
- 8th March 2007, 19:30
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.

Bonxy
- 8th March 2007, 19:35
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 ???

keithdoxey
- 8th March 2007, 20:59
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.

Bonxy
- 8th March 2007, 21:06
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).

Jerson
- 9th March 2007, 06:44
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.



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

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



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

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

Bonxy
- 9th March 2007, 12:24
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.

Jerson
- 9th March 2007, 12:51
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

Bonxy
- 9th March 2007, 12:57
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.

mister_e
- 9th March 2007, 17:29
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.