serial communication with device
显示 更早的评论
Hello. I have an instrument (konika chromameter CS100A) which is designed to communicate with a computer through the serial port. I bought a serial-to-usb converter to connect it to my macbookpro. The manual of the chromameter says to use a baud rate of 4800, even parity, 7 bits data length, 2 stop bits and a CR+LF terminator. To request a measurement I should send "MES", then read what is sent. I tried to do so with the following code:
CS100 = serial('/dev/tty.usbserial', 'BaudRate',4800, 'Parity','even',...
'DataBits',7, 'StopBits',2, 'Terminator','CR/LF');
fopen(CS100);
fprintf(CS100,'MES');
pause(5);
received = fgets(CS100);
However matlab returns:"Warning: Unexpected Warning: A timeout occurred before the Terminator was reached. "
And I receive: 'ÅÒ00'
Which in decimal is: [197,210,48,48,141,10]
So I have been puzzled by this for different reasons. Because I specified a data format of 7 bits, I shouldn't receive a character higher than 128, no? Also, the manual of the meter says that if a terminator is not sent 5 seconds after a command, or if the command is undefined, the meter will return "ER00". The terminator should be automatically sent by matlab, shouldn't it? I tried to add it after the command ('MES\r\n') but that made no difference. Matlab receives 6 bytes, which seems consistent with receiving ER00 + 2 terminators. The last character is 10, correctly corresponding to LF. And there is a difference of 13 between the characters 1 and 2, as between E and R, which made me suspect that the string should be "ER00". But then, why are characters 3 and 4 "0", as expected?
Does anyone have an idea on how to send the command to the chromameter and read its answer? Thank you very much.
Baptiste
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Downloads 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!