There are so many options for reading and writing data to and from the serial port that it really becomes application dependent. I know how difficult it can be to choose the right examples and to avoid mistakes but its a great self teaching experience.
SerialPort1.Read(InPacket, 0, BytesToRead) ' read in a packet If BytesToRead > 2000 Then BytesToRead = 2000 If SerialPort1.BytesToRead = 0 Then Exit Do ' no more bytes
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tickĭim BytesToRead As Integer ' may as well get in batches
Timer1.Interval = 150 ' check for new characters every 150msĤ) a timer - and you need to disable the timer while the timer does things in case it causes reentry code Imports Strings = Microsoft.VisualBasic ' so can use things like left( and right( for stringsĢ) for reading bytes, a buffer at the beginning of frmmainģ) if you get sick of typing in the com port every time you start - put it in a startup formĪnd you can set the timer interval here too Can someone help get this working with my current code?įollow me on Twitter! Search "Microcontrolled" I need to have a function that waits until a serial signal is received in the USB port and then stores the following string of bytes into a String. SerialPort.DiscardInBuffer() ' clear the input buffer SerialPort.ReadTimeout = 1000 ' milliseconds so times out in 1 second if no response Port = InputBox("Select the COM port you wish to open","COM port","COM9") Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load I have this code (as a template):ĭim WithEvents SerialPort As New IO.Ports.SerialPort ' serial port declare (don't worry, there is still be plenty of coding to do that sample does not interpret the NMEA, it just assembles the lines from the incoming data and emits them for something else to handle).I have tried many options for this, but can come up with nothing that is compatable with my current setup. Maybe that sample will be useful in some way: Lastly, someone else had a serial port question, and coincidentally they were using a GPS, and also coincidentally I wrote some code that does the serial stuff of reading and emitting the NMEA lines. (some folks have it happen as soon as they do ethernet in another thread, and for me it just seems to be the number of threads while running several serial ports). I seems to be somehow related to how much other stuff you've got going on in your app. That being said, I do want to mention that I (and others) have had problems with DataReceived ceasing to function inexplicably after some time. This seems to be how you interpreted it yourself, so maybe look for some bugs in managing your write index into your application buffer, or maybe in how you are pulling lines and and doing the bookkeeping of that pointer. You need to retrieve all that is available, because you will not get another event fired until a new received byte comes in (i.e., merely having some unread data will not stimulate the event). The DataReceived is meant to be fired when there is data available in the internal buffers, that you can retrieve with Read(). Hi Jamo your handler looks sensible to my eyes, though I haven't run the code. It could be that the setup is causing an issue but I'm not sure if that is the case or where to look. My Netduino 1 Tx and Rx are connected to a small circuit that coverts a single wire serial GPS. I have a serial port read GPS signals but it also fills up the buffer with 0's. Can anyone explain how the SerialPort.DataReceived works?