USB Serial COM is akin to socket programming. The routines are somewhat platform-dependent, and today's developer doesn't usually care about the details. Just give me an abstraction that lets me communicate!
Then I realized that Python has PySerial. Using this library, I was easily able to communicate with the device, and send it via localhost socket to my C++ program. Done and done.
Check out the super simple source code:
import serial
import socket
IP="127.0.0.1"
PORT=3001
sock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM) #Sending data to localhost via UDP
ser= serial.Serial(2,timeout=2) #COM3 is port number 2.
print ser.name
line=''
while (True):
line=ser.readline()
print line
sock.sendto(line,(IP,PORT))
Python for the win.
No comments:
Post a Comment