Serial.Readbit

Hello,

is it possible to read serial data from PC to arduino as bits? It would be much faster...for example if you want read a number "255" you have to read 3 bytes (1st Byte=2, 2nd B=5, 3th B=5) instead of 1 Byte...(as binary 11111111).

Tnx, luka

You can transmit binary 11111111 as one byte and then read it as one byte at the other end.

Pete

You can send and receive serial data to and from the arduino using Serial.write() and Serial.read(), respectively. Whatever you are communicating with, however, also has to have that capability.

sprintf(buffer, "%3d", y);
if(!WriteFile(hSerial, buffer, strlen(buffer), &dwBytesRead, NULL)){}

i'm using this code to send 3 digit number from PC to arduino... unfortunately every digit as one byte. How would you send it binary?

Don't use sprintf() to convert it to a string before sending it.

tnx,

do you maybe know how the number 256( binary 00000001 000000) would be sent? Which one is sent as 1st byte?

Do you maybe have any idea how to send always 2 bytes? - for example: the number to send is 255 (bin 00000000 11111111)?

difenbahij: tnx,

do you maybe know how the number 256( binary 00000001 000000) would be sent? Which one is sent as 1st byte?

No, but it takes about two minutes to figure it out. I think it might be least significant byte first when using i2c from arduino to arduino, but I think it's MSB first for i686 processors.

Do you maybe have any idea how to send always 2 bytes? - for example: the number to send is 255 (bin 00000000 11111111)?

Make the number to send a uint16_t -- u for unsigned, int for integer, 16 for 16 bits, and _t saying it's a datatype

If you need further help, you're probably going to need to tell us more about the program that's sending the data to the arduino -- what language it's written in and the code.

tnx,

i’m writting an application in C++ to send a number from 0 to 600 from PC to arduino and from arduino to touchshield slide to present it on touchshield slide screen.

i stucked with this:
PC

unsigned short buffer;
unsigned short x = 300;
printf(buffer, "%3d", x);
if(!WriteFile(hSerial, buffer, strlen(buffer), &dwBytesRead, NULL)){}
// the errors I get are:
//error C2664: 'printf' : cannot convert parameter 1 from 'unsigned short' to 'const char *'
//error C2664: 'strlen' : cannot convert parameter 1 from 'unsigned short' to 'const char *'
//error C2664: 'WriteFile' : cannot convert parameter 2 from 'unsigned short' to 'LPCVOID'

Arduino

#include <AFSoftSerial.h>
#define RX_PIN 3
#define TX_PIN 2

AFSoftSerial mySerial = AFSoftSerial(RX_PIN, TX_PIN);

int a[2];

void setup (){
  Serial.begin(9600);
  mySerial.begin(9600);
}

void loop (){
  while (Serial.available()<2){}
  
  for (i=0; i<2; i++){
  a[i] = Serial.read();
  }
  for (i=0; i<2 ; i++) 
  mySerial.print(a[i]);
}

TouchShield Slide

int a[2];
int x;

void setup() {
  Serial.begin(9600);
}
 
void loop(){ 
  while (Serial.available()<2){}
                                                           
  for (i=0; i<2 ; i++){ 
  a[i]=Serial.read();
  }
  x = a[0] + a[1];
  text(x, 90, 223);
}
unsigned short buffer;

Which is NOT an array.

printf(buffer, "%3d", x);

Assuming you meant sprintf here, the value in x will be converted to an array of chars, containing at least 3 characters and a terminating NULL (for a minimum of 4 array elements needed).

What you have done here is stomp on at least 2 bytes of memory that you haven't reserved. (Assuming a short is 2 bytes on the PC.)

if(!WriteFile(hSerial, buffer, strlen(buffer), &dwBytesRead, NULL)){}

strlen() expects to be passed a NULL terminated array of characters. What you have passed does not match that definition.

char buffer [4];                

printf(buffer, "%3d", y);
unsigned short b = atoi(buffer);
if(!WriteFile(hSerial, b, 2, &dwBytesRead, NULL)){}

I tried to convert char to int but it seems that because of parameter 2 in 'WriteFile' this isn't possible. According to msdn parameter 2 is 'lpBuffer [in]'

/*BOOL WINAPI WriteFile(
  __in         HANDLE hFile,
  __in         LPCVOID lpBuffer,
  __in         DWORD nNumberOfBytesToWrite,
  __out_opt    LPDWORD lpNumberOfBytesWritten,
  __inout_opt  LPOVERLAPPED lpOverlapped
);*/

lpBuffer [in]

    A pointer to the buffer containing the data to be written to the file or device. This buffer must remain valid for the duration of the write operation. The caller must not use this buffer until the write operation is completed.

I'm wondering if it's even possible to send a int to hSerial... ?

I'm wondering if it's even possible to send a int to hSerial... ?

As an int or as a string representation of an int? Both are possible, but you seem to be trying to mix the two.

PaulS:

I'm wondering if it's even possible to send a int to hSerial... ?

As an int or as a string representation of an int? Both are possible, but you seem to be trying to mix the two.

just as an int ... but if i understand the code i'm using so far, is written to send just char. How to procede?

The 2nd argument to WriteFile is the address where the data starts.

WriteFile(hSerial, (void *)x, sizeof(x));

This will send some bytes to the serial port. How many depends on the type of x.

I can’t collect any data on Arduino and Touchshield slide.

on PC i used

short x = InRec.Event.MouseEvent.dwMousePosition.X
if(!WriteFile(hSerial, (void*)x, sizeof(x), &dwBytesRead, NULL)){}

Arduino

byte a[2];
while (Serial.available<2){}
for (i=0; i<2; i++){a[i] = Serial.read();}
for (i=0; i<2; i++){mySerial.print(a[i]);} //used to send data from Arduino to Touchshield

TSS

byte a[2];
int x;
while (Serial.available<2){}
for (i=0; i<2; i++){a[i] = Serial.read();}
x = a [0] + a[1];
text(x, 280, 223);

where should i correct?

I can't collect any data on Arduino

Does the RX light even flash? Feel free to offer more details. We can't see what you are seeing.

PaulS:

I can't collect any data on Arduino

Does the RX light even flash? Feel free to offer more details. We can't see what you are seeing.

no, the RX doesn't flash.. so there is allready problem with sending from pc... is

if(!WriteFile(hSerial, (void*)x, sizeof(x), &dwBytesRead, NULL)){}

even acceptable? Compiling is otherwise done without error.

so there is allready problem with sending from pc

Yes. Most likely from opening the wrong serial port, or not opening it at all.

is...even acceptable? Compiling is otherwise done without error.

Yes, it looks right. When you are able to get serial data to the Arduino, you'll know.

These are my configurations. Do you think, i should change or add something since i want to send binary coded data?

HANDLE hSerial;
hSerial = CreateFile("COM7", GENERIC_WRITE | GENERIC_READ,
0,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
if(hSerial==INVALID_HANDLE_VALUE){
if(GetLastError()==ERROR_FILE_NOT_FOUND){}}
DCB dcbSerialParams = {0};
if (!GetCommState(hSerial, &dcbSerialParams))

dcbSerialParams.BaudRate=CBR_9600;
dcbSerialParams.ByteSize=8;
dcbSerialParams.StopBits=ONESTOPBIT;
dcbSerialParams.Parity=NOPARITY;
if(!SetCommState(hSerial, &dcbSerialParams)){}

COMMTIMEOUTS timeouts={0};
timeouts.ReadIntervalTimeout=5;
timeouts.ReadTotalTimeoutConstant=5;
timeouts.ReadTotalTimeoutMultiplier=1;

timeouts.WriteTotalTimeoutConstant=5;
timeouts.WriteTotalTimeoutMultiplier=1;
if(!SetCommTimeouts(hSerial, &timeouts)){}

I was missing the address. Tnx for help

if (!WriteFile(hSerial, (void*) &x, sizeof(x), NULL, NULL)) {}
//                              ^ the address was missing