English 中文(简体)
send string to serial
原标题:

Buongiorno, I m trying to send a simple string to a serial port to command an instrument for noise measures.
The strings are very easy:
"M 1" = instrument on
"M 2" = instrument off
"M 3" = begin the measure
"M 4" = stop the measure

I ve found this program:

import serial

ser = serial.Serial(0)  #Seleziona la porta seriale COM4
ser.baudrate = 9600 #Imposta il baudrate a 9600bps

ser.open() #apre la porta com
ser.close() 
#verifica se la porta e  aperta
if ser.isOpen():
   com_num = ser.portstr
   print ("Porta " + com_num + " aperta")
   #invia il comando alla seriale
   buffer = "M 3"
   ser.write(buffer)     
   #Loop d attesa caratteri
   num = 0
   while num == 0:
       num = ser.inWaiting()
   #scarica il buffer della seriale
   buffer = ser.read(num)
   print ("Dati ricevuti dalla seriale:")
   print buffer
   ser.close()             #chiude la porta
else:
   print ("Porta seriale gia  in uso o inesistente") 
s = raw_input("digita INVIA per uscire")

USING LINUX
The program is ok and I have only some problems with the type of string. In fact I had to insert a carriage return but I can t to do it.
I said that the program is okay because the instrument turn from off to on when I made my first connection, and on video I read "Porta /dev/ttyS= aperta" thet means "open". But this appens for any kind of string I send. In fact this is like an "iniatilization" of the port, not a really communication.
Then I don t manage to send string in the correct way, may be for the problem of carriage return.

USING WINDOWS
I can t open the port, I have an error of Denied Access to the port, this is the error:
Traceback (most recent call last):
File "C:/d.py", line 9, in
ser.open() #apre la porta com
File "C:ProgrammiPython26libsite-packagesserialserialwin32.py", line 53, in open raise SerialException("could not open port %s: %s" % (self.portstr, ctypes.WinError())) serial.serialutil.SerialException: could not open port COM1: [Error 5] Accesso negato.

Can you help me to:
1. Manage to run the program under windows
2. Manage to give in the exactly way the string with carriage return???

Thak you very much.
Stefano

问题回答

To add the carriage return just append to your string, or perhaps, it may need a carriage return/linefeed , which some systems use to mark the end of line. Of course, it may only need a linefeed. In that case, you would simply use .

buffer = "M 3
"

or

buffer = "M 3
"

or

buffer = "M 3
"

You might find the pySerial documentation of help in resolving your Windows issues.

Unfortunately it doesn t work.
In the past I ve already tried to use with the command echo in linux via bash... but nothing.
Now I m trying with python but nothing again. It may be interesting that the instrument, when I give the string in this way:

buffer = "M 3 "
or
buffer = "M 3 "

gives to me this answer: @

That s all...
I don t know.
Another thing: when I ve tried with the bash language the instrument begins the measure with the command:
echo "M 3 " > /dev/ttyS0
that is with a blanck after 3.
Obviously the other command don t work with this strange blanck...

Thank you very much!





相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签