Serial Port error while sending message

I am writing to a serial port, (not reading) and I get this error when I want to send data to my micro. The command is just a hex string of 9 characters. To get the micro to accept the command I have to reset the micro then within two seconds send the data. I know that the data is correct structure is correct, as it worked once or twice in the many times I have attempted to send the command. I also know that the device works as I can use cool terminal and send the command and the LED Lites up.

Error Message can’t send image so…

“An error occurred while reading the data from the device.”

The following is the code I used which is from XOJO serial port routine.

If Me.Caption = “Disconnect” Then // Disconnect from the serial device
Me.Caption = “Connect”
DevicesPopupMenu.Enabled = True
DeviceListUpdator.RunMode = Timer.RunModes.Multiple 'turn it on
Else // Connect to the serial device
// Set the serial device to the index of the one chosen in the popup menu
SerialConnection1.Device = SerialDevice.At(DevicesPopupMenu.SelectedRowIndex)
DevicesPopupMenu.Enabled = False
DeviceListUpdator.RunMode = Timer.RunModes.Off
Me.Caption = “Disconnect”
Catch error As IOException
MessageBox(“The selected serial device could not be opened.”)
End Try
End If

Hi Robert,

I’m familiar with the routine you’ve posted. Is SerialConnection1 a SerialConnection object you dragged into the UI, or are you instantiating this programmatically in code? Regardless my real question is if you set the comm attributes of the SerialConnection correctly, to match the settings you used in CoolTerm.

Got to look at the settings of both the coolterm and XOJO and the only difference I see is that the coolterm has “software supported flow control” check on and “Block keystrokes while flow is halted” set to on. I put those to off and there is no difference. The micro LED turns on with coolterm but not the XOJO program.

I see your code (above) for Opening the serial connection, can you share the code you use for sending the command/request to the serial device?

Here is that code.

//var LED_ON As int //= 5403010000000000A8
var LED_ON_Hex As String
var Start as Int8 = 84 // Equ of 54 hex Indicates we want to write to EEPROM
var Start_Hex As String
var EEPROM_Location As int8 = 3
var EEPROM_Location_Hex as String
var LED_Set as Int8 = 1
var LED_Set_Hex as String
Var Double_00 As Int8 = 00
Var Double_00_Hex As String
var Parity As Int8 = 168 // Already know what the parity is for this function so don’t need to calculate it.
var Parity_Hex As String

Start_Hex = Start.ToHex(2)
EEPROM_Location_Hex = EEPROM_Location.ToHex(2)
LED_Set_Hex = LED_Set.ToHex(2)
Double_00_Hex = Double_00.ToHex(2)
Parity_Hex = Parity.ToHex(2)

LED_ON_Hex = (Start_Hex + " " + EEPROM_Location_Hex + " " + LED_Set_Hex + " " + Double_00_Hex + " " + Double_00_Hex + " " + Double_00_Hex + " " + Double_00_Hex + " " + Double_00_Hex + " " + Parity_Hex)


I should maybe said that I get the error message when I reset the micro

If you were to capture “DecodeHex(Parity_Hex)” out to a string you could compare it to the string you are sending in Coolterm. I know Coolterm will let you put in an ASCII string, and then click on Hex and it’ll convert it right in the UI so you can copy it back out (if you are sending ASCII chars to the micro and want to confirm you are doing like-for-like).

In coolterm I use the send string command and click the hex values I want and send it, that message looks like this “54 03 01 00 00 00 00 00 A8”. I can verify the message, but with the XOJO program I cannot even get the command to work. As soon as I reset the micro to accept the message to use the EEPROM I get the serial port error message that says there “An error occurred while reading the data from the device.” What I am thinking is that upon reset the micro sends data to the serial port and XOJO cannot interpet the message, whereas the coolterm ignores the message.

Maybe put a line just above your Write command like this…



…and grab to content of the string from the Xojo console for analysis.

Ok will test it out this afternoon,

That is the key piece of information that was missing. You need to be able to detect when your connection has been closed (by resetting the micro) and reconnect. I don’t have any code in front of me, but do you get an error event on the serial connection when you reset the micro?

yes I get a pop up with the following message
“An error occurred while reading the data from the device.”

Where does that popup come from? I wouldn’t expect Xojo to issue such a message. It should either raise an event or raise an exception.

The first thing I do is connect to the serial port and I don’t see errors when I do that. Once the serial port is connect I have a button that I push to run the routine that writes to the micro. But before I can write to the micro I have to reset it to go into the mode that writes to the EEPROM of the micro and that is when I get the error popup.

I found where the error is coming from, I am unable to upload an image as that would be the simplest, I will try and describe where it is at, (sorry not very familiar with XOJO). Anyway there are two library’s I believe it is for the “system” as they are on the bottom of the window1, one is the DeviceListUpdator the other is the SerialConnection1 the error is coming from the SerialConnection1 device. There are only two lines of code in that section they are the following.

MessageBox(“An error occured while reading data from the device.”)

So the message is being generated here, I am assuming I will need some code here to read what is coming in no matter if I use it or not.

If you need to reset the device, then in the button that sends the data you should

Open the connection
Send the data
Flush the connection
Close the connection

Don’t try to hold the serial connection open. Your use case does not support that.

thanks for the help, will work on the additions.