Instrument Control (GPIB, Serial, VISA, IVI)

cancel
Showing results for 
Search instead for 
Did you mean: 

VISA C# .NET

Solved!
Go to solution

Bonjour à tous,

 

Je viens vers vous parce que j'ai un problème de communication avec VISA que j'essaye d'utiliser en C#.

 

Je tente de communiquer via plusieurs modes de communication (série, GPIB, ethernet) entre 2 PC.

Seul le PC "esclave" m'intéresse puisque c'est sur celui-ci que je développe mon application WPF.

Je suis en attente de message du PC"maitre" via une liaison prédéterminée au lancement de mon application.

 

J'arrive à recevoir des messages mais je n'utilise pas correctement VISA puisque je n'arrive pas à faire en sorte que mon "ReadString()" s'arrête! A chaque fois j'arrive à une exception de type "timeout"!

 

m_Serial_Session = new SerialSession("COM1");
Fill_SerialSession(m_Serial_Session); //Je remplis tous les champs de ma liaison série (bits de données, parité, contrôle de flux...)
m_Serial_Session.EnableEvent(SerialSessionEventType.AnyCharacterReceived, EventMechanism.Queue);
Thread m_Thread_WaitData = new Thread(WaitForData_SerialRead);
m_Thread_WaitData.IsBackground = true;
m_Thread_WaitData.Start();



private void WaitForData_SerialRead()
{
      m_Serial_Session.WaitOnEvent(MessageBasedSessionEventType.AllEnabledEvents);
      try
      {
           MessageBox.Show(m_Serial_Session.ReadString());
      }
      catch (VisaException e)
      {
           MessageBox.Show("Délai dépassé depuis le premier élément reçu!");
      }
}

 

Je penses ne pas être très loin du but mais je n'arrives pas à trouver la fonction ou le paramètre à changer afin que tout fonctionne!

 

J'attends votre aide...

0 Kudos
Message 1 of 7
(7,300 Views)

Hi everyone!

 

Perhaps the problem is that my message is in French... So I will translate if someone will be able to help me in English...

 
I'm using VISA in C# but I have some communication trouble! 

 

I'm trying to communicate between 2 PC using several communication modes (serial, GPIB, ethernet).

I'm looking only at the "slave" computer because it's on it that I have to develop my WPF application.

I'm waiting some messages from the "master" PC. I know from which communication mode it will come because the user have to choose it when he launch my application.

 

I think I'm not using VISA correctly because when I'm reading (using ReadString()) the information coming from the "master", it can't stop to read before a timeout exception!

 

Here is the important part of my serial session communication:

 

m_Serial_Session = new SerialSession("COM1");
Fill_SerialSession(m_Serial_Session); //Je remplis tous les champs de ma liaison série (bits de données, parité, contrôle de flux...)
m_Serial_Session.EnableEvent(SerialSessionEventType.AnyCharacterReceived, EventMechanism.Queue);
Thread m_Thread_WaitData = new Thread(WaitForData_SerialRead);
m_Thread_WaitData.IsBackground = true;
m_Thread_WaitData.Start();



private void WaitForData_SerialRead()
{
      m_Serial_Session.WaitOnEvent(MessageBasedSessionEventType.AllEnabledEvents);
      try
      {
           MessageBox.Show(m_Serial_Session.ReadString());
      }
      catch (VisaException e)
      {
           MessageBox.Show("Délai dépassé depuis le premier élément reçu!");
      }
}

 

I hope I'm close to succeed but I miss something...
I tried to use the examples given by NI with VISA but I have the same timeout problem... So perhaps it's my writing that is wrong!
 
I'm using something very simple to write:
m_Serial_Session = new SerialSession("COM1");
Fill_SerialSession(m_Serial_Session);
m_Serial_Session.Write("test");

 

If anyone can help me I will be very greatful!

Message 2 of 7
(7,262 Views)

Hello Kimimarusama,

 

Here some links which indicate you the location of the VISA .NET examples :

 

VISA and GPIB Application Development Using C# or VB.NET

 

Where Can I Find Text-based Programming Examples for NI-VISA?

 

For Seven, you can take a look in this folder :

 

C:\Users\Public\Documents\National Instruments\NI-VISA\Examples\DotNET4.0

 

I think you should try to run some of these examples and see if you're able to execute them correctly. Then, you can start from one of them and adapt it in order to fit with your needs.

 

Regards,

Jérémy C.
NI France

0 Kudos
Message 3 of 7
(7,259 Views)

Hello JérémyC,

 

I have already read these links (and much more) and I have already see and try to use all VISA examples given at the links you give me!

 

But there is not something like I try to do... And when I try to use it with 2 computers like I want to do, I have some errors!

 

When I use my program to read, I always have a timeout error!

And when I use "simplereadwriteasynchronous" from VISA example to read information I send, I have an error like this: VISA error code -1073807253 (0xBFFF006B), ErrorSerialFraming

 

That's why I come in that forum! I'm pretty desperate...

I really need to have some example that works with that I'm trying to do!

 

I will try again in few hours to oparate my program using other functions that are not in VISA examples...

Like:

m_msgSessionReader = new MessageBasedSessionReader(m_Serial_Session);
MessageBox.Show(m_msgSessionReader.ReadLine());

 

Or:

m_Serial_Session.TerminationCharacter = 0x0a; // = '\n'
m_Serial_Session.TerminationCharacterEnabled = false;
m_Serial_Session.SendEndEnabled = true;

 

but I don't have lot of hope...

0 Kudos
Message 4 of 7
(7,257 Views)

Do you use a Null Modem Cable and not an ordinary Serial cable?

 

Wiki Null modem

 

Null modem cable

 

Computers are considered as Masters and you have to use this kind of cable if you want to exchange data between them.

 

Did you try to test your VIs in loopback test ?

 

How to Do a Serial Loopback Test

 

Regards,

Jérémy C.
NI France

0 Kudos
Message 5 of 7
(7,254 Views)

I think it's ok!

 

I can communicate without trouble with HyperTerminal!

0 Kudos
Message 6 of 7
(7,251 Views)
Solution
Accepted by topic author Kimimarusama

Hi again,

 

I don't know if it's the better thing to do because it's not like VISA examples but I found A solution:

 

m_Serial_Session = new SerialSession(rS232.port);
Fill_SerialSession(m_Serial_Session);
m_Serial_Session.Timeout = Session.InfiniteTimeout;
Thread m_Thread_WaitData = new Thread(WaitForData_SerialRead);
m_Thread_WaitData.IsBackground = true;
m_Thread_WaitData.Start();


        private void WaitForData_SerialRead()
        {
            while (true)
            {
                try
                {
                    MessageBasedSessionReader Reader = new MessageBasedSessionReader(m_Serial_Session);
                    string responseString = Reader.ReadLine();
                    if (responseString.Length != 1)
                    {
                        Log_File.Log("Message reçu sur la liaison série " + m_Serial_Session.ResourceName + " : " + responseString);
                        m_Parser_ReceivedOrders.Parse_ReceivedOrders(responseString, m_IHM_Concerned, m_Choosed_Material);
                    }
                }
                catch (VisaException e)
                {
                    MessageBox.Show("Délai dépassé depuis le premier élément reçu!");
                }
            }
        }

 

To communicate with that I use this kind of write function on the other computer:

MessageBasedSessionWriter Writer = new MessageBasedSessionWriter(m_Serial_Session);
Writer.WriteLine(DataToSend);

 

The result looks like that I'm waiting for so it's resolved from my point of view!

P.S: If someone have to say to me that it's not good to do this like that AND this people have a solution to offer, I will listen to him to see if I can improve my solution! 🙂

0 Kudos
Message 7 of 7
(7,229 Views)