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
SerialConnection1.Close
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)
Try
SerialConnection1.Connect
DevicesPopupMenu.Enabled = False
DeviceListUpdator.RunMode = Timer.RunModes.Off
Me.Caption = “Disconnect”
Catch error As IOException
Beep
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)
MessageBox(LED_ON_Hex)

SerialConnection1.Write(DecodeHex(Parity_Hex))
SerialConnection1.Flush()

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…

System.debuglog(DecodeHex(Parity_Hex))

SerialConnection1.Write(DecodeHex(Parity_Hex))

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

Ok will test it out this afternoon,
Thanks

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.

1 Like

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.

System.Beep
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.

Finally, back to trying to figure out why the serial port gives and error and just don’t know. I started over and took the serial read routine example “Serial Device Bar Code Reader Example” changed the port setting to match the device I am talking to. So as I stated before I have to reset the micro to get it to boot up into config mode to send it commands. I push reset and I get and instant popup that is in the error routine. The messagebox has the following “An error occurred while reading the data from the device.” I am also able to use cool terminal and push the reset on the micro and data will come into the window to read. The one thing I did notice in the cool term is that in the settings it Ignores receive signal framing errors, not sure if that has anything to do with it.

What is this “section”? Is it an event handler?

Please re-post all code relating to your SerialConnection, using the forum’s code formatting function so we can read it. Paste your code, select it, then click the “</>” button in the toolbar to automatically format it.

[quote=“Robert Wiser, post:18, topic:74715, username:Robert_Wiser”],Serial Device Bar Code Reader Example
[/quote]

If you bring up the example program Serial Device Bar Code Reader Example you can see everything you need to, as that is how I am starting the code off.