Question Read SERIAL PORT in vs2008

vachhaninimit

Member
Joined
Aug 4, 2010
Messages
5
Programming Experience
Beginner
I working on modbus software. I am able to communicate with the modbus slave. The only problem with me is after i write the message on the serial port , i read the serial port for data after say 100ms and i use "system.threading.thread.sleep(100)". This freezes my program for a while. Can you please help me build the logic for reading the serial port after say n no. of seconds without using "system.threading.thread.sleep". I am new to vb.net. I am using .net 3.5sp1 and vs2008.
Thanks.
 
to avoid freezing your GUI you have to use the "CommsReader" routine in a separate thread.
The easiest way to implement threading in .net is using the BackgroundWorker class.

Also, using a sleep to read the modbus message is just not correct.
Those messages always have a message termination string, the serial port in .net have a ReadTo method, I suggest you check it.
 
Hey Budius,
Thanks for the reply. Well i am totally new to vb.net programming. I searched but was not able to fine commsreader routine in .net3.5 . Well for readto method it reads the commport data into the value specified. Here i know that slave will be sending me 9bytes , so what i do is after writing the data to serial port i wait for response from slave and keep on pooling it till i get 7 bytes and i stop pooling the port after 3 tries. Still not able to figure out. I have used the code =====

Do Until SerialPort1.BytesToRead = 7 Or counter = 3 'Keep on sending message to slave device till we get 7 bytes on the port to read
SerialPort1.Write(flagmessage, 0, flagmessage.Length)
'System.Threading.Thread.Sleep(50)
Application.DoEvents()
counter = counter + 1
Loop
 
sorry, maybe I didn't explain properly.
"CommsReader" I was referring to YOUR rountine, there is nothing in .net to do that.

On your form designer, in the left-hand side, there is the tool-box.
In the Components section you will find there the "BackgroundWorker", which is a class that wraps threading in a easy way.
What I meant is, for you to write your communication code in the BackgroundWorker DoWork event.

I'm assuming that from those 9 bytes, the last or 2 last bytes are some kind of message termination (e.g.: carriage return, line feed, some symbol) so it's just easier and more reliable to set your serial port timeout and ReadTo(termination)

got it ?


ps.: when writing code in the forum use the code tag.
 
Alright Thanks for the info. I will tryout . Well last 2 bytes are the 16bit crc sent by slave device.
well, so you can't use the ReadTo, but I remember there is something like "Read X amount of bytes" ... should be ReadBytes(byval NumberOfBytes) .. I'm not sure here, just guessing.
 
Take a read through of the following. http://www.modbus.org/docs/PI_MBUS_300.pdf

Look at the ASCII and RTU framing. Both have definitions for the start and end of messages. If you are lucky, then you are using the ASCII Message Frame, which always starts with a colon :)) and ends with CrLf.

The SerialPort componet has a DataReceived event where you should be reading the data received and verifying that the message is complete. Which means you should be looking for the colon and CrLF to determine when you have a complete message. Using the event means no thread sleeping or anything else that may impact the performance of your application.
 
Back
Top