Using Packet Serial in .NET - RoboClaw 2x60a
Posted: Sat May 02, 2020 8:17 pm
Hello,
Sorry if this question have been asked and answered before, but I am having a tremendously communicating with RoboClaw 2x60a, purchased through ServoCity, using the packetized serial protocol from a .NET based program. Standard serial seems to work, but I am looking to send more advanced commands to control drive and turn. To test, I have an OSEPP FTDI adapter connected to my Windows computer which shows it as COM Port 12 in Device Manager. FTDI TX goes RoboClaw RX, and RX goes to RoboClaw transmit.
Here's my sample test harness:
This code does not seems to work. What could I be missing?
Sorry if this question have been asked and answered before, but I am having a tremendously communicating with RoboClaw 2x60a, purchased through ServoCity, using the packetized serial protocol from a .NET based program. Standard serial seems to work, but I am looking to send more advanced commands to control drive and turn. To test, I have an OSEPP FTDI adapter connected to my Windows computer which shows it as COM Port 12 in Device Manager. FTDI TX goes RoboClaw RX, and RX goes to RoboClaw transmit.
Here's my sample test harness:
Code: Select all
using System;
using System.IO.Ports;
using System.Threading;
namespace RC_Console
{
class Program
{
static void Main(string[] args)
{
SerialPort port = new SerialPort("COM12", 115200, Parity.None, 8, StopBits.One);
port.Open();
Console.WriteLine("Hello World!");
byte address = 128;
byte command = 12;
while (true)
{
byte valueToWrite = 32;
ushort crc = 0;
CalculateChecksum(address, ref crc);
CalculateChecksum(command, ref crc);
CalculateChecksum(valueToWrite, ref crc);
byte msb = (byte)(crc >> 8);
byte lsb = (byte)(crc);
byte[] buffer = new byte[] { address, command, valueToWrite, msb, lsb };
port.Write(buffer, 0, buffer.Length);
Thread.Sleep(10);
}
}
private static void CalculateChecksum(byte data, ref ushort crc)
{
int i;
crc = (ushort)(crc ^ ((ushort)data << 8));
for (i = 0; i < 8; i++)
{
if ((crc & 0x8000) != 0)
crc = (ushort)((crc << 1) ^ 0x1021);
else
crc <<= 1;
}
//return crc;
}
}
}