Lantronix Developer Forums  

Go Back   Lantronix Developer Forums > PremierWave > Python Scripting > General Questions and Discussion

General Questions and Discussion Discuss Python on PremierWave here

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 05-03-2016, 10:42 AM
chrischen chrischen is offline
Junior Member
 
Join Date: Apr 2016
Posts: 4
Exclamation PySerial issue

Hi,

I have a premierwave xn communication with a omron PLC through serial port with hostlink protocol.
My script is working fine with sending command to PLC and receive the response from the PLC.

But when I disconnect the serial cable, the script will hang.
Here is my code:

import os
import serial
import datetime

ser = serial.Serial(port = '/dev/ttyS2', bytesize = 7, stopbits = 1, parity = serial.PARITY_EVEN)

def recv(serial):
while True:
buffer = ''
buffer = serial.read(size = 15) #step A
if buffer:
return buffer

def send(data):
ser.write(data)

command ='@01RR0000000140*\r'

while True:
send (command)
response = recv(ser)

When I disconnect the cable, the script is not dead with exception, it hangs at step A. Even I set a read timeout, it won't return me empty string. It hangs too.
I want to know, how can I make my script work even disconnecting happened.

Thanks and best regards,
Chris
Reply With Quote
  #2  
Old 05-12-2016, 01:44 AM
chrischen chrischen is offline
Junior Member
 
Join Date: Apr 2016
Posts: 4
Default

I have solved this issue. I think the problem is my in my recv function, I put thie serial.read in a loop. And after that I put it with the write function in a loop too. That's why when I disconnect, it stuck at reading.
After not put the read in a loop, it works fine now.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 06:23 PM.


Copyright Lantronix, Inc. 2013. All rights reserved.
Powered by vBulletin® Copyright ©2000 - 2017, vBulletin Solutions, Inc.