From: Abbas on 9 Jul 2010 18:51 Hi, I am trying to read from a 10 Hz device connected to serial port. I searched the forum and wrote the code. But, I have a problem. The device that I am working with, requires an input to show the results. The inputs are in ASCI, like 'A' or '0', '1', etc. I am using fprintf function to write to the device. But, it does not work. When I show s which is my serial port; TransferStatus is idle and BytesAvailable and ValuesReceived are zero. However, as I keep sending ASCIs s.ValueSent changes. I know that there s nothing wrong with the device and It works with QBASIC. Am I using fprintf wrong? I have tried different Terminators from 0 to 127 and the result was the same. I also tried all kind of '%g', '%s', ... and the result was the same. The RecordStatus is on. I have brought the first part of the code. s = serial('com4') set(s,'BaudRate',4800) s.InputBufferSize = 50000; fopen(s) fprintf(s,'A') Thanks for your help. Abbas
From: Walter Roberson on 9 Jul 2010 21:51 Abbas wrote: > I am using fprintf function to write to the device. > s = serial('com4') > set(s,'BaudRate',4800) > s.InputBufferSize = 50000; > fopen(s) > fprintf(s,'A') http://www.mathworks.com/access/helpdesk/help/techdoc/ref/serial.fprintf.html fprintf(obj,'cmd') writes the string cmd to the device connected to the serial port object, obj. The default format is %s\n. The write operation is synchronous and blocks the command-line until execution completes. So you are sending \n after the 'A', which is perhaps not what you want. fprintf(s,'%c','A') should send just the 'A' without any \n afterwards. I do not recommend relying on the %s\n default format: it is not well known (especially as the regular fprintf to files does not do this) and leads to confusion.
From: Abbas on 12 Jul 2010 13:37 Hi Walter, Thanks for the reply. I was waiting for a notice in my email that somebody has replied. So, Sorry about the delay. Anyways, I have tried '%c' before and that does not work. Right now, I have connected the device to another computer with serial interface (It is a super old computer) and it works. I write 'A' and it responds. So, there is something wrong in my code. I am confused. I am geting this error : "Invalid Precision specified. Type 'help serial/fread' for more information" as I use x = fread(s,1,'ubit13'). I have also tried different precisions (Although I am pretty sure I should use 'ubit13') My idea is there is something wrong with fread or fprintf. I have not figured that out yet. Thanks for your time. Walter Roberson <roberson(a)hushmail.com> wrote in message <KmQZn.6542$xZ2.4504(a)newsfe07.iad>... > Abbas wrote: > > > I am using fprintf function to write to the device. > > > s = serial('com4') > > set(s,'BaudRate',4800) > > s.InputBufferSize = 50000; > > fopen(s) > > fprintf(s,'A') > > > http://www.mathworks.com/access/helpdesk/help/techdoc/ref/serial.fprintf.html > > fprintf(obj,'cmd') writes the string cmd to the device connected to the > serial port object, obj. The default format is %s\n. The write operation > is synchronous and blocks the command-line until execution completes. > > > So you are sending \n after the 'A', which is perhaps not what you want. > > fprintf(s,'%c','A') > > should send just the 'A' without any \n afterwards. > > I do not recommend relying on the %s\n default format: it is not well > known (especially as the regular fprintf to files does not do this) and > leads to confusion.
From: Walter Roberson on 12 Jul 2010 14:36 Abbas wrote: > Anyways, I have tried > '%c' before and that does not work. Right now, I have connected the > device to another computer with serial interface (It is a super old > computer) and it works. I write 'A' and it responds. So, there is > something wrong in my code. I am confused. I am geting this error : > "Invalid Precision specified. Type 'help serial/fread' for more > information" as I use x = fread(s,1,'ubit13'). I have also tried > different precisions (Although I am pretty sure I should use 'ubit13') fprintf() and fread() to a serial device are different than fprintf() and fread() to files; the serial device versions do not support the bit* and ubit* formats. >> > s = serial('com4') >> > set(s,'BaudRate',4800) >> > s.InputBufferSize = 50000; >> > fopen(s) >> > fprintf(s,'A') It would not hurt to be explicit about the start and stop bits and about the flow control format especially. Also because of that big buffer size, it migth be waiting for 50000 bytes to come in before it releases the data, unless you ask to read only s.BytesAvailable bytes (which could be 0.)
From: Abbas on 12 Jul 2010 18:06
Hi Walter, You are right. I cannot use *ubit for serial fread. So, now I am using this code : s = serial('COM2') s.Baudrate = 19200; s.BytesAvailableFcnCount = 100; s.BytesAvailableFcnMode = 'byte'; s.BytesAvailableFcn = @instracallback; s.InputBufferSize = 100; s.OutputBufferSize = 2; set(s,'TimeOut',1); fopen(s) fwrite(s,'R','char') [x,count] = fread(s) fwrite(s,'S','char'); close(s) Using Hyper Terminal comminucator (Windows XP) When I write 'R' to device I see infinite number of binaries until I write 'S' to the device. But, in MATLAB, when I run the code x will be a 5 by 1 matrix and count = 5 I do not know why I am seeing this When I solve this problem, I should read bit by bit (the first 3 bits are the number of the channels and the other 13 are the data for a channel (Every 2 bytes)). Well, for now, I just want to have some infinite data, not a 5 by 1 matrix. I have played with most of the variables and no results. Do you what is wrong? Thanks. Abbas Walter Roberson <roberson(a)hushmail.com> wrote in message <i1fngo$kan$1(a)canopus.cc.umanitoba.ca>... > Abbas wrote: > > > Anyways, I have tried > > '%c' before and that does not work. Right now, I have connected the > > device to another computer with serial interface (It is a super old > > computer) and it works. I write 'A' and it responds. So, there is > > something wrong in my code. I am confused. I am geting this error : > > "Invalid Precision specified. Type 'help serial/fread' for more > > information" as I use x = fread(s,1,'ubit13'). I have also tried > > different precisions (Although I am pretty sure I should use 'ubit13') > > fprintf() and fread() to a serial device are different than fprintf() and > fread() to files; the serial device versions do not support the bit* and ubit* > formats. > > >> > s = serial('com4') > >> > set(s,'BaudRate',4800) > >> > s.InputBufferSize = 50000; > >> > fopen(s) > >> > fprintf(s,'A') > > It would not hurt to be explicit about the start and stop bits and about the > flow control format especially. Also because of that big buffer size, it migth > be waiting for 50000 bytes to come in before it releases the data, unless you > ask to read only s.BytesAvailable bytes (which could be 0.) |