Search code examples
c#windows-ce

Wifibot RS232 Serial Communication


I am working on Robotic project with Wifibot Platform, this robot contains a Windows Embedded Standard 7 OS, to make things short, i want to send some bits from the WES7 to a DSPIC 3FF via RS232 that controls the platform motors to make the robot move, i have developed a lot of programs using C#, but no results, is there any help from you guys.

using System;
using System.IO.Ports;
using System.Threading; 
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace sendsendRS232
{
class Program
{
    static void Main(string[] args)
    {
        SerialPort serial = new SerialPort();


        serial.PortName = "COM2";                
        serial.BaudRate = Convert.ToInt32(9200);
        serial.Handshake = System.IO.Ports.Handshake.None;
        serial.Parity = Parity.None;
        serial.DataBits = 8;
        serial.StopBits = StopBits.One;
        serial.ReadTimeout = 200;
        serial.WriteTimeout = 50;

        serial.Open();


       if (serial.IsOpen)
       {
           byte[] hexstring = { 255, 0, 120, 0};
           foreach (byte hexval in hexstring)
           {
               byte[] _hexval = new byte[] { hexval };
               serial.Write(_hexval, 0, 1);
               Thread.Sleep(1);
           }
       }
    }
}
}

Solution

  • We've not got much to go on here, since we don't have the documentation for the device you are communicating with, so we cannot check the serial port settings, or whether the 4 bytes of data you are writing to the port are a correct message to send to it.

    Since you have handshaking set to None, you might want to play with setting DtrEnable and RtsEnable to true, typical settings for a device that wants to send.

    Another thing to check that might be an issue is the 'endedness' of the data. If the data you are sending includes multi-byte values and one device is big-endian and another is little-endian, then the order the bytes are sent in is important, see the following more detailed explanation.

    Unfortunately it often comes down to experimenting with settings, or even using terminal programs like Hyperterminal or Putty to connect to the device and send the same data.

    Just as an aside, when dealing with classes that implement IDisposable like the SerialPort does, I would use it within a using statement, e.g.:

    using(SerialPort serial = new SerialPort())
    {
        // do stuff
    }
    

    That way the connection will always be closed and disposed of when it goes out of scope.