Click here to Skip to main content
15,889,096 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

VB
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# ;

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 ;

C#
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.
Posted
Updated 1-Dec-18 10:20am
Comments
George Jonsson 11-Jul-14 20:04pm    
Have you compared content of the variable "sending" between vb and c#?
What is the content of "DriverNo"?
Doesn't vb start an index with 1?
In that case you have decrement the index in c#
Convert.ToByte((Strings.Mid(DriverNo, 0, 2))); // start with 0 instead
Burak Demirsoy 12-Jul-14 1:52am    
As you can see from Strings.Mid which Mic.VisualBasic.CompilerServices class's function.Therefore, It does not start with 0 in C#. DriverNo = "20021267" all are defined in code.

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))
sakzu@hotmail.com 23-Oct-15 18:42pm    
Merhaba Burak bey, Bende mbus ile uğraşıyorum şuan ve ne yaptıysam data alamadım. Tx ok ama rx te problem var bu konuda yardımcı olabilir misiniz?

1 solution

There could be a few things wrong here. The VB array bounds was a good spot.

Chars in .Net are 16bit, and that could be the problem. You're working with binary data so don't use them. Just checking the documentation for the SerialPort class, default encoding is ASCII, 7 bit. Anything above 0x7f gets converted to a question mark. So I suspect your data is getting corrupted.

http://msdn.microsoft.com/en-us/library/y2sxhat8(v=vs.110).aspx[^]

Instead don't go near strings and chars.

Try:
sp.Write(SendData, 0, 8);

And see if that helps.
 
Share this answer
 
Comments
Burak Demirsoy 12-Jul-14 6:31am    
You are totally right. Before you told me I did that way and right now I solved receiving data. However there still exists problems.


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.
Burak Demirsoy 12-Jul-14 6:35am    
So the open valve function is the following ;

private void OpenValve(string DeviceNo,bool ValvePosition)
{

string giden = "";
SendData[0] = 0xfa;
SendData[1] = Conversions.ToByte(Strings.Mid(DeviceNo, 1, 2));
SendData[2] = Conversions.ToByte(Strings.Mid(DeviceNo, 3, 2));
SendData[3] = Conversions.ToByte(Strings.Mid(DeviceNo, 5, 2));
SendData[4] = Conversions.ToByte(Strings.Mid(DeviceNo, 7, 2));
if (ValvePosition)
SendData[5] = 50;
else
{
SendData[5] = 51;
}
SendData[6] = CheckSum_Temass(5);
SendData[7] = 0xfb;



sp.Write(SendData, 0, SendData.Length);

I manage open/close valve but it takes time. I'm continuously calling OpenValve() function. It should be like when I call OpenValve() function, it should operate at first time. But it does not and cant figure out why ?
verniy 17-Nov-17 12:25pm    
merhaba burak.
internete o kadar çok şey aramama rağmen mbus haberleşme ile ilgili düzgün bilgi bulamadım.haberleşmede yardımcı olabilir misiniz?
Rob Philpott 12-Jul-14 9:23am    
Ok, this sounds like a typical issue where you need to 'flush' the data to get it to send. SerialPort doesn't expose this method, but looking around others have suggested that it could be down to handshaking. As a thing to try, set DtrEnable and RtsEnable to true on the serial port at the same time as setting baud/parity etc. Worth a try..
Burak Demirsoy 12-Jul-14 16:21pm    
I tried and nothing changed actually. But I found a way to operate this. So I looked at ns times for receiving and sending times and by observing, I'm now able to open close valve and receive data. However, this seems stupid cus I flush open close function several times as well as for receiving. Theory does not comply with life indeed :) Thanks for help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900