Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27554

ReadTimeout not Working?

$
0
0
I am communicating with an Arduino microcontroller board which is running a simple C program which echoes input on the serial port back to a PC. When the application on the PC sends the contents of a textbox to the Arduino, the code on the Arduino reads the port until it intercepts a CR and then echoes this input back to the PC, adding an LF in the process.

Now when I use a ReadLine() method, it times out straight away, even though the timeout is set to 2 seconds. The LF code is being transmitted as I have checked it using a terminal program. I can see the TX and RX LEDs lighting on the Arduino board so transmission and reception occurs immediately without a delay. The ReadLine method should block until a terminator character is received or it times out. The odd thing is that I can use two methods of sending text to the Arduino board, type text in a textbox and press a button to transmit or type text in the same textbox and hit enter to transmit. Pressing the button doesn't create a timeout but hitting enter does. AcceptsReturn is set to true for the textbox and I have set the multline property to false so as not to confound the issue. The port is a USB virtual serial port.

Any ideas? The Arduino C and VB code is below:

#define CR 13
#define LF 10

int button_input =12;
int led_output =7;
int onboard_led =13;
char received_command[50];
byte num_chars_received;
int char_received;


// Arduino program. This echoes input on the serial port
void setup()
{
pinMode(onboard_led,OUTPUT);
pinMode(button_input,INPUT_PULLUP);
pinMode(led_output,OUTPUT);

int i;

Serial.begin(9600); // Set the baud rate to 9600

}

void loop()
{
if(digitalRead(button_input)) // A high on a digital input allows the program to execute
{
if (Serial.available() > 0) // If there is data in the buffer
{
num_chars_received = Serial.readBytesUntil(CR, received_command, 25); // read until CR terminator. CR not inclued in number of characters received
if(num_chars_received>0)
{
received_command[num_chars_received]='\0'; // terminate the string with a NULL
Serial.println(received_command); // This writes to the serial port and adds an LF
digitalWrite(led_output,HIGH); // Pulse LED on and off
delay(500);
digitalWrite(led_output,LOW);
}
}
}
}



// Event handler for received data
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived

Dim myArray(1) As Object

myArray(0) = " "

SerialPort1.ReadTimeout = 2000

If told_to_close = True Then
okToCloseSerialPort.Set() 'got the close message // set a wait handle.
Exit Sub
End If

timeout = False
Try
myArray(1) = SerialPort1.BytesToRead
myArray(0) = SerialPort1.ReadLine() + vbLf // ReadLine strips the LF when reading from the buffer so add an LF to generate a newline in the text box

Catch ex As TimeoutException
timeout = True
Beep()
End Try


' use a delegate for updating controls to avoid cross threading
iasync1 = Me.BeginInvoke(DelegateDataReceived, myArray)

End Sub



// Transmit the contents of a textbox when a "send" button is clicked
Private Sub btnSendCommand_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSendCommand.Click
If SerialPort1.IsOpen = True Then
SerialPort1.Write(txtBoxSendData.Text & vbCr) // Add a CR terminator for the benefit of the Arduino
txtBoxSendData.Text = "" // blank the textbox
txtBoxSendData.Focus()
End If
End Sub

// Transmit the contents of a textbox when enter key is pressed
Private Sub txtBoxSendData_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtBoxSendData.KeyPress
If e.KeyChar = vbCr Then // Send data if enter key is pressed
If SerialPort1.IsOpen = True Then
SerialPort1.Write(txtBoxSendData.Text & vbCr)
txtBoxSendData.Text = ""
txtBoxSendData.Focus()
End If
End If

End Sub

Viewing all articles
Browse latest Browse all 27554

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>