0

I have really terrible problem that makes me almost sick. For 2-3 days, I've been dealing with this protocol issue and i find myself here to get some help from you guys. I hope I'll be solving. Thanks in advance. I had code in Vb that uses Old MsComm Library. So I decided to change all stuff with C#. I'm okey with opening , closing port and sending data etc.

In Vb; I have the following part of code which is for receiving data from Mbus driver via RS485. Once you sent this it responses you to obtain data. It works and no problem.

Dim SendData(19) As Byte
Dim sending As String
SendData(0) = &HFA
SendData(1) = Mid(DriverNo, 1, 2)
SendData(2) = Mid(DriverNo, 3, 2)
SendData(3) = Mid(DriverNo, 5, 2)
SendData(4) = Mid(DriverNo, 7, 2)
SendData(5) = 210

SendData(6) = CheckSum_Temass(5)
SendData(7) = &HFB

sending = ""
For i = 0 To 7
    sending= sending + Chr(SendData(i))
Next

SP.Output = sending

So , the code above is working fine in Vb and Vb.Net. However when I convert it to C# like the following ; I cant get response from mbus driver. While sending data via RS485, I can see that yellow led fires. Normally while receiving data, you can see that red led also fires. The Code in C# ;

string sending= "";
byte[] SendData = new byte[8];
SentData[0] = 0xfa;
SendData[1] = Convert.ToByte((Strings.Mid(DriverNo, 1, 2)));
SendData[2] =  Convert.ToByte((Strings.Mid(DriverNo, 3, 2)));
SendData[3] = Convert.ToByte((Strings.Mid(DriverNo, 5, 2)));
SendData[4] = Convert.ToByte((Strings.Mid(DriverNo, 7, 2)));
SendData[5] = 210
SendData[6] = CheckSum_Temass(5); 
SendData[7] = 0xfb;

for (int i = 0; i <= 7; i++)
{
    sending= sending+ ((char)SendData[i]);
}

sp.Write(sending);

I cant see any problem with this but Vb Code works and C# does not.

In c# , the following is the part of my open port function ;

    sp.PortName = portName;
    sp.BaudRate = baudRate;
    sp.DataBits = databits;
    sp.Parity = parity;
    sp.StopBits = StopBits.One;//stopBits;
    sp.PinChanged += SerialPinChangedEventHandler1;
    sp.ErrorReceived += SerialErrorReceivedEventHandler1;
    sp.DataReceived += new SerialDataReceivedEventHandler(DataReceived);

    sp.ReadTimeout = 1000;
    sp.WriteTimeout = 1000;

Everything works fine. I can see as I said the flow of data through Mbus via RS485. I can see it from TX led which fires all the time I send data.However, as i said again, RX led does not fires.

Destroy
  • 73
  • 1
  • 9
  • I compared sent data with C# and Vb.Net, both show the same output in text file (I wrote in a text the output). So from this point, what I've understood is the problem should be related with the output function [sp.Output(VB.Net MSComm) sp.Write(C# Serial Com)) – Destroy Jul 12 '14 at 05:54

2 Answers2

0

I solved the problem which was relasted to parity. In default it is none but in my system it was supposed to be even. So I can receive data right now but the problem is now speed of data.

In Vb. I'm using valveopen function forthtimes to open valve.Therefore I code like ;

valveopen();
valveopen();
valveopen();
valveopen();

However in C# it is like god knows how many times it will operate :). Arbitrarily I'm able to open valve right now. All stuff are the same and no problem. I think data transfer speed of MSComn and Serial Port are different.

Destroy
  • 73
  • 1
  • 9
0

I solved the problem by flushing read and send data.

Destroy
  • 73
  • 1
  • 9