0

I have been googling and haven't found any code to help me with this, maybe I missed something? I want to receive RS485 commands. At the moment I am just receiving rubbish. Here is my current code although I don't think it would help:

//C# CODE
byte[] arr = new byte[serialPort1.BytesToRead];
serialPort1.Read(arr, 0, serialPort1.BytesToRead);
String s = "";
foreach (byte b in arr)
{
     s += b.ToString() + " ";
}
/*String s = serialPort1.ReadByte().ToString();
while (serialPort1.BytesToRead > 0)
{
     s += " " + serialPort1.ReadByte().ToString();
     Thread.Sleep(10);
}*/
//String s = serialPort1.ReadLine().ToString();
richTextBox1.Invoke((MethodInvoker)delegate { richTextBox1.AppendText(s + "\n"); });

This was just a test to see if I could receive the data. Does anyone know how I can receive data through serial port and show them in a text box?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
M.R. Inc
  • 19
  • 1
  • 3
  • Well, if stuff is being *received*, that's a start - so what's the *actual* problem? Also look at it from another way: what is the *expected* output/result? – user2246674 May 24 '13 at 21:31

1 Answers1

-1

I am not sure about your final goal but if you are looking only to receive data and show them in a text box, there should be no difference with a standard RS232 serial communication, this should do it. The name of the text box in this example is txtOutput.

To start:

  public Form1()
    {
        InitializeComponent();

        serialPort1.PortName=("COM1");
        serialPort1.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(serialPort1_DataReceived);
        serialPort1.Open();
    }

serial port

  private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {

            try
            {
                SetText(serialPort1.ReadLine());

            }

            catch (Exception ex)
            {
                SetText(ex.ToString());
            }

        }

Delegate:

delegate void SetTextCallback(string text);

Append to text box:

 private void SetText(string text)
        {
            if (this.txtOutput.InvokeRequired)
            {
             SetTextCallback d = new SetTextCallback(SetText);
             this.BeginInvoke(d, new object[] { text });
            }
            else
            {
                txtOutput.AppendText(text + "\r\n");
            }}

Remember to set the serial port1 with bauds and make sure you have added the serial port to your form. I have tested the code with a standard serial connection and it is working.

FeliceM
  • 4,163
  • 9
  • 48
  • 75
  • Hello Thank you for your quick reply. RS485 Doesn't seem to be the same. As above in my code I had the serialport.readline All the code doesn't work The closest I got to any output was 000 243 112 Reading it like the above commented code. And it should be something like this 254 000 000 000 000 000 000 It is a pain in the ass that it doesn't seem to be the same. Thanks – M.R. Inc May 25 '13 at 07:57
  • What about using an RS485 to Rs232 hardware converter and make use of standard serial code? At least the protocol will be known. – FeliceM May 25 '13 at 09:16
  • However, I visited the website for Pelco and they also recommend a hardware converter. http://www.commfront.com/RS232_Examples/CCTV/Pelco_D_Pelco_P_Examples_Tutorial.htm – FeliceM May 25 '13 at 09:34
  • Hello I am using a converter and this is the response. PTZ Controller - RS485 > RS232 Converter - RS232 > USB - PC Thanks – M.R. Inc May 25 '13 at 13:49
  • Sorry man, I did not know you were using a converter, you never mentioned it. – FeliceM May 25 '13 at 14:38
  • Sorry mate, :) It's Fine. Any ideas though ? – M.R. Inc May 25 '13 at 14:51
  • @M.R.Inc So far have you got any improvement? – FeliceM May 27 '13 at 12:50
  • No, none. Have no clue what to do. Tried everything I could think of. Still can't figure out how to do it – M.R. Inc May 28 '13 at 14:29
  • @M.R.Inc, you have supplied to little information to be able to help. From the Pelco web site I noted that the you need a serial converter 485/232. They use the own software to communicate however, looks like it is just a serial software. in this page you can see how the port setting should be: http://www.commfront.com/RS232_Examples/CCTV/Pelco_D_Pelco_P_Examples_Tutorial.htm#1. If there are not hidden tricks on their side (and it is very possible) you should be able to communicate with the camera with a standard serial port ReadLine() and Write. They also say that the camera will not respond – FeliceM May 28 '13 at 16:26
  • ...so do not expect the camera to send back any string. I can see they use hex strings to communicate with the camera. Rather than these information there is not a lot more I can do to help you without the camera on my desk. Good luck. – FeliceM May 28 '13 at 16:28
  • Hello, as said in the previous replies, I am not using a PTZ. I am using a PTZ Controller which does send out data, I need to receive that data and manipulate it. Nothing on that page helped trying to receive data other that 232 analyser which does not give any code. Thanks – M.R. Inc Jun 03 '13 at 20:26