receiving 2 16bit numbers
Author |
Message |
<spen.. #1 / 4
|
 receiving 2 16bit numbers
Hi, I have managed to achieve this but it falls over sometimes when I assign the incomming four bytes to the two VB double variables. The error message is "overflow". Being new to this type of programming (RS232 data capture) I have cobbled it up in "my own" way. What is the official way - it seems so hard to find specific stuff in the VB 6 manual. My hardware sends a.lowbyte, a.highbyte, c.lowbyte, c.highbyte and VB is set to an interrupt when 4 bytes are received. The hardware can send any byte value for all four bytes. Basically, I want to receive the two16 bit numbers from the PIC microcontroller into two real numbers in VB - a and c. After a lot of messing about I came up with what cannot be the right solution (?) but it works except for the frequent overflows. The overflows occur on the lines starting a=a+256 and c=c+256. Case comEvReceive 'Received RThreshold # of chars Rthreshold =4 'receive 2 - 16-bit words UStimes = MSComm1.Input If UStimes <> "" Then a = Asc(Mid(UStimes, 1, 1)) a = a + 256 * Asc(Mid(UStimes, 2, 2)) c = Asc(Mid(UStimes, 3, 3)) c = c + 256 * Asc(Mid(UStimes, 4, 4)) End If DoEvents draw_graph UStimes = "" 'Case comEvSend ' There are SThreshold number of characters in the TX buffer End Select It works but there must be a "proper" method to capture the four bytes into two integers. In the above it has been necessary to capture them into doubles. any help would be appreciated Spencer
|
Mon, 03 Jun 2002 03:00:00 GMT |
|
 |
Frank Lewi #2 / 4
|
 receiving 2 16bit numbers
VB handles all intermediate variables as Ints unless told otherwise. Cast the intermediate as a Long, and your problem will go away. Also, your code appears to be specifying the incorrect "length" parameter for the Mid function. It should look like this: a = Asc(Mid(UStimes, 1, 1)) a = a + (256& * Asc(Mid(UStimes, 2, 1))) c = Asc(Mid(UStimes, 3, 1)) c = c + (256& * Asc(Mid(UStimes, 4, 1))) Enjoy!
Quote: >Hi, >I have managed to achieve this but it falls over sometimes when I assign the >incomming four bytes to the two VB double variables. The error message is >"overflow". >Being new to this type of programming (RS232 data capture) I have cobbled it >up in "my own" way. What is the official way - it seems so hard to find >specific stuff in the VB 6 manual. >My hardware sends a.lowbyte, a.highbyte, c.lowbyte, c.highbyte and VB is set >to an interrupt when 4 bytes are received. The hardware can send any byte >value for all four bytes. Basically, I want to receive the two16 bit numbers >from the PIC microcontroller into two real numbers in VB - a and c. >After a lot of messing about I came up with what cannot be the right >solution (?) but it works except for the frequent overflows. The overflows >occur on the lines starting a=a+256 and c=c+256. > Case comEvReceive 'Received RThreshold # of chars Rthreshold =4 > 'receive 2 - 16-bit words > UStimes = MSComm1.Input > If UStimes <> "" Then > a = Asc(Mid(UStimes, 1, 1)) > a = a + 256 * Asc(Mid(UStimes, 2, 2)) > c = Asc(Mid(UStimes, 3, 3)) > c = c + 256 * Asc(Mid(UStimes, 4, 4)) > End If > DoEvents > draw_graph > UStimes = "" > 'Case comEvSend ' There are SThreshold number of characters in the TX >buffer >End Select >It works but there must be a "proper" method to capture the four bytes into >two integers. In the above it has been necessary to capture them into >doubles. >any help would be appreciated >Spencer
|
Mon, 03 Jun 2002 03:00:00 GMT |
|
 |
<spen.. #3 / 4
|
 receiving 2 16bit numbers
Hi, I have managed to achieve this but it falls over sometimes when I assign the incomming four bytes to the two VB double variables. The error message is "overflow". Being new to this type of programming (RS232 data capture) I have cobbled it up in "my own" way. What is the official way - it seems so hard to find specific stuff in the VB 6 manual. My hardware sends a.lowbyte, a.highbyte, c.lowbyte, c.highbyte and VB is set to an interrupt when 4 bytes are received. The hardware can send any byte value for all four bytes. Basically, I want to receive the two16 bit numbers from the PIC microcontrolle r into two real numbers in VB - a and c. After a lot of messing about I came up with what cannot be the right solution (?) but it works except for the frequent overflows. The overflows occur on the lines starting a=a+256 and c=c+256. Case comEvReceive 'Received RThreshold # of chars Rthreshold =4 'receive 2 - 16-bit words UStimes = MSComm1.Input If UStimes <> "" Then a = Asc(Mid(UStimes, 1, 1)) a = a + 256 * Asc(Mid(UStimes, 2, 2)) c = Asc(Mid(UStimes, 3, 3)) c = c + 256 * Asc(Mid(UStimes, 4, 4)) End If DoEvents draw_graph UStimes = "" 'Case comEvSend ' There are SThreshold number of characters in the TX buffer End Select It works but there must be a "proper" method to capture the four bytes into two integers. In the above it has been necessary to capture them into doubles. any help would be appreciated Spencer
|
Mon, 03 Jun 2002 03:00:00 GMT |
|
 |
Frank Lew #4 / 4
|
 receiving 2 16bit numbers
VB handles all intermediate variables as Ints unless told otherwise. Cast the intermediate as a Long, and your problem will go away. Also, your code appears to be specifying the incorrect "length" parameter for the Mid function. It should look like this: a = Asc(Mid(UStimes, 1, 1)) a = a + (256& * Asc(Mid(UStimes, 2, 1))) c = Asc(Mid(UStimes, 3, 1)) c = c + (256& * Asc(Mid(UStimes, 4, 1))) Enjoy!
Quote: >Hi, >I have managed to achieve this but it falls over sometimes when I assign the >incomming four bytes to the two VB double variables. The error message is >"overflow". >Being new to this type of programming (RS232 data capture) I have cobbled it >up in "my own" way. What is the official way - it seems so hard to find >specific stuff in the VB 6 manual. >My hardware sends a.lowbyte, a.highbyte, c.lowbyte, c.highbyte and VB is set >to an interrupt when 4 bytes are received. The hardware can send any byte >value for all four bytes. Basically, I want to receive the two16 bit numbers >from the PIC microcontroller into two real numbers in VB - a and c. >After a lot of messing about I came up with what cannot be the right >solution (?) but it works except for the frequent overflows. The overflows >occur on the lines starting a=a+256 and c=c+256. > Case comEvReceive 'Received RThreshold # of chars Rthreshold =4 > 'receive 2 - 16-bit words > UStimes = MSComm1.Input > If UStimes <> "" Then > a = Asc(Mid(UStimes, 1, 1)) > a = a + 256 * Asc(Mid(UStimes, 2, 2)) > c = Asc(Mid(UStimes, 3, 3)) > c = c + 256 * Asc(Mid(UStimes, 4, 4)) > End If > DoEvents > draw_graph > UStimes = "" > 'Case comEvSend ' There are SThreshold number of characters in the TX >buffer >End Select >It works but there must be a "proper" method to capture the four bytes into >two integers. In the above it has been necessary to capture them into >doubles. >any help would be appreciated >Spencer
|
Mon, 03 Jun 2002 03:00:00 GMT |
|
|
|