新闻  |   论坛  |   博客  |   在线研讨会
python 接收串口信息,并以二进制写入txt
tanry111 | 2018-07-12 13:55:04    阅读:1957   发布文章

import serial

import cv2

import matplotlib as plt

import binascii

import os

from PIL import Image

import numpy as np

from numpy import linalg

import matplotlib.pyplot as plt

import math  

import string  

import os 

import time


img_with=1280

img_high=1024


got_img_th=1


try:

    ser=serial.Serial( #下面这些参数根据情况修改

                  port='COM10',

                  baudrate=115200,

                  parity=serial.PARITY_NONE,

                  stopbits=serial.STOPBITS_ONE,

                  bytesize=serial.EIGHTBITS)

    print "COM open Success!!"

    ComSus=1

except Exception as e:

    print(e) 

    

    print ("COM open Fail!!")

    ComSus=0

try:

    os.remove("123.txt")

except Exception as e:

    print(e) 

data=[]

starts=[0,0,0,0]

success=0;

txt=open("123.txt","a")

perent=0

if (ComSus):

    print ("start send...")

    ser.write("debug("+str(got_img_th)+")\r\n")

    print ("send will")

    i=10000

    while (i):

        i=i-1

        temp =ser.read()

        starts[3]=starts[2]

        starts[2]=starts[1]

        starts[1]=starts[0]

        starts[0]=temp

#        print starts

        if (starts==["t","r","t","s"]):

            print "start collect"

            now=time.time()

            while(1):

                temp =ser.read()

                starts[3]=starts[2]

                starts[2]=starts[1]

                starts[1]=starts[0]

                starts[0]=temp

                if (starts==["r","e","v","o"]):

                    print "collect success"

                    i=0

                    success=1

                    break

                else:

                    txt.write(temp)

                    perent=perent+1

                    if(perent%10000==0):

                        print str(perent*100.0/(img_with*img_high) )+"%"                   

        

    if (success==0):

        print ("no image!!") 

    else:

        print ("success!!")

        print ("time="+str(time.time()-now))

    ser.close()

txt.close()

print ("COM close!!")


*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。

参与讨论
登录后参与讨论
推荐文章
最近访客