【CSEN032】CS Robot: Use socket to receive string type data (With reformat example)

    Assuming the data from external device is "3.1,232.4,-232.123,ok#", which is a string contains 3 sets of number and term ok seperated by comma, then end with the # sign.

    The user need to analysis the data, by check the data ends with ok or not to decide post work flow.

    The following is a example to convert the 3 numbers to float if the data ends with ok.

socket_open("192.168.230.1",60000,"socket_1")
# Open socket, the IP address 192.168.230.1 and port number 60000 are belong to the external device.
global recv  # Data received, will remove the # sign
global data # Store the effective data
# Global variable can be monitored in the monitor window on the teach pendant
while True:
  socket_send_string("Hello world#","socket_1")
  # Send string to server
  recv = socket_read_string(socket_name="socket_1",suffix="#",timeout=120)
  # Assume the data received is '3.1,232.4,-232.123,ok#', use suffix="#" can remove the # at the end
  # So recv stores '3.1,232.4,-232.123,ok'
  b = str.split(recv,',')
  # Split data in recv by comma and store in the list b
  data = [float(b[0]),float(b[1]),float(b[2])]
  # Since the elements in b are float numbers, so float function can convert string to float directly
  sleep(0.1)
点击显示全文
赞同0
发表评论
分享

手机扫码分享
0
167
收藏
举报
收起
登录
  • 密码登录
  • 验证码登录
还没有账号,立即注册
还没有账号,立即注册
注册
已有账号,立即登录
选择发帖板块
举报
请选择举报理由
举报
举报说明