Shop OBEX P1 Docs P2 Docs Learn Events
C# and PBasic code problem — Parallax Forums

C# and PBasic code problem

Jeff RileyJeff Riley Posts: 5
edited 2007-08-08 21:11 in BASIC Stamp
Hello all,

Please forgive me as I am new to PBasic and C#.· I have created the following PBasic and C# code with the intent that the C# code would send the byte code 100, and 101, and get back from the Stamp 2 the text "a" and "b".· All I am getting back is the ASCII conversion from 100 to "d" and 101 to "e".· Any help would be greatly appreciated.

Thank you,
Jeff Riley

PBasic code
' SerialEcho.bs2
' {$STAMP BS2}
' {$PBASIC 2.5}
serialIn VAR Byte(3)
foo····· VAR Byte
main:
· foo = 255
· serialIn(0) = 0
· serialIn(1) = 0
· serialIn(2) = 0
· SERIN 16,16780,1000,main,[noparse][[/noparse]STR serialIn\3\"!"]
· PAUSE 100
· LOOKDOWN serialIn(0), [noparse][[/noparse]100, 101, 102], foo
· BRANCH foo,[noparse][[/noparse]test1, test2, getCompass]
· PAUSE 5
· GOTO main
test1:
· SEROUT 16,16780,[noparse][[/noparse]"a"]
· GOTO main
test2:
· SEROUT 16,16780,[noparse][[/noparse]"b"]
· GOTO main
getCompass:
· SEROUT 16,16780,[noparse][[/noparse]"180"]
· GOTO main

C# code

using
System;

using System.Threading;

using System.IO.Ports;

namespace CSharpRobotics

{

public class StampSerialTest

{

private SerialPort serialPort;

public StampSerialTest()

{

serialPort = new SerialPort("COM5");

// If the port is open, close it.

if (serialPort.IsOpen)

{

serialPort.Close();

}

else

{

// Set the port's settings

serialPort.BaudRate = 2400;

serialPort.DataBits = 8;

serialPort.StopBits = (StopBits)Enum.Parse(typeof(StopBits), "1");

serialPort.Parity = (Parity)Enum.Parse(typeof(Parity), "None");

serialPort.DtrEnable = false;

// Open the port

serialPort.Open();

// Pause the thread

Thread.Sleep(100);

}

}

public string test(byte something)

{

byte[noparse]/noparse a = { something };

Console.WriteLine("The text of something is: " + a[noparse][[/noparse]0].ToString());

serialPort.Write(a, 0, a.Length);

Thread.Sleep(100);

string data = serialPort.ReadExisting();

Console.WriteLine("The returned data is: " + data);

return data;

}

public void close()

{

serialPort.Close();

}

}

class Program

{

static void Main(string[noparse]/noparse args)

{

StampSerialTest sst;

try

{

sst = new StampSerialTest();

Console.WriteLine("From Stamp: " + sst.test((byte)100));

Console.WriteLine("From Stamp: " + sst.test((byte)101));

Console.ReadLine();

sst.close();

}

catch (System.Exception ex)

{

Console.WriteLine("The following exception was received: " + ex);

Console.ReadLine();

}

}

}

}

Comments

  • Kevin WoodKevin Wood Posts: 1,266
    edited 2007-08-07 23:37
    What type of setup are you using for your Stamp? HomeWork board or BoE?

    With these boards, there is an automatic echo sent back to the serial port, so in your C# code:

    Console.WriteLine("From Stamp: " + sst.test((byte)100));
    Console.WriteLine("From Stamp: " + sst.test((byte)101));

    // You probably need a pause at this point to avoid immediately
    // picking up the echo from the above 2 writes
    Console.ReadLine();

    If that doesn't work, see if you get the correct results using the debug window of the StampIDE. If it doesn't work there, the problem is likely in the PBasic code.
  • Jeff RileyJeff Riley Posts: 5
    edited 2007-08-08 15:56
    Kevin,

    Thank you very much for the relpy.· I am using the BOE board.· So you were correct about the timing, but I also had to make some other changes.· I·changed the PBasic program serialIn variable from a byte array to just a simple byte, and changed the main code as shown below.

    main:
    · serialIn = 0
    · foo = 255
    · SERIN 16,16780,1000,main,[noparse][[/noparse]DEC serialIn]
    · PAUSE 100
    · LOOKDOWN serialIn, [noparse][[/noparse]100, 101, 102], foo
    · BRANCH foo,[noparse][[/noparse]test1, test2, getCompass]
    · PAUSE 5
    · GOTO main

    I then made some changes to the C# test method passing in a string in stead of a byte (since I needed a space after the 100 and 101 to tell DEC the input was complete).· I changed the test method as follows added more time in the thread sleep line.

    public string test(string something)

    {

    byte[noparse]/noparse a = Encoding.Default.GetBytes(something);

    Console.WriteLine("The text of something is: " + a[noparse][[/noparse]0].ToString());

    serialPort.Write(a, 0, a.Length);

    Thread.Sleep(500);

    string data = serialPort.ReadExisting();

    Console.WriteLine("The returned data is: " + data);

    return data;

    }

    Now it seems to be working better.· I am getting the "a" and "b" back as expected, but I am also getting the "100" and "101" back.· So my return data is "100 a" and "101 b".· Does anyone know of a way to turn off the echoing of the "100" and "101"?

    Thank you very much,

    Jeff Riley

  • Mike GreenMike Green Posts: 23,101
    edited 2007-08-08 16:03
    You can't turn off the echoing. It's part of the circuitry used to interface the Stamp to the PC for programming purposes.
  • Jeff RileyJeff Riley Posts: 5
    edited 2007-08-08 16:34
    Bummer. I want to thank you all for your help with this.

    Thank you,
    Jeff Riley
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2007-08-08 18:34
    Hi Jeff, although you can't stop the default programming port (P16) echoing back you can remove the echo or avoid reading it in your C# application.

    There are several ways you can do this. Say for example you are transmitting a number as a string ("101") and expect 1 character to be returned ("a") when the write is completed the readbuffer will contain 4 bytes (101a). If you keep account of the number of bytes you sent then you can remove these bytes before you read your data.

    First keep track of the length :

    out_string="101"

    len_of_string=len(out_string)

    Serialport.Write(out_string)

    Then read the echo to oblivion

    For discard =1 to len_of_string

    discard=Serialport.Readbyte

    Next

    Then read data

    in_data=Serialport.ReadExisting

    Thats just a rough example I am sure you can expand on

    Jeff T.
  • Kevin WoodKevin Wood Posts: 1,266
    edited 2007-08-08 19:40
    Since he is sending "100 " and getting back "100 a", he should be able to use the space character as the string delimiter.
  • Jeff RileyJeff Riley Posts: 5
    edited 2007-08-08 21:11
    Great. Thank you both for the great information. I will add the parsing of the returned data to my code.

    Thank you,
    Jeff Riley
Sign In or Register to comment.