LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How to setup tcpip communication from a computer to a board?

I've been given a custom-built instrument that utilizes a crossover cable to communicate between the computer and board.  Currently there is python code used to run this system that I am having to call from my labwindows/cvi program.  I need to rewrite this python code in cvi but can't figure out how to setup the communication.  The computer has been set with an ip of 192.168.2.101 and it appears that the board is set to 192.168.2.105.  It also appears that port 2000 is set to be used. 
 
It doesn't seem I can use the tcp support library because there is no client that connects to the server.  I'd like to register a tcp server in my program and then use it to write to the client at 192.168.2.105 without having the client send any information over first.  Is this possible?
 
I've also looked at the datasocket library but it doesn't seem like I can do anything there. 
 
I've also gone into nimax and tried to add a tcp/ip resource but it can't open a session there. 
 
Anybody with any insight?  I'm completely lost.
 
Thanks
0 Kudos
Message 1 of 11
(4,079 Views)

You should be able to go into NIMax and open up a session to "tcpip0::192.169.2.105::2000::socket". Then you can use viWrite and viRead to send/receive commands and data. If you can't open up the session then there  is something else missing? Firewall maybe? If you have more than one network card in the computer try 'tcpip1::......"

 

0 Kudos
Message 2 of 11
(4,062 Views)
Hello hillb,

just an idea: maybe that the board is the server and you (the computer) need
to be the client?
Have you tried to connect to the board as client at port 2000? just use the
cvi samples/tcp/client.cws

Greetings from Bremerhaven, Germany

Norbert Rieper


"hillb" <x@no.email> schrieb im Newsbeitrag
news:1200595213622-640190@exchange.ni.com...
> I've been given a custom-built instrument that utilizes a crossover cable
> to communicate between the computer and board.&nbsp; Currently there is
> python code used to run this system that I am having to call from my
> labwindows/cvi program.&nbsp; I need to rewrite this python code in cvi
> but can't figure out how to setup the communication.&nbsp; The computer
> has been set with an ip of 192.168.2.101 and it appears that the board is
> set to 192.168.2.105.&nbsp; It also appears that port 2000 is set to be
> used.&nbsp;
> &nbsp;
> It doesn't seem I can use the tcp support library because there is no
> client that connects to the server.&nbsp; I'd like to register a tcp
> server in my program and then use it to write to the client at
> 192.168.2.105 without having the client send any information over
> first.&nbsp; Is this possible?
> &nbsp;
> I've also looked at the datasocket library but it doesn't seem like I can
> do anything there.&nbsp;
> &nbsp;
> I've also gone into nimax and tried to add a tcp/ip resource but it can't
> open a session there.&nbsp;
> &nbsp;
> Anybody with any insight?&nbsp; I'm completely lost.
> &nbsp;
> Thanks


0 Kudos
Message 3 of 11
(4,061 Views)
Thanks cymrieg , I tried this and it couldn't open a session.  The computer does have a disabled wireless network card but from what I can tell in max, I can't manually change the resource name to try tcpip1.  I don't think its a firewell issue since I can communicate with the device through that port using the python code I have.


Message Edited by hillb on 01-18-2008 11:57 AM
0 Kudos
Message 4 of 11
(4,047 Views)

Hi Norbert,

I did try the example client program but it was not successful: the connection to server failed.

 

0 Kudos
Message 5 of 11
(4,043 Views)
can you post the network related part of the python code you are calling ?

especially look at the beginning of the file, you should find something like 'from socket import *' or 'import socket'
also, you should find a line looking like:
socket( AF_INET, SOCK_STREAM, IP_PROTO_TCP )

look for something looking like a function call to a function called 'socket', and post the resulting lines if you can. it may be a UDP connection to the board (which can be handled starting with CVI 8.5, or a raw socket which you can't handle using CVI network library but using Winsock library)


0 Kudos
Message 6 of 11
(4,002 Views)
Hi dummy_decoy, it definitely sounds like you may be able to put me on the right track.  Here are some different pieces of the code:

   def init(self, comm):
       self.initialized = 0
       if comm.keys()[0] == 'uart':
           import uart1
           CONN = uart1.UART(comm_cfg)
       elif comm.keys()[0] == 'usi':
           pass
       elif comm.keys()[0] == 'ethernet':
           ip = comm['ethernet'][0]
           port = comm['ethernet'][1]
           self.initEthernet(ip, port)
       elif comm.keys()[0] == 'rf':
           import rf
           CONN = rf.RF()
       else:
           return 0
       return 1

   def initEthernet(self, ip, port):
       self.host = '192.168.2.101'
       self.client = ip
       self.port = port
       self.soc = socket(AF_INET, SOCK_DGRAM)
       addr = getaddrinfo(self.host, port, AF_INET, SOCK_DGRAM)

       self.soc.bind(addr[0][4])
       self.soc.settimeout(3)
       connected = 0
       self.addr = (self.client, int(self.port) +1)
       while not connected:
           cbuf = self.soc.sendto("hello", self.addr)
           print self.addr
           try:
               cbuf, addr = self.soc.recvfrom(1024)
               connected = 1
           except:
               pass
       return connected

  def shuttercontrolfunction(self,shutteron):
       if (shutteron==0):
          self.sendCmd('pj2=o')
          self.sendCmd('pj2=0')
          self.quit()
       elif (shutteron==1):
          self.sendCmd('pj2=o')
          self.sendCmd('pj2=1')
          self.quit()
       else:
          self.quit()

   def sendCmd(self, cmd):
       self.cmdlist.append(cmd)
       self.cmdno += 1
       r = struct.pack('h', self.cmdno)
       self.soc.sendto(r+':'+cmd, self.addr)
       time.sleep(0.1)
0 Kudos
Message 7 of 11
(3,977 Views)
ok, it is now clear how it tries to communicate with the board.

i first have to tell you that the protocol seems really awkward, mixing ascii and binary messages...

the socket is indeed an UDP socket. UDP is only available starting with CVI 8.5. if you use an earlier version of CVI you will have to use windows network API through the winsock library.

the computer seems to need being located at address 192.168.2.101 when using this python snippet... for whatever reason. there may be a filtering on the board preventing from communicating with the board if the message does not come from this address (my guess is that the author did not know how to retrieve the local address to setup the socket).

an address and a port (which we will now refer to as IP and PORT) are specified somewhere, maybe in a configuration file (they are stored in variables comm['ethernet'][0] and comm['ethernet'][1]).  

the computer sends messages to the board at address IP on port PORT+1, and listen to replies on port PORT.

to 'establish' a connection, the computer sends the message "hello" to the board, until the board replies. the content of the reply seems not relevant since it is simply discarded.

messages are comprised of a command number, a separator and a command.
- the command number is a short int (2 bytes) written in binary, least significant byte first. the command number seems to be increased by one for each command transmitted to the board.
- the separator is always ':'
- the command is the ascii command. i'm not sure about the terminating null character, but i think you should NOT send it (python does not use a terminating null character for strings)

the given snippet waits 100ms after each command. i don't have the code of the quit() function, it may wait for a reply...

from the snippet you give, valid commands are:
- shutter off: 'pj2=o' followed by 'pj2=0'
- shutter on:
'pj2=o' followed by 'pj2=1'

example: if the 12th command sent to the board is a shutter on, you will send to the board the string "\x0c\x00:pj2=o" followed by
"\x0d\x00:pj2=1".  (i don't know how to disable that smiley thingy ! the smileys are ':' followed by 'p')

i hope this helps...


Message Edited by dummy_decoy on 01-22-2008 03:31 PM
0 Kudos
Message 8 of 11
(3,967 Views)
Thanks! I'll definitely give this a try.


0 Kudos
Message 9 of 11
(3,961 Views)
Hey dummy_decoy, just wanted to give you thanks.  I'm able to control the shutter now Smiley Happy
0 Kudos
Message 10 of 11
(3,935 Views)