Shop OBEX P1 Docs P2 Docs Learn Events
String To COM PORT on Eddie in C# — Parallax Forums

String To COM PORT on Eddie in C#

T3rr0rByte13T3rr0rByte13 Posts: 29
edited 2014-02-08 16:46 in Robotics
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.
[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

  • T3rr0rByte13T3rr0rByte13 Posts: 29
    edited 2014-02-05 11:19
    I guess my question might make more sense if I asked how the Parallax serial terminal sends its commands to the Eddie control board? Would I need to break down the string into each byte and then send it one at a time? how does the Parallax serial terminal work?
  • bomberbomber Posts: 297
    edited 2014-02-05 14:30
    T3rr0rByte,
    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.
  • T3rr0rByte13T3rr0rByte13 Posts: 29
    edited 2014-02-08 15:31
    Thanks for the tip again Bomber! Most Useful.

    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.
    command = ("TRVL " + HexDistance + " " + HexPower + "\r\n")
    byte[] bytes = ASCIIEncoding.ASCII.GetBytes(command);
    ComPort.Write(bytes, 0, bytes.Length);
    
  • T3rr0rByte13T3rr0rByte13 Posts: 29
    edited 2014-02-08 16:46
    Correction: The "\r\n" should be replaced with "\r". If you use the \n it will produce an error the next time you enter a command. Entering "\r" is the carriage return.
Sign In or Register to comment.