String To COM PORT on Eddie in C#
T3rr0rByte13
Posts: 29
Hi I am trying to send a command string (TRVL 96 14 (This is what the command variable will read after having the values converted to hex in the below code)) from a C# program to the Eddie by way of a serial port.
The command works correctly when sending over the parallax serial terminal. My code is below.
Thanks for any insight.
The command works correctly when sending over the parallax serial terminal. My code is below.
[FONT=Consolas][SIZE=2][COLOR=#020fc0][FONT=Consolas][SIZE=2][COLOR=#020fc0][FONT=Consolas][SIZE=2][COLOR=#020fc0]public [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#020fc0][FONT=Consolas][SIZE=2][COLOR=#020fc0][FONT=Consolas][SIZE=2][COLOR=#020fc0]void[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] OpenComPort()[/SIZE][/FONT][/SIZE][/FONT] [FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] { [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] ComPort = [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#020fc0][FONT=Consolas][SIZE=2][COLOR=#020fc0][FONT=Consolas][SIZE=2][COLOR=#020fc0]new[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af]SerialPort[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]([/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#a31515][FONT=Consolas][SIZE=2][COLOR=#a31515][FONT=Consolas][SIZE=2][COLOR=#a31515]"COM8"[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2], 115200, [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af]Parity[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2].None, 8, [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af]StopBits[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2].One);[/SIZE][/FONT][/SIZE][/FONT] [FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] ComPort.Handshake = [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af]Handshake[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2].None;[/SIZE][/FONT][/SIZE][/FONT] [FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] ComPort.DataReceived += [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#020fc0][FONT=Consolas][SIZE=2][COLOR=#020fc0][FONT=Consolas][SIZE=2][COLOR=#020fc0]new [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af]SerialDataReceivedEventHandler[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2](DataRecieved);[/SIZE][/FONT][/SIZE][/FONT] [FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] ComPort.Open(); } [/SIZE][/FONT][/SIZE][/FONT] [FONT=Consolas][SIZE=2][COLOR=#020fc0][FONT=Consolas][SIZE=2][COLOR=#020fc0][FONT=Consolas][SIZE=2][COLOR=#020fc0]public[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#020fc0][FONT=Consolas][SIZE=2][COLOR=#020fc0][FONT=Consolas][SIZE=2][COLOR=#020fc0] void[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] DriveDistance([/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#020fc0][FONT=Consolas][SIZE=2][COLOR=#020fc0][FONT=Consolas][SIZE=2][COLOR=#020fc0]int[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] Distance, [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#020fc0][FONT=Consolas][SIZE=2][COLOR=#020fc0][FONT=Consolas][SIZE=2][COLOR=#020fc0]int[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] Power)[/SIZE][/FONT][/SIZE][/FONT] [FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] { [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#020fc0][FONT=Consolas][SIZE=2][COLOR=#020fc0][FONT=Consolas][SIZE=2][COLOR=#020fc0]if[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] ((Power > 127) || (Power < 1))[/SIZE][/FONT][/SIZE][/FONT] [FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] { [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af]MessageBox[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2].Show([/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#a31515][FONT=Consolas][SIZE=2][COLOR=#a31515][FONT=Consolas][SIZE=2][COLOR=#a31515]"Incorrect Power value input"[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]);[/SIZE][/FONT][/SIZE][/FONT] [FONT=Consolas][SIZE=2][COLOR=#020fc0][FONT=Consolas][SIZE=2][COLOR=#020fc0][FONT=Consolas][SIZE=2][COLOR=#020fc0] return[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]; }[/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#020fc0][FONT=Consolas][SIZE=2][COLOR=#020fc0][FONT=Consolas][SIZE=2][COLOR=#020fc0]else[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#020fc0][FONT=Consolas][SIZE=2][COLOR=#020fc0][FONT=Consolas][SIZE=2][COLOR=#020fc0] if[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] ((Distance > 32767) || (Distance < 1))[/SIZE][/FONT][/SIZE][/FONT] [FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] { [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af]MessageBox[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2].Show([/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#a31515][FONT=Consolas][SIZE=2][COLOR=#a31515][FONT=Consolas][SIZE=2][COLOR=#a31515]"Incorrect Distance value input"[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]);[/SIZE][/FONT][/SIZE][/FONT] [FONT=Consolas][SIZE=2][COLOR=#020fc0][FONT=Consolas][SIZE=2][COLOR=#020fc0][FONT=Consolas][SIZE=2][COLOR=#020fc0] return[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2];[/SIZE][/FONT][/SIZE][/FONT] [FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] } [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#020fc0][FONT=Consolas][SIZE=2][COLOR=#020fc0][FONT=Consolas][SIZE=2][COLOR=#020fc0] string[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] command;[/SIZE][/FONT][/SIZE][/FONT] [FONT=Consolas][SIZE=2][COLOR=#020fc0][FONT=Consolas][SIZE=2][COLOR=#020fc0][FONT=Consolas][SIZE=2][COLOR=#020fc0] string[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] HexPower = ConvertToHex(Power);[/SIZE][/FONT][/SIZE][/FONT] [FONT=Consolas][SIZE=2][COLOR=#020fc0][FONT=Consolas][SIZE=2][COLOR=#020fc0][FONT=Consolas][SIZE=2][COLOR=#020fc0] string[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] HexDistance = ConvertToHex(Distance);[/SIZE][/FONT][/SIZE][/FONT] [FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] command = ([/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#a31515][FONT=Consolas][SIZE=2][COLOR=#a31515][FONT=Consolas][SIZE=2][COLOR=#a31515]"TRVL "[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] + HexDistance + [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#a31515][FONT=Consolas][SIZE=2][COLOR=#a31515][FONT=Consolas][SIZE=2][COLOR=#a31515]" "[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] + HexPower);[/SIZE][/FONT][/SIZE][/FONT] [FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af] MessageBox[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2].Show(command);[/SIZE][/FONT][/SIZE][/FONT] [FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] ComPort.Write(command); } [/SIZE][/FONT][/SIZE][/FONT]
Thanks for any insight.
Comments
Something to keep in mind is that you have to send a carriage return after each command for Eddie's Firmware to actually run the command. The carriage return's ASCII value is 13 BTW. In my Python code that I use for serial communication, I convert the string into UTF-8 before sending it. I am not sure if c# does that automatically or not.
The trick was to convert the ascii string into a byte array before sending it. I also needed to add \r\n to the end of the command string before getting the bytes, this was for the carriage return.