VB Question -

I am reading the serial port in VB for an output from the Arduino

however sometimes the displayed information is not as neat as the Arduino Sreial Monitor and I get extra charachters

I am using "SerialPort1.ReadLine"

should I be using something else ?

Code is as below


Imports System

Imports System.IO.Ports

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

SerialPort1 = New SerialPort("COM6", 9600, Parity.None, 8, StopBits.One)
'SerialPort1.Open()

End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

If Not SerialPort1.IsOpen Then SerialPort1.Open()
'TextBox1.Text = ""
TextBox1.Text = SerialPort1.ReadLine
SerialPort1.Close()

End Sub
End Class

Have you modified your Arduino to prevent reset on opening and closing the serial port? If not, the Arduino resets every time you open and close the serial port, which you do every time the timer ticks.

You should open and close the serial port once, and read from the port whenever there is data, not whenever the timer ticks.

spot on - works a treat

I was thinking closing the port was enabling it for other applications, but in reality it is only this one reading the port


qu - is it possible for more than one application to read the serial port ?

could you get the arduino to transmit on more than one port ?

qu - is it possible for more than one application to read the serial port ?

If the serial port is closed by one application (resetting the Arduino), another application can open it (again resetting the Arduino).

could you get the arduino to transmit on more than one port ?

Yes, you can send and receive data on each of the USB ports on your Arduino.

I am struggling to get the arduino to work consistantly

if i plug and unplug it, it does not always transmist data

however if i open the serial monitor, it seems to somehow reset the device and it starts to read again

is there a procedure i should be following to make sure it will communicate ok when i plug it in

also is there a way to change the name of the board so it does not appear named as an arduino in the device manager

(is there a sample vb program that sets the serial ports up correctly)

ty

I am struggling to get the arduino to work consistantly

I'm sure that it IS working consistently. But whether that consistency is in line with your expectations, or not, is a different issue.

if i plug and unplug it, it does not always transmist data

Again, I think that it does. Whether the receiver, whatever that is, properly connects to the serial port, and receives that data, or not, is a different issue.

however if i open the serial monitor, it seems to somehow reset the device and it starts to read again

Opening the serial monitor opens the serial port. Any time that happens, the Arduino resets, unless it has been modified to not reset. The second "it" in that sentence, though, is not properly bound to a noun, so I have no idea what starts to read what again.

is there a procedure i should be following to make sure it will communicate ok when i plug it in

The Arduino always communicates OK when plugged in. Communication is a two way street, though. The other end may be where the issue is.

also is there a way to change the name of the board so it does not appear named as an arduino in the device manager

In theory, yes, if you have a UNO or a Mega 2560. If you have any other Arduino, no. There is a big gap between theory and practice, however. Why do you think you need to change the name that the PC shows?

(is there a sample vb program that sets the serial ports up correctly)

Yes.

Hi Paul,

thanks for the reply,

You mentioned there is some vb code that sets up the port in the correct manner - do you know where this is ?

my arduino is transmitting data to the serial port when a button is pressed. The tx button flashes every time it transmits data
however sometimes when you plug it into the usb port it does not flash the tx button and the program reading the serial port will crash or hang when trying to read the port

is there a preffered method to find the correct serial port automatically without the user selecting it

the arduino board may end up getting rebuilt to my own spec in the future and i would like the product name to be displayed in the device manager not "arduino"

regards

however sometimes when you plug it into the usb port it does not flash the tx button and the program reading the serial port will crash or hang when trying to read the port

There should not be an application already running/connected to the serial port when the Arduino is plugged in. If there is, that application won’t properly reconnect to the serial port.

is there a preffered method to find the correct serial port automatically without the user selecting it

The preferred method IS to have the user select the port. Might not be what you prefer. You could have the Arduino send “Hey there” every 5 seconds. Then, your VB application could connect to each serial port, and wait up to 6 seconds for “Hey there” to appear. If it doesn’t, close that port, and try the next one. If it does, then you found the right port.

Of course, then you’d probably want to send a message to the Arduino telling it to stop with the "Hey there"s…

the arduino board may end up getting rebuilt to my own spec in the future and i would like the product name to be displayed in the device manager not “arduino”

When you put the USB interface chip on your board, you can program it however you like. I’m thinking that doing that is currently beyond your comfort zone, though.

it appears software hanging when accessing the serial port is a common thing !

http://social.msdn.microsoft.com/forums/en-US/netfxbcl/thread/68d3e00b-aae7-40db-afba-8a4ee7b210e2/

sounds like there is some kind of deadlock which causes it to hang / crash

i might give firmata a go and see if the problems can be resolved that way

if anyone is interested - I got to the bottom of the problem

VB/VC++ is event driven and the serial port uses multithreading

it is possible for the GUi to end up deadlocked when trying to communicate

setting CheckForIllegalCrossThreadCalls = False will allow you to ignore any deadlocks and continue

this code will work

____________________________________________________________-

Imports System
Imports System.IO.Ports

Public Class Form1
Dim WithEvents port As SerialPort = New System.IO.Ports.SerialPort("COM6", 9600, Parity.None, 8, StopBits.One)

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
CheckForIllegalCrossThreadCalls = False
If port.IsOpen = False Then port.Open()
End Sub

Private Sub port_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles port.DataReceived
TextBox1.Text = (port.ReadLine) ' ReadTo("!%"))
If port.ReadExisting.Length = 0 Then
ListBox1.Items.Add(TextBox1.Text)
'TextBox1.Text = ""
End If
End Sub
End Class

In C#, you can't disable the illegal cross thread calls checking. You must properly define delegates to handle the serial thread communication with the GUI thread. I'd be surprised if that could not be done in C++. Don''t know about VB. Disabling the check, thus allowing one thread to use resources that belong to another thread seems like risky behavior to me.