Results 1 to 23 of 23

Thread: serial port read

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2019
    Posts
    4

    serial port read

    Friends I am new to vb6. i am doing project. ol. help me.
    i am sanding data from key board like 00,01, 02,03,04,05 ...... up to 32
    as per serial port in put i play wav file =using MM Control.
    code is attach here with... and i am getting result also

    code:


    Private Sub Form_Load()

    ' Fire Rx Event Every single Bytes
    MSComm1.RThreshold = 8

    ' When Inputting Data, Input 1 Byte at a time
    MSComm1.InputLen = 8

    ' 9600 Baud, No Parity, 8 Data Bits, 1 Stop Bit
    MSComm1.Settings = "9600,N,8,1"
    ' Disable DTR
    MSComm1.DTREnable = False

    ' Open COM1
    MSComm1.CommPort = 1
    MSComm1.PortOpen = True

    End Sub
    Private Sub MSComm1_OnComm()

    Dim Data As String
    Dim file As String
    ' Dim Data() As Byte
    ' If comEvReceive Event then get data and display
    If MSComm1.CommEvent = comEvReceive Then
    Data = MSComm1.Input 'get data
    Lbldata.Caption = Data 'convert to ASCII and display
    Data2 = ""
    For i = 1 To Len(Data)
    x = Left(Data, 1)
    Data = Right(Data, Len(Data) - 1)
    If i = 2 Or i = 3 Then Data2 = Data2 & x


    Next
    Text1.Text = Data2
    file = "C:\Users\Administrator\Desktop\bh prog\n1" & Data2 & ".wav"
    Text2.Text = file
    End If


    MMControl1.Visible = False
    MMControl1.Notify = False
    MMControl1.Wait = True
    MMControl1.Shareable = False
    MMControl1.DeviceType = "WaveAudio" 'specifying which type of audio is
    MMControl1.FileName = Text2 ' path of audio
    MMControl1.Command = "Open" ' default open
    MMControl1.Command = "Play" ' play command
    MMControl1.Command = "wait"
    'MMControl1.Command = "stop"

    End Sub





    now i want if i press 01 first and then 02 then it should play file 01.wav aqnd then 02.wav.

    if nothing after 01 then only file 01

    if 00 is pressed then no wav file will play..

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: serial port read

    Are you saying the keyboard you're typing on is connected to a comm port somehow, i.e. some old fashion serial keyboard?

    Otherwise, I don't see how typing on the keyboard has anything to do with reading from the serial port.

    Or is the keyboard on another computer, and you are sending strings out a serial port from that computer, and receiving it on the computer the code you posted is running?
    Code:
    ' When Inputting Data, Input 1 Byte at a time
    MSComm1.InputLen = 8
    That is setting the InputLen to 8 bytes, not 1 Byte at a time. You would set it to 1 for 1 byte.
    Same for the previous line. The values are in units of bytes, not bits.
    Last edited by passel; Aug 5th, 2020 at 02:01 PM.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2019
    Posts
    4

    Re: serial port read

    Thanks for Reply... actually i am sanding string from multiplex keyboard to my microcontroller and same string to my computer com port..... its. 8 bit string...
    which contents no. of sentence like 01,,,, 02 so on.... my microcontroller device is working properly .... in announcement software which code is given is also working .
    now if my comport gets 01 then it should run 01.wav file ... again and again till 00 comes... if input is 01 then 02 or nothing then announcement of file 01 and 02 both should be announce.... continuously .... i hope it will give you the idea what i want to do...

  4. #4
    New Member
    Join Date
    Aug 2020
    Posts
    2

    Re: serial port read

    Hi there,

    I'm not certain I understand your task correctly, so maybe if I tell you what I am reading, you can correct my understanding. Once I know that, I can help, no problem.

    passel's post identified the core of the problem: word size is a problem.

    I am reading that the serial data arrives as individual single bytes. Each is an "8 bit string..."

    Each 8-bit byte should be read as two 4-bit words.

    The Leftmost 4 bits seem to be insignificant, being 0 in your examples; the rightmost 4 bits are processed to distinguish a unique filename such as "01.wav"
    A file with the given name should loop "continuously " until a "Stop Byte" is received to stop it playing.
    That byte will be all zeroes.


    If that understanding is correct, then
    1. Don't set .RThreshold or .InputLen. They default to 0 and that will work.
    2.
    Data = MSComm1.Input 'get data
    Lbldata.Caption = Data 'convert to ASCII and display
    is not going to convert your incoming data as you need it to... It needs to be changed.
    3. Your MMControl1. code will not loop the audio as you want it to. You will have to add MMControl1_PlayCompleted code to restart it to get the looping done.


    If that understanding is not correct, then you'll need to correct it for me.

    There's no point cranking out code until the problem is defined and understood.

    Come back on it.

  5. #5
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: serial port read

    As stated above the 8 in the Rthreshold and InputLen refer to bytes not bits.
    A setting of 1 sets it to one byte.

    The receive event even when set to one does not fire for every byte. It fires when there is at least 1 byte received. It is up to you to read whatever is there and process it according to your needs. When that event fires if there was 12 bytes received then you will have 12 bytes in your input buffer. It will not fire again until there is additional data received. If you want to read the data one byte at a time then you need to set up a loop and read from the buffer one byte and keep doing so until the buffer is empty.

  6. #6
    New Member
    Join Date
    Aug 2020
    Posts
    2

    Re: serial port read

    I was re-reading while checking a response and see that I may have over-thought the incoming data format.
    Is this just a single-byte code between 0x01 and 0x20 as selectors for 32 files and 0x00 as the Stop All command?

    You also say that your announcement software is working when the requirement is to "announce.... continuously" and the code you've posted can play only once.

    Be sure to include that info when you reply please.

  7. #7
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: serial port read

    No idea where you found that stuff to copy/paste together, but there are lots of problems even in the simple coding there.

    No idea what the traffic between the two is supposed to look like. You seem to speak randomly of "sentences" and "bits" and "bytes" and "ASCII" but either you don't know what those words mean or you are using machine-translation and something has gotten lost in that process.

    Here's a quick hack.

    It seems pretty clear that the microcontroller is barely doing anything, leaving the heavy lifting to the VB6 program. Here I simulated an MSComm control talking to a microcontroller with small screen and keypad.

    I punted and assumed you were trying to say you accumulate keypresses and send them in a clump, with a byte value corresponding to each key pressed on the keypad. Your "0" key seems to have special meaning, something like "clear the values buffered and stop playing, wait for more sounds to play."

    Name:  sshot.png
Views: 509
Size:  2.9 KB

    The attachment is large because I included four .MP3 sound samples. I did not include samples for keypad keys 5 through 9.
    Attached Files Attached Files

  8. #8

    Thread Starter
    New Member
    Join Date
    Sep 2019
    Posts
    4

    Re: serial port read

    Thanks Every one for Replying me.... hope this will clear you all what exactly what i want....

    I have one LED board having 8* 8 matrix with memory have pre loaded 32 nos of sentences indicating location.
    to select location i use keypad which is connected to Led Board and to PC using RS 232 port
    pressing 01 and * (star key) is selecting location 01 and display msg on Led board (go to meter Room) (msg. no. 01 out of 32 msg.)
    i can have 4 msg at a time (01 * 02* 03 * and 04*) will display location 01,02,03 and 04 one by one....

    on pc at 232 port i get string LIKE (^01Ã|^A|) 01 is location. (pressed 01*)
    on receiving data on pc 232 port.... particular location sentence has to be announce through pc sound card from pre recorded .wav file (01.wav)

    if 232 port receive more then one input then all sentence has to be announce one by one...maximum 4 location (looping ) till "00" selected....
    Last edited by cws_ap; Aug 10th, 2020 at 12:49 AM.

  9. #9
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: serial port read

    You only seem to be using 2 characters out of the 8 characters being sent. No idea why you are looping through one by one and just dropping all but the two. In that case would make more sense to use Mid$() to just pull the 2 and get rid of that loop.

    There are two other things you have to consider though. 1: you may get more than one set of data on any given read and 2: the data may not be complete on any given read.
    So your code needs to be able to tell when it has all the characters that would be sent and it needs to parse them into single transactions.

    You showed us what it would look like if they pressed 1 but what if they pressed 1,2,3 what would that look like coming in?

  10. #10
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: serial port read

    Quote Originally Posted by cws_ap View Post
    ....

    on pc at 232 port i get string LIKE (^01Ã|^A|) 01 is location. (pressed 01*)
    ...
    Based on the "string" you're seeing, you are not sending characters, but byte values, so you should be reading into a byte or byte array, not into a string.
    For instance, if you send a byte with a value of 1, in ASCII/ANSI parlance, that could be considered a character that is generated for the Control-A combination, which gets represented in your string as "^A".

    If you read the data from the serial port into a byte, you can process the value of each byte read.
    If you put a breakpoint on where it read the byte, as you hit each key, you could look at the value of the byte, i.e. 0, 1, 2 etc.
    If you are entering an * from your keypad, I don't know what byte value that ends up being, (I'm thinking it will not be the ASCII code for a "*" character).
    Once you know what byte value each key ends up sending, then you should be better ready to convert that value into the string you want to add to your file name.

    You will need a periodic process, probably using a timer tick event, to repeat the audio outputs you want. I haven't looked at dilettante's code, but perhaps there is code in there to help with that part. Maybe even help with the whole process.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  11. #11
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: serial port read

    Another issue is that if you are using actual RS-232 and COMx: port (instead of something like a USB "communications device class" that emulates the hardware) detecting and dealing with errors is important.

  12. #12
    Member
    Join Date
    Aug 2020
    Posts
    56

    Re: serial port read

    Code:
    MSComm1.InputLen = 1
    MSComm1.RThreshold = 1
    Will this combination of settings give an comEvReceive event for each received character?

  13. #13
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: serial port read

    Quote Originally Posted by Zann View Post
    Code:
    MSComm1.InputLen = 1
    MSComm1.RThreshold = 1
    Will this combination of settings give an comEvReceive event for each received character?
    No.
    RThreshold tells it the minimum number of characters it should receive before firing a receive event. There can be more.
    InputLen tells it how many characters to read from the buffer when using the input method. usually this is left at 0 in order to read as many as may be there. Setting this to one restricts the input method to reading only 1 byte even if there are 1000 bytes available to read.

    You may want to have a look at this https://firmanupm.files.wordpress.co.../mscomm_vb.pdf

  14. #14
    Member
    Join Date
    Aug 2020
    Posts
    56

    Re: serial port read

    If there is more than one character in the buffer, and I read only one, by using InputLen = 1, will there be another receive event for other characters?

  15. #15
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: serial port read

    Quote Originally Posted by Zann View Post
    If there is more than one character in the buffer, and I read only one, by using InputLen = 1, will there be another receive event for other characters?
    No. There will be another receive event when more data is received. The remaining data will be left in the inputbuffer. So if you receive 1000 bytes the event fires to let you know it received data, you read 1 byte so there is 999 bytes left to read. If nothing more is being received then no more receive events will be fired as you already have what was sent and nothing is being received.

    Reading 1 byte at a time is not a good idea, can be very slow and can result in buffer over runs depending on what speed you are using and what you are doing with that byte of data. If is generally better to read all the data that has been received and store it in your own buffer variable and then process as needed.

  16. #16
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: serial port read

    Since he is reading button presses from a keypad, I don't think he will outrun his code.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  17. #17
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: serial port read

    Quote Originally Posted by passel View Post
    Since he is reading button presses from a keypad, I don't think he will outrun his code.
    Different poster name from the OP so no idea what he may be doing but yes overrun is unlikely.

  18. #18
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: serial port read

    Yes, that makes a difference. I don't know why I don't check for hijackers.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  19. #19
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: serial port read

    I assumed he was buffering at the microcontroller, since he said something about "sentences" which I took a guess might have meant something like "messages."

    But it is hard to figure out just what is going on here.

    Re-reading post #1 above makes me think entries are two decimal digits (from 00 through 32) and these get sent as two ASCII characters... or something. If so (instead of byte values) then he also has to make sure he gets an even number of input characters to process, at least holding onto any trailing odd character until more data arrives.

  20. #20
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: serial port read

    In a later post he shows an example where it is sending 8 characters for 1 key pressed.
    He seems to be ignoring all the characters except the ones at positions 2 and 3 of the 8 character string.

  21. #21
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: serial port read

    No, I don't think it is 8 characters for 1 key press.

    Each audio message is identified by a two digit number 01 through 32.
    He can have up to four of these messages playing in a loop, so he would enter up to 8 digits, so I think that is the reason for the 8 character string stuff.
    But, it looks like he actually has a separator between the numbers, so it would be 2 digits, separator, 2 digits, separator, .... so up to 12 characters really.
    But, I don't think they are characters. The string he says he is getting when he is pressing 01 contains ^A which represents Ctrl-A, which is an ASCII code byte value of 1.
    So, when he enters 01 on the keypad, I think he gets two bytes, a 0 followed by a 1. If he presses * he probably gets some other value, which we don't know. Since the digits don't appear to be ASCII code, I would doubt the * would generate the ASCII code for "*" either. More than likely it is in the 10 to 12 range.

    So, the initial code parsing he posted was trying to process the "stream" in pairs or triplets, and shifting the string down as he removed them. The idea appearing to be he would process the string from the left end, removing and pairing up the numbers as he went. But the method of collecting those pairs so that he could loop through those audio messages continuously hadn't been worked out at all.
    Last edited by passel; Aug 13th, 2020 at 11:47 PM.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  22. #22
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: serial port read

    Well he said
    i get string LIKE(^01Ã|^A|) 01 is location. (pressed 01*)
    I assume that is the actual text that is received which is 8 bytes for one key pressed. Or perhaps the ( ) are part of it in which case it would be 10 bytes

    Also the code he posted reads 8 bytes and ignores all but bytes 2 and 3
    Last edited by DataMiser; Aug 14th, 2020 at 07:50 AM.

  23. #23
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: serial port read

    Well, you could be correct. There may be additional data from the keypad, perhaps a preface and a length, and a key value and a post value, etc.
    Reading possible non string data into a string, which the string reader can massage in unknown ways to make it printable/readable in whatever manner it is designed to do is not the way I would approach an unknown serial stream. I would always read it into an array of bytes so I can examine the byte values unmolested by a string interpreter and analyze those for a recognizable pattern associated with each key press.
    When you have meta data surrounding the data you want, and that meta data, i.e. a length, CRC value, or simple checksum which can change depending on the content of the data itself, can make decoding a string decoded version of the stream non-repeatable or subject to various inconsistencies.

    Better to look at the raw byte values, so you know you are looking at the true values of the byte stream, rather than an interpretation of the byte stream.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width