08-01-2012 07:46 AM
Hi,
Again we are trying to send the data in bigger project in C#.There they have provided the function "m_socListener_server.BeginAccept(new AsyncCallback(OnClientConnect), null);" in which BeginAccept(new AsyncCallback(OnClientConnect), null) is a function of Socket class and m_socListener_server is an object reference of that Socket class through which we are calling this function.The above written function is not accepting the data from Labview.How we send the data through socket??
08-01-2012 07:59 AM - edited 08-01-2012 08:02 AM
And how is it not accepting the data? The BeginAccept is not accepting anything anyhow, it just starts an asynchonrous thread that is waiting for connection requests to arrive.
Is this with the data server in LabVIEW? Then you need to think again! BeginAccept() waits for connection attempts by a client, not for data from a server. Begin Accept is similar to the TCP Listen primitive in LabVIEW, not to TCP Read.
You are always providing information in litle pieces which ends up in a ping pong game about requesting extra informations and getting part of that and requesting more, etc. This is very frustrating.
08-09-2012 04:34 AM
Hi,
Now we are able to send data from labview to C# and it is displaying in the bigger project written in C#.But we are unable to update the data, that is after changing any of the inputs,the change not get reflected in C# application.But after stopping and again running that change can be visible.where will be the problem.Below is our C# code.Please give necessary instructions.
System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();
byte[] inStream = new byte[10025];
ArrayList list = new ArrayList();
ArrayList orglist = new ArrayList();
ArrayList Andata = new ArrayList();
ArrayList didata = new ArrayList();
ArrayList FFrame = new ArrayList();
string st="";
try
{
clientSocket.Connect("127.0.0.1", 8221);
NetworkStream serverStream = clientSocket.GetStream();
serverStream.Read(inStream, 0, (int)clientSocket.ReceiveBufferSize);
// soc.BeginReceive(theSocPkt.dataBuffer, 0, theSocPkt.dataBuffer.Length, SocketFlags.None, pfnWorkerCallBack_server, theSocPkt);
//int x = inStream.Length;
System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
string str = enc.GetString(inStream);
char[] strToParse = str.ToCharArray();
int n=str.Length;
//char ch;
//string st;
for (int i = 0; i < n; i++)
{
while (strToParse[i] != '\t' && strToParse[i] != '\n' && strToParse[i] != '\r')
{
if (i == 0)
{
st = strToParse[i].ToString();
}
else
st = st + strToParse[i];
i++;
}
i++;
list.Add(st);
st=strToParse[i].ToString();
if (strToParse[i] == '\r' || strToParse[i] == '\n' || strToParse[i] == '\0')
break;
}
/* for(int i=7;i<216;i++)
{
if(i<=207)
Andata.Add(list[i]);
if(i>=210 && i<=215)
didata.Add(list[i]);
} */
for (int i = 0; i < list.Count; i++)
{
//
if (list[i].Equals("0") || list[i].Equals("1") || list[i].Equals("2") || list[i].Equals("3") || list[i].Equals("4") || list[i].Equals("5") || list[i].Equals("6") || list[i].Equals("7") || list[i].Equals("8") || list[i].Equals("9") || list[i].Equals("A") || list[i].Equals("B") || list[i].Equals("C") || list[i].Equals("D") || list[i].Equals("E") || list[i].Equals("F"))
{
String strr = "0" + list[i];
orglist.Add(strr);
}
else
{
String strr = (string)list[i];
orglist.Add(strr);
}
}
byte[] orgbytes = new byte[orglist.Count];
for (int j = 0; j < orgbytes.Length; j++)
{
//int i = Convert.ToInt32(orglist[j], 16);
orgbytes[j] = Convert.ToByte(orglist[j].ToString().Substring(0,2) , 16);
}
for (int x = 0; x < orgbytes.Length; x++)
{
FFrame.Add(orgbytes[x]);
DF.readBuffer.Add(orgbytes[x]);
}
DF.BuildDF();
08-09-2012 04:46 AM - edited 08-09-2012 04:47 AM
This is not a .Net support forum!!!
A quick cursory look at the code shows exactly one connect(),and then you make a synchronous read to the socket and then start an asynchonous one and process the read from the synchronous read. What does your pfnWorkerCallBack_server() function do? Why do you mix synchronous and asynchonous processing? Have you studied asynchonous socket communication in .Net?
You either use synchronous receive and put the entire code inside the try code in a loop that terminates on disconnection and possibly other errors, or use asynchronous receive and put the entire processing inside pfnWorkerCallBack_server(). You could add the whole data parsing in pfnWorkerCallBack_server() too and then the first read would be synchronous and the rest asynchronous, but why duplicate code. Just use either synchronous or asynchonous but not both at the same time.
08-09-2012 04:54 AM
rolfk wrote:
This is not a .Net support forum!!!
Sorry!!!.Thanks for suggesstion.
08-09-2012 05:04 AM
You need to give some more details.
What program are you stopping/starting to get it to work ? The C# or the LabVIEW program ?
How does your LabVIEW program looks like ?
Are you sure it is the C# program that is not working ?
What values do you expect to be updated ?
Is this all of the C# program you have shown ?
I'm missing some sort of while loop for the C# program to continue to read from the socket.
If this is the only C# code you are runing, then the program will only read the socket one single time.
There is also missing a close socket in the C# program.
This is just what I can see right now, but I'm sure there are a lot more missing.
08-09-2012 05:39 AM
What program are you stopping/starting to get it to work ? The C# or the LabVIEW program ?
Both the programs.
How does your LabVIEW program looks like ?
My labview program contains two listboxes for sending analog and digital data through TCP/IP.
What values do you expect to be updated ?
whenever we changes any of the analog or digital data that value must be updated in C# program
If this is the only C# code you are runing, then the program will only read the socket one single time.
please give some instructions,to achieve updation,Sorry
I know this is Labview Forum
08-09-2012 05:50 AM
Please have a look at my labview program.
08-09-2012 05:54 AM - edited 08-09-2012 05:55 AM
@danil33 wrote:
How does your LabVIEW program looks like ?
My labview program contains two listboxes for sending analog and digital data through TCP/IP.
I can't the program [EDIT: Too late in my reply]
@danil33 wrote:
If this is the only C# code you are runing, then the program will only read the socket one single time.
please give some instructions,to achieve updation,Sorry
I know this is Labview Forum
Sorry, but I'm not going to teach you to program C#. Or C for that matter.
But the rule is the same for LabVIEW as for a C# program. If you need at program to continuously doing something, you need a while loop.
I'm sure there are multiple forums for C# where there are people working with C# that can help you making a C# program that continuously read from the socket.
You know you can read one time from the socket, so you are close to the goal.
We can help you in your making of a LabVIEW program that sends data on a socket.
But for that to happen, we need to see your code.
08-09-2012 08:30 AM
@dkfire wrote:
Sorry, but I'm not going to teach you to program C#. Or C for that matter.But the rule is the same for LabVIEW as for a C# program. If you need at program to continuously doing something, you need a while loop.
I'm sure there are multiple forums for C# where there are people working with C# that can help you making a C# program that continuously read from the socket.
You know you can read one time from the socket, so you are close to the goal.
We can help you in your making of a LabVIEW program that sends data on a socket.
But for that to happen, we need to see your code.
Actually he mixed two different types of programming together. The first reading the socket in synchonrous manner one time with serverStream.Read() and the (commented out, I missed that in the first look) asynchonous one which gets started with soc.BeginReceive().
Whatever it is, there are still some lurking bugs in there. Unless he uses the undocumented/unsupported UTF8 nodes in LabVIEW, the stream will never be UTF8 encoded, and trying to read it with UTF8 encoding is likely to run into some problems. LabVIEW uses simple ANSI C for data strings.