I have a binary question for using the serial port (write and read) at python. I want to use Matlab code transfer to python code. But I don’t know the Matlab "fwrite" function how to use it in the Python. And I don’t know my code which part has some issues for python.
I think it is very easy to do the Matlab code "fwrite" and read, but I want to do it in python. Thank you very much.
Here is the Matlab code:
function [ linkit ] = serialSetup(COMPORT)
linkit = serial(COMPORT);
set(linkit, 'BaudRate', 115200);
linkit.InputBufferSize = 30000;
fopen(linkit);
end
function [ ] = sendmachine(linkit)
A1 = hex2dec({'00','00','00','5C'}); %0x0000005C, Length 32 bits
A2 = hex2dec({'00','00','00','9F'}); %0x0000009F, Length 32 bits
**fwrite(linkit,A1);**
**fwrite(linkit,A2);** % How to tranfer to the python code
end
function [ GSolve ] = remachine(linkit)
[GA1, GA2] = readmachine(linkit); % This is return result from machine
end
Here is my python code
import os
import sys
import serial
A1 =('00','00','00','5C') #0x0000005C, Length 32 bits
A2 =('00','00','00','9F') #0x0000009F, Length 32 bits
A1 = [int(i,16) for i in A1]
A2 = [int(i,16) for i in A2]
QCport=serial.Serial(port='COM10',baudrate=115200,stopbits=serial.STOPBITS_ONE,timeout=2,bytesize=8)#here is serial open
QCport.isOpen()
print('t/f = ', QCport.isOpen())
QCport.write(A1) #here is write
QCport.write(A2)
while True:
out= QCport.readline(50000) # here is read
out2=out.decode('utf-8')
print('out=', out) =>the machine return always shown out= b''
User contributions licensed under CC BY-SA 3.0