python逐字节写入串口不起作用

时间:2017-02-16 04:58:22

标签: python python-2.7 serialization pyserial

正如之前在论坛中所讨论的那样,阅读问题正在解决但现在却出现了写问题。我想写一些十六进制地址到串口。地址如下:

    000062240
    000062A4B
    000062244
    000062245
    000062D50
    00006225E
    00006A25F

我想逐字节地写它们,延迟为0.01秒。写我做: 在写作之前我需要编写N并且完美无缺。在C之后我需要写地址。我把内部循环放在文本文件中以读取地址。但是ser.write(line[line1-1])给字符串不能与int错误结合。任何解决方案?

for line1 in range(1,8):
                  ser.write(line[line1-1])
                  time.sleep(0.01)

编写代码:

with open('lut.txt', 'r') as f:
            for line in f:
             #print line
             ser.write('N')
             time.sleep(0.01)
             ser.write(' ')
             time.sleep(0.01)
             ser.write('C')
             time.sleep(0.01)
             for line1 in range(1,8):
              ser.write(line[line1-1])
              time.sleep(0.01)
              line+=1
              time.sleep(0.01)

参考完整代码。

import serial, time
from addresstable import *
ser = serial.Serial()
ser.port = "/dev/ttyUSB0"
ser.baudrate = 38400
ser.bytesize = serial.EIGHTBITS 
ser.parity = serial.PARITY_NONE 
ser.stopbits = serial.STOPBITS_ONE              
ser.xonxoff = False    
ser.rtscts = False    
ser.dsrdtr = False  
number_address = 10
number_char = 9  
timeout=1
#f=open('lut.txt','r')

try: 
    ser.open()
except Exception, e:
    print "error open serial port: " + str(e)
    exit()

if ser.isOpen():

    try:
         ser.flushInput()
     ser.flushOutput()
# reading        
         max_packet = 20
         lines = 0
         while True:
           byteData = ser.read_until('\r',max_packet)
       newdata=str(byteData)
           print newdata.strip()

       ser.write('I')
           time.sleep(0.01)
# writing
           with open('lut.txt', 'r') as f:
            for line in f:
             #print line
             ser.write('N')
             time.sleep(0.01)
             ser.write(' ')
             time.sleep(0.01)
             ser.write('C')
             time.sleep(0.01)
             for line1 in range(1,8):
              ser.write(line[line1-1])
              time.sleep(0.01)
              line+=1
              time.sleep(0.01)



    except Exception, e1:
        print "error communicating...: " + str(e1)

else:
    print "cannot open serial port "

2 个答案:

答案 0 :(得分:0)

这就是我现在正在做的事情:

表示f:

private static string ExportToBase64Pdf(DataTable dt, string entityName)
    {
        using (MemoryStream memoryStream = new MemoryStream())
        {
            Document document = new Document(PageSize.A4);
            PdfWriter writer = PdfWriter.GetInstance(document, memoryStream);
            document.Open();
            Font font5 = FontFactory.GetFont(FontFactory.HELVETICA, 5);

            PdfPTable table = new PdfPTable(dt.Columns.Count);
            float[] widths = new float[dt.Columns.Count];

            for (int i =0; i<dt.Columns.Count; i++) { widths[i] = 4f; }
            table.SetWidths(widths);
            table.WidthPercentage = 100;

            PdfPCell cell = new PdfPCell(new Phrase(entityName));

            cell.Colspan = dt.Columns.Count;

            foreach (DataColumn c in dt.Columns)
            {
                table.AddCell(new Phrase(c.ColumnName, font5));
            }

            foreach (DataRow r in dt.Rows)
            {
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    table.AddCell(new Phrase(r[i].ToString(), font5));
                }
            }
            document.Add(table);

            var bytes = memoryStream.ToArray();
            var encodedPDF = Convert.ToBase64String(bytes);

            document.Close();
            return encodedPDF;
        }
    }

答案 1 :(得分:0)

问题以更简单的方式解决了。供将来参考。

for i in line:
              newdata=i
              ser.write(newdata)
              time.sleep(0.01)