0

I have tried probably 6 different ways. I am just too much of a beginner, when it come to this. I need to be able to get this Axis Position info., below is an example of position 30000 and 0.

This is what comes into the serial port, after I send a request for Position info. - Other times it may be Velocity I am trying to read. Which would be anywhere from 100 to 33000.

Here is what I am trying to read:

        Hex                       Ascii

FF 2F 30 60 33 30 30 30 30 03    ÿ/0`30000.    

Sometimes this:

FF 2F 30 60 30 03                ÿ/0`0.   

I have already sent request to my controller. It has already sent back my reply. I can see it with my terminal programs. But my program just doesn't work. I have tried to use .ReadByte and .ReadExisting, with no success.

//BUTTON TO READ THE PORT AND ATTEMPT TO CONVERT DATA TO USABLE DATA.

private void Com10rcvbutton_Click(object sender, EventArgs e)
 {
   try
   {
       string line = serialPort1.ReadLine();
    StreamReader sr = new StreamReader(line);
    sr.BaseStream.Position = 0x04;
    byte[] buffer = new byte[0];
    sr.BaseStream.Read(buffer, 4, 8);
    foreach (byte myByte in buffer)

    textBox1.Text += myByte.ToString("X") + " ";
    sr.Dispose();


   }   
   catch(Exception ex)
   {
       MessageBox.Show(ex.Message);

   }
   finally
   {
       MessageBox.Show("Your Serial Test Code is Done");
   }
}

Thanks for your help in advance...

Renya Karasuma
  • 1,044
  • 4
  • 11
  • 18
jeffserv
  • 69
  • 2
  • 9
  • It will not work because SerialPort.ReadLine() won't return until it received the SerialPort.NewLine string. What you receive isn't actually text, it is binary data. You could limp along with `serialPort1.NewLine = new String((char)3, 1);` and ReadLine() will work. – Hans Passant Dec 15 '13 at 13:32

1 Answers1

1

In my experience of serial port communication, I have never used a StreamReader. You should be able to use an event-driven approach.

If the device is operating in VCP (Virtual Com Port), then it is as easy as using the System.IO.Ports.SerialPort type. You will need to know some basic information about the device, most of which can be gleaned from Windows Management (Device Manager) if you don't know it. After constructing like so:

SerialPort port = new SerialPort(portNo, baudRate, parity, dataBits, stopBits); You may or may not need to set some additional flags, such as Request to send (RTS) and Data Terminal Ready (DTR)

port.RtsEnable = true;
port.DtrEnable = true;

Then, open the port.

port.Open();

To listen, you can attach an event handler to port.DataReceived and then use port.Read(byte[] buffer, int offset, int count)

port.DataReceived += (sender, e) => 
{ 
    byte[] buffer = new byte[port.BytesToRead];
    port.Read(buffer,0,port.BytesToRead);
    // Do something with buffer
};

To send, you can use port.Write(byte[] buffer, int offset, int count)

Community
  • 1
  • 1
Will Faithfull
  • 1,868
  • 13
  • 20
  • Ok, I put this code into the program.I am now getting data back. But all the data in Hex so far. Thanks so much... – jeffserv Dec 15 '13 at 15:17
  • my byte is `var dataItems = new byte[] { 0x68, 0x01, 0x61, 0x01, 0x06, 0x1B, 0x4A, 0x60, 0x0B, 0x86, 0xE8, 0x46, 0x04, 0x68, 0x04, 0x02, 0x04, 0x22, 0x04, 0x42, 0x04, 0x00, 0x04, 0x20, 0x04, 0x40, 0x02, 0x06, 0x02, 0x26, 0x02, 0x46 ,0x76 ,0xDB ,0x16 };` then I am doing `port.Write(dataItems, 0, dataItems.Length);` then following your code I am trying to read the buffer but I unable to check the bytes recieved – Moeez May 18 '20 at 05:38