Shop OBEX P1 Docs P2 Docs Learn Events
Setting baud rate for PSC USB — Parallax Forums

Setting baud rate for PSC USB

DutchDakDutchDak Posts: 11
edited 2006-10-29 16:04 in Robotics
I am writing an interface for the PSC USB in C# (.NET express 2005).
I have every thing working now except for setting the baud rate.
I am not getting a result when I do the· !SCSBR... call for 38K4.

Below the code:


public void SetBaudRate(byte Rate)
{
byte[noparse]/noparse Token = new byte[noparse][[/noparse]8];
byte[noparse]/noparse PSCBuffer = new byte[noparse][[/noparse]11];
// The buffer may not be empty, so this is done explicitly
Com.DiscardInBuffer();
// 01234567
Token = Ascii.GetBytes("!SCSBR_\x0D");
// Syntax: !SCSBR x $0D (no spaces)
// Where x = 0 for 2400 or 1 for 38K4
// Reply: BR x (no spaces / 3 bytes)
Token[noparse][[/noparse]6] = Rate;
Com.Write(Token, 0, Token.Length);
// Com.Read(PSCBuffer, 0, 8);
switch (Rate)
{
·· case Baud2400:
····· Com.BaudRate = 2400;
····· break;
·· case Baud38K4:
····· Com.BaudRate = 38400;
····· break;
·· default:
····· ErrorMessage("SetBaudRate - Wrong baud rate.");
····· break;
}
// wait for 4 seconds to give the PSC the chance to respond
Pause(4);
// returns the command (8 positions) and the baud rate (3 positions)
Com.Read(PSCBuffer, 0, 11);
// Com.Read(PSCBuffer, 0, 3);
}

Note that also the·version with the
Com.Read(PSCBuffer, 0, 8);
Statement works, but then there is a time out for
Com.Read(PSCBuffer, 0, 3);
because there is no output from the PSC.

Any hints ???

Herman.

Comments

  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-10-22 02:33
    Try this:
    Token = Ascii.GetBytes("!SCSBR_\r");

    regards peter
  • DutchDakDutchDak Posts: 11
    edited 2006-10-22 20:04
    "\r" is the same as "\x0D".

    I think I have a partial solution.
    I do in C# an additional open and close of the COM port.
    Why that is needed I do not totally understand ...
    The result is a working 38K4 connection.
    However the 3 bytes output are not produced ...
    Is this due to the USB driver ??

    public void SetBaudRate(byte Rate)
    {
    byte[noparse]/noparse Token = new byte[noparse][[/noparse]8];
    byte[noparse]/noparse PSCBuffer = new byte[noparse][[/noparse]11];

    // The buffer may not be empty, so this is done explicitly
    Com.DiscardInBuffer();

    // 01234567
    Token = Ascii.GetBytes("!SCSBR_\x0D");
    // Syntax: !SCSBR x $0D (no spaces)
    // Where x = 0 for 2400 or 1 for 38K4
    // Reply: BR x (no spaces / 3 bytes)

    Token[noparse][[/noparse]6] = Rate;

    Com.Write(Token, 0, Token.Length);

    Com.Read(PSCBuffer, 0, 8);

    Com.Close();

    switch (Rate)
    {
    case Baud2400:
    Com.BaudRate = 2400;
    break;
    case Baud38K4:
    Com.BaudRate = 38400;
    break;
    default:
    ErrorMessage("SetBaudRate - Wrong baud rate.");
    break;
    }

    // wait for 2 seconds to give the PSC the chance to respond
    Pause(2);

    Com.Open();
    }

    Herman.
  • kwokkiekwokkie Posts: 8
    edited 2006-10-23 20:18
    Perhaps Com.BaudRate =38400 only sets a variable in your SerialPort object, and the com port needs to be re-initialized before it is really in 38400 baud mode. (e.g. close the connection, and re-open)
    I'm just guessing here, though.


    Kwok
  • Mike GMike G Posts: 2,702
    edited 2006-10-27 02:14
    I created a PSC object in VB express that works great.

    This is the SetBaud method.
        Public Function SetBaud(ByVal baud As BaudRate) As BaudRate
          If IsSerialPortOpen() AndAlso Not Me.Baud = baud Then
            Select Case baud
              Case BaudRate.N2400
                connection.WriteLine(N2400)
                CloseSerialPort()
                OpenSerialPort(BaudRate.N2400)
              Case BaudRate.N38k4
                connection.WriteLine(N38k4)
                CloseSerialPort()
                OpenSerialPort(BaudRate.N38k4)
            End Select
          End If
          Return baud
        End Function 'SetBaud
    
    


    Once you set the PSC baud rate you need change the serial connection to the new baud. I took the lazy path and closed the connection then opened it again and lose the PSC response. You could use the DCB structure in windows but that's a bit of work. I don't think the serialport object in VS2005 allows you to change the serial port baud on the fly. After you change the baud then send the ?Ver command to verify your connection.
        Public Function Ping() As Boolean
          If IsSerialPortOpen() Then
            Dim buffer(11) As Char
    
            connection.WriteLine(VER)
            Thread.Sleep(200)
            connection.Read(buffer, 0, 12)
    
            If Not buffer(9) = Nothing Then Return True
          Else
            Throw New IOException("Ping error -- serial port: " & Me.PortName & " is closed")
          End If
          Return False
        End Function 'Ping
    

    Post Edited (Mike Gebhard) : 10/27/2006 2:22:37 AM GMT
  • Mike GMike G Posts: 2,702
    edited 2006-10-29 16:04
    Here's part of my VB Express project for the PSC for those that are interested. The project contains everthing you need to control and group PSC channels. I'm currently working on the GUI. Hopefully I'll be done in a few weeks.

    Mike
    zip
    308K
    PSC.zip 308.2K
Sign In or Register to comment.